Skip to content

Commit

Permalink
chore: allow to give arguments propagated to the updater method
Browse files Browse the repository at this point in the history
allow to give extra args when calling manual fetch method
then, the updater method can check these arguments

related to podman-desktop#3376

Signed-off-by: Florent Benoit <[email protected]>
  • Loading branch information
benoitf committed Aug 22, 2023
1 parent 451f62c commit dd538de
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 5 deletions.
29 changes: 29 additions & 0 deletions packages/renderer/src/stores/event-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyCustomTypeInfo[]> = 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);
});
14 changes: 9 additions & 5 deletions packages/renderer/src/stores/event-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface EventStoreInfo {
clearEvents(): void;

// force a fetch of the data to update the store
fetch(): Promise<void>;
fetch(...args: unknown[]): Promise<void>;
}

// Helper to manage store updated from events
Expand All @@ -65,7 +65,7 @@ export class EventStore<T> {
private checkForUpdate: (eventName: string, args?: unknown[]) => Promise<boolean>,
private windowEvents: string[],
private windowListeners: string[],
private updater: () => Promise<T>,
private updater: (...args: unknown[]) => Promise<T>,
private iconComponent?: ComponentType,
) {
if (!iconComponent) {
Expand All @@ -89,7 +89,11 @@ export class EventStore<T> {
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 });
Expand Down Expand Up @@ -129,8 +133,8 @@ export class EventStore<T> {
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);
},
};
Expand Down

0 comments on commit dd538de

Please sign in to comment.