Skip to content

Commit

Permalink
fix: core wallet test, provider ready promise
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava committed Aug 22, 2024
1 parent 2ab0518 commit 616df33
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 16 deletions.
16 changes: 13 additions & 3 deletions src/background/providers/initializeInpageProvider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,9 +185,6 @@ describe('src/background/providers/initializeInpageProvider', () => {
expect(windowMock.dispatchEvent.mock.calls[3][0].type).toEqual(
'eip6963:announceProvider'
);
expect(windowMock.dispatchEvent.mock.calls[4][0].type).toEqual(
'core-wallet:announceProvider'
);
expect(windowMock.dispatchEvent.mock.calls[3][0].detail).toEqual({
info: provider.info,
provider: provider,
Expand All @@ -211,6 +208,19 @@ describe('src/background/providers/initializeInpageProvider', () => {
expect(windowMock.dispatchEvent.mock.calls[3][0].type).toEqual(
'eip6963:announceProvider'
);
});
});
describe('core-wallet ', () => {
it('should announce chainagnostic provider with core-wallet:announceProvider', () => {
initializeProvider(connectionMock, 10, windowMock);

expect(windowMock.dispatchEvent.mock.calls[4][0].type).toEqual(
'core-wallet:announceProvider'
);
});
it('should re-announce on core-wallet:requestProvider', () => {
initializeProvider(connectionMock, 10, windowMock);

expect(windowMock.dispatchEvent.mock.calls[4][0].type).toEqual(
'core-wallet:announceProvider'
);
Expand Down
3 changes: 1 addition & 2 deletions src/background/providers/utils/ProviderReadyPromise.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ describe('src/background/providers/utils/ProviderReadyPromise', () => {
initializedPromise.uncheck(InitializationStep.DOMAIN_METADATA_SENT);
initializedPromise.call(callMock);

expect(callMock).toHaveBeenCalledTimes(2);

expect(callMock).toHaveBeenCalledTimes(1);
initializedPromise.check(InitializationStep.DOMAIN_METADATA_SENT);

await new Promise(process.nextTick);
Expand Down
20 changes: 9 additions & 11 deletions src/background/providers/utils/ProviderReadyPromise.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,32 @@ export enum InitializationStep {
}

export class ProviderReadyPromise {
#unpreparedSteps: InitializationStep[] = [];
#unpreparedSteps: Map<InitializationStep, boolean> = new Map();
#inflightRequests: {
resolve(value: unknown): void;
fn(): Promise<any>;
fn(): Promise<unknown>;
}[] = [];

constructor(steps: InitializationStep[]) {
this.#unpreparedSteps = Object.values(steps);
steps.map((step) => this.#unpreparedSteps.set(step, true));
}

check = (step: InitializationStep) => {
const stepIndex = this.#unpreparedSteps.findIndex(
(currentStep) => step === currentStep
);
const hasStep = this.#unpreparedSteps.has(step);

if (stepIndex > -1) {
this.#unpreparedSteps.splice(stepIndex, 1);
if (hasStep) {
this.#unpreparedSteps.delete(step);
}

this._proceed();
};

uncheck = (step: InitializationStep) => {
this.#unpreparedSteps[step] = step;
this.#unpreparedSteps.set(step, true);
};

private _proceed = () => {
if (this.#unpreparedSteps.length) {
if (this.#unpreparedSteps.size) {
return;
}

Expand All @@ -41,7 +39,7 @@ export class ProviderReadyPromise {
}
};

call = (fn) => {
call = (fn: () => Promise<unknown>) => {
return new Promise((resolve) => {
this.#inflightRequests.push({
fn,
Expand Down

0 comments on commit 616df33

Please sign in to comment.