wait for event refactor

This commit is contained in:
Jonathan Jogenfors
2024-02-29 23:04:56 +01:00
parent 967b30cfa5
commit 0f331a3b50
2 changed files with 22 additions and 20 deletions
@@ -84,10 +84,7 @@ describe(`Library watcher (e2e)`, () => {
`${IMMICH_TEST_ASSET_TEMP_PATH}/file5.jPg`, `${IMMICH_TEST_ASSET_TEMP_PATH}/file5.jPg`,
); );
await waitForEvent(libraryService, StorageEvent.ADD); await waitForEvent(libraryService, StorageEvent.ADD, 4);
await waitForEvent(libraryService, StorageEvent.ADD);
await waitForEvent(libraryService, StorageEvent.ADD);
await waitForEvent(libraryService, StorageEvent.ADD);
const afterAssets = await api.assetApi.getAllAssets(server, admin.accessToken); const afterAssets = await api.assetApi.getAllAssets(server, admin.accessToken);
expect(afterAssets.length).toEqual(4); expect(afterAssets.length).toEqual(4);
@@ -161,9 +158,7 @@ describe(`Library watcher (e2e)`, () => {
`${IMMICH_TEST_ASSET_TEMP_PATH}/dir3/file4.jpg`, `${IMMICH_TEST_ASSET_TEMP_PATH}/dir3/file4.jpg`,
); );
await waitForEvent(libraryService, StorageEvent.ADD); await waitForEvent(libraryService, StorageEvent.ADD, 3);
await waitForEvent(libraryService, StorageEvent.ADD);
await waitForEvent(libraryService, StorageEvent.ADD);
const assets = await api.assetApi.getAllAssets(server, admin.accessToken); const assets = await api.assetApi.getAllAssets(server, admin.accessToken);
expect(assets.length).toEqual(3); expect(assets.length).toEqual(3);
+20 -13
View File
@@ -138,19 +138,26 @@ export const testApp = {
}, },
}; };
export function waitForEvent<T>(emitter: EventEmitter, event: string): Promise<T> { export function waitForEvent(emitter: EventEmitter, event: string, times = 1): Promise<void[]> {
return new Promise((resolve, reject) => { const promises: Promise<void>[] = [];
const success = (value: T) => {
emitter.off(StorageEvent.ERROR, fail); for (let i = 1; i <= times; i++) {
resolve(value); promises.push(
}; new Promise((resolve, reject) => {
const fail = (error: Error) => { const success = (value: any) => {
emitter.off(event, success); emitter.off(StorageEvent.ERROR, fail);
reject(error); resolve(value);
}; };
emitter.once(event, success); const fail = (error: Error) => {
emitter.once(StorageEvent.ERROR, fail); emitter.off(event, success);
}); reject(error);
};
emitter.once(event, success);
emitter.once(StorageEvent.ERROR, fail);
}),
);
}
return Promise.all(promises);
} }
const directoryExists = async (dirPath: string) => const directoryExists = async (dirPath: string) =>