From 616df33e1ddac3100cfc27d1ee43f39b74ff49fe Mon Sep 17 00:00:00 2001 From: Viktor Vasas Date: Thu, 22 Aug 2024 14:58:19 +0200 Subject: [PATCH] fix: core wallet test, provider ready promise --- .../initializeInpageProvider.test.ts | 16 ++++++++++++--- .../utils/ProviderReadyPromise.test.ts | 3 +-- .../providers/utils/ProviderReadyPromise.ts | 20 +++++++++---------- 3 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/background/providers/initializeInpageProvider.test.ts b/src/background/providers/initializeInpageProvider.test.ts index d5e9d386..259bfd79 100644 --- a/src/background/providers/initializeInpageProvider.test.ts +++ b/src/background/providers/initializeInpageProvider.test.ts @@ -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, @@ -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' ); diff --git a/src/background/providers/utils/ProviderReadyPromise.test.ts b/src/background/providers/utils/ProviderReadyPromise.test.ts index 3c92d147..e9550557 100644 --- a/src/background/providers/utils/ProviderReadyPromise.test.ts +++ b/src/background/providers/utils/ProviderReadyPromise.test.ts @@ -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); diff --git a/src/background/providers/utils/ProviderReadyPromise.ts b/src/background/providers/utils/ProviderReadyPromise.ts index fd8fb09b..e877d73a 100644 --- a/src/background/providers/utils/ProviderReadyPromise.ts +++ b/src/background/providers/utils/ProviderReadyPromise.ts @@ -4,34 +4,32 @@ export enum InitializationStep { } export class ProviderReadyPromise { - #unpreparedSteps: InitializationStep[] = []; + #unpreparedSteps: Map = new Map(); #inflightRequests: { resolve(value: unknown): void; - fn(): Promise; + fn(): Promise; }[] = []; 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; } @@ -41,7 +39,7 @@ export class ProviderReadyPromise { } }; - call = (fn) => { + call = (fn: () => Promise) => { return new Promise((resolve) => { this.#inflightRequests.push({ fn,