From e109d68ff0f37dd866627b67e3ac314f351ff409 Mon Sep 17 00:00:00 2001 From: Tobias Deekens Date: Sun, 20 Aug 2023 12:14:07 +0200 Subject: [PATCH] fix(combine-adapters): status to connect underlying adapters (#1791) * fix(combine-adapters): status to connect underlying adapters * Create moody-beds-rhyme.md --- .changeset/moody-beds-rhyme.md | 5 ++ .../src/adapter/adapter.spec.js | 50 +++++++++++++++---- .../combine-adapters/src/adapter/adapter.ts | 18 ++----- 3 files changed, 50 insertions(+), 23 deletions(-) create mode 100644 .changeset/moody-beds-rhyme.md diff --git a/.changeset/moody-beds-rhyme.md b/.changeset/moody-beds-rhyme.md new file mode 100644 index 000000000..6137fd28e --- /dev/null +++ b/.changeset/moody-beds-rhyme.md @@ -0,0 +1,5 @@ +--- +"@flopflip/combine-adapters": patch +--- + +fix(combine-adapters): status to connect underlying adapters diff --git a/packages/combine-adapters/src/adapter/adapter.spec.js b/packages/combine-adapters/src/adapter/adapter.spec.js index 28936a09c..79a7947ce 100644 --- a/packages/combine-adapters/src/adapter/adapter.spec.js +++ b/packages/combine-adapters/src/adapter/adapter.spec.js @@ -178,18 +178,48 @@ describe('when combining', () => { }); it('should resolve `waitUntilConfigured`', async () => { - await expect(adapter.waitUntilConfigured()).resolves.not.toBeDefined(); + await expect(adapter.waitUntilConfigured()).resolves.toBeDefined(); }); - it('should invoke `onStatusStateChange` with configured', () => { - expect(adapterEventHandlers.onStatusStateChange).toHaveBeenCalledWith( - expect.objectContaining({ - id: adapter.id, - status: { - configurationStatus: AdapterConfigurationStatus.Configured, - }, - }) - ); + describe('invokcation of `onStatusStateChange`', () => { + describe('of `combine-adapters`', () => { + it('should invoke `onStatusStateChange` with configured', () => { + expect(adapterEventHandlers.onStatusStateChange).toHaveBeenCalledWith( + expect.objectContaining({ + id: adapter.id, + status: { + configurationStatus: AdapterConfigurationStatus.Configured, + }, + }) + ); + }); + }); + + describe('of `memory-adapter`', () => { + it('should invoke `onStatusStateChange` with configured', () => { + expect(adapterEventHandlers.onStatusStateChange).toHaveBeenCalledWith( + expect.objectContaining({ + id: memoryAdapter.id, + status: { + configurationStatus: AdapterConfigurationStatus.Configured, + }, + }) + ); + }); + }); + + describe('of `localstorage-adapter`', () => { + it('should invoke `onStatusStateChange` with configured', () => { + expect(adapterEventHandlers.onStatusStateChange).toHaveBeenCalledWith( + expect.objectContaining({ + id: localstorageAdapter.id, + status: { + configurationStatus: AdapterConfigurationStatus.Configured, + }, + }) + ); + }); + }); }); it('should invoke `onFlagsStateChange`', () => { diff --git a/packages/combine-adapters/src/adapter/adapter.ts b/packages/combine-adapters/src/adapter/adapter.ts index 9e93a04c9..00cb09db7 100644 --- a/packages/combine-adapters/src/adapter/adapter.ts +++ b/packages/combine-adapters/src/adapter/adapter.ts @@ -129,7 +129,7 @@ class CombineAdapters implements TCombinedAdapterInterface { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument return adapter.configure(adapterArgsForAdapter, { onFlagsStateChange: adapterEventHandlers.onFlagsStateChange, - onStatusStateChange: () => undefined, + onStatusStateChange: adapterEventHandlers.onStatusStateChange, }); }) ).then((allInitializationStatus) => { @@ -192,7 +192,7 @@ class CombineAdapters implements TCombinedAdapterInterface { // eslint-disable-next-line @typescript-eslint/no-unsafe-argument return adapter.reconfigure(adapterArgsForAdapter, { onFlagsStateChange: adapterEventHandlers.onFlagsStateChange, - onStatusStateChange: () => undefined, + onStatusStateChange: adapterEventHandlers.onStatusStateChange, }); }) ).then((allInitializationStatus) => { @@ -229,17 +229,9 @@ class CombineAdapters implements TCombinedAdapterInterface { }; async waitUntilConfigured() { - return new Promise((resolve) => { - if ( - this.getIsConfigurationStatus(AdapterConfigurationStatus.Configured) - ) { - resolve(); - } else - this.#adapterState.emitter.on( - this.#__internalConfiguredStatusChange__, - resolve - ); - }); + return Promise.all( + this.#adapters.map(async (adapter) => adapter?.waitUntilConfigured?.()) + ); } unsubscribe = () => {