From dd538de36d1bdbcdac10c91a610df6d5bc3c8f9d Mon Sep 17 00:00:00 2001 From: Florent Benoit Date: Tue, 22 Aug 2023 13:57:12 +0200 Subject: [PATCH] chore: allow to give arguments propagated to the updater method allow to give extra args when calling manual fetch method then, the updater method can check these arguments related to https://github.com/containers/podman-desktop/issues/3376 Signed-off-by: Florent Benoit --- .../renderer/src/stores/event-store.spec.ts | 29 +++++++++++++++++++ packages/renderer/src/stores/event-store.ts | 14 +++++---- 2 files changed, 38 insertions(+), 5 deletions(-) diff --git a/packages/renderer/src/stores/event-store.spec.ts b/packages/renderer/src/stores/event-store.spec.ts index 07b174c9038bb..8a3c23c0a2e75 100644 --- a/packages/renderer/src/stores/event-store.spec.ts +++ b/packages/renderer/src/stores/event-store.spec.ts @@ -155,3 +155,32 @@ test('should call fetch method using listener event', async () => { expect(eventStoreInfo.bufferEvents[0]).toHaveProperty('args', undefined); expect(eventStoreInfo.bufferEvents[0]).toHaveProperty('length', 1); }); + +test('should call fetch with arguments', async () => { + const myStoreInfo: Writable = writable([]); + const checkForUpdateMock = vi.fn(); + + const updater = vi.fn(); + + // return true to trigger the update + checkForUpdateMock.mockResolvedValue(true); + + const eventStore = new EventStore('my-listener-test', myStoreInfo, checkForUpdateMock, [], [], updater); + // now call the setup + const eventStoreInfo = eventStore.setup(); + + expect(eventStoreInfo.bufferEvents.length).toBe(0); + + const args = ['my', 'list', 'of', 'arguments']; + + // do a manual fetch + await eventStoreInfo.fetch(...args); + + // wait updater being called + while (updater.mock.calls.length === 0) { + await new Promise(resolve => setTimeout(resolve, 100)); + } + + // check the updater is called + expect(updater).toHaveBeenCalledWith(...args); +}); diff --git a/packages/renderer/src/stores/event-store.ts b/packages/renderer/src/stores/event-store.ts index daad23d40100b..7973bbc743c1b 100644 --- a/packages/renderer/src/stores/event-store.ts +++ b/packages/renderer/src/stores/event-store.ts @@ -53,7 +53,7 @@ export interface EventStoreInfo { clearEvents(): void; // force a fetch of the data to update the store - fetch(): Promise; + fetch(...args: unknown[]): Promise; } // Helper to manage store updated from events @@ -65,7 +65,7 @@ export class EventStore { private checkForUpdate: (eventName: string, args?: unknown[]) => Promise, private windowEvents: string[], private windowListeners: string[], - private updater: () => Promise, + private updater: (...args: unknown[]) => Promise, private iconComponent?: ComponentType, ) { if (!iconComponent) { @@ -89,7 +89,11 @@ export class EventStore { try { if (needUpdate) { const before = performance.now(); - const result = await this.updater(); + const customArgs = []; + if (args) { + customArgs.push(...args); + } + const result = await this.updater(...customArgs); const after = performance.now(); numberOfResults = Array.isArray(result) ? result.length : 0; updateDuration = humanizeDuration(after - before, { units: ['s', 'ms'], round: true, largest: 1 }); @@ -129,8 +133,8 @@ export class EventStore { bufferEvents.length = 0; updateStore(eventStoreInfo); }, - fetch: async () => { - await this.performUpdate(true, eventStoreInfo, 'manual'); + fetch: async (...args: unknown[]) => { + await this.performUpdate(true, eventStoreInfo, 'manual', args); updateStore(eventStoreInfo); }, };