wait for event refactor
This commit is contained in:
@@ -84,10 +84,7 @@ describe(`Library watcher (e2e)`, () => {
|
||||
`${IMMICH_TEST_ASSET_TEMP_PATH}/file5.jPg`,
|
||||
);
|
||||
|
||||
await waitForEvent(libraryService, StorageEvent.ADD);
|
||||
await waitForEvent(libraryService, StorageEvent.ADD);
|
||||
await waitForEvent(libraryService, StorageEvent.ADD);
|
||||
await waitForEvent(libraryService, StorageEvent.ADD);
|
||||
await waitForEvent(libraryService, StorageEvent.ADD, 4);
|
||||
|
||||
const afterAssets = await api.assetApi.getAllAssets(server, admin.accessToken);
|
||||
expect(afterAssets.length).toEqual(4);
|
||||
@@ -161,9 +158,7 @@ describe(`Library watcher (e2e)`, () => {
|
||||
`${IMMICH_TEST_ASSET_TEMP_PATH}/dir3/file4.jpg`,
|
||||
);
|
||||
|
||||
await waitForEvent(libraryService, StorageEvent.ADD);
|
||||
await waitForEvent(libraryService, StorageEvent.ADD);
|
||||
await waitForEvent(libraryService, StorageEvent.ADD);
|
||||
await waitForEvent(libraryService, StorageEvent.ADD, 3);
|
||||
|
||||
const assets = await api.assetApi.getAllAssets(server, admin.accessToken);
|
||||
expect(assets.length).toEqual(3);
|
||||
|
||||
@@ -138,19 +138,26 @@ export const testApp = {
|
||||
},
|
||||
};
|
||||
|
||||
export function waitForEvent<T>(emitter: EventEmitter, event: string): Promise<T> {
|
||||
return new Promise((resolve, reject) => {
|
||||
const success = (value: T) => {
|
||||
emitter.off(StorageEvent.ERROR, fail);
|
||||
resolve(value);
|
||||
};
|
||||
const fail = (error: Error) => {
|
||||
emitter.off(event, success);
|
||||
reject(error);
|
||||
};
|
||||
emitter.once(event, success);
|
||||
emitter.once(StorageEvent.ERROR, fail);
|
||||
});
|
||||
export function waitForEvent(emitter: EventEmitter, event: string, times = 1): Promise<void[]> {
|
||||
const promises: Promise<void>[] = [];
|
||||
|
||||
for (let i = 1; i <= times; i++) {
|
||||
promises.push(
|
||||
new Promise((resolve, reject) => {
|
||||
const success = (value: any) => {
|
||||
emitter.off(StorageEvent.ERROR, fail);
|
||||
resolve(value);
|
||||
};
|
||||
const fail = (error: Error) => {
|
||||
emitter.off(event, success);
|
||||
reject(error);
|
||||
};
|
||||
emitter.once(event, success);
|
||||
emitter.once(StorageEvent.ERROR, fail);
|
||||
}),
|
||||
);
|
||||
}
|
||||
return Promise.all(promises);
|
||||
}
|
||||
|
||||
const directoryExists = async (dirPath: string) =>
|
||||
|
||||
Reference in New Issue
Block a user