mirror of
https://github.com/immich-app/immich.git
synced 2026-05-21 07:06:31 -04:00
e79a98fa82
Change-Id: I0a37b417ee4c247dcc93d442c976eede6a6a6964
37 lines
914 B
TypeScript
37 lines
914 B
TypeScript
export class InvocationTracker {
|
|
invocationsStarted = 0;
|
|
invocationsEnded = 0;
|
|
|
|
startInvocation() {
|
|
this.invocationsStarted++;
|
|
const invocation = this.invocationsStarted;
|
|
|
|
return {
|
|
isStillValid: () => invocation === this.invocationsStarted,
|
|
endInvocation: () => {
|
|
this.invocationsEnded = Math.max(this.invocationsEnded, invocation);
|
|
},
|
|
};
|
|
}
|
|
|
|
isActive() {
|
|
return this.invocationsStarted !== this.invocationsEnded;
|
|
}
|
|
|
|
async invoke<T>(invocable: () => Promise<T>, catchCallback?: (error: unknown) => void, finallyCallback?: () => void) {
|
|
const invocation = this.startInvocation();
|
|
try {
|
|
return await invocable();
|
|
} catch (error: unknown) {
|
|
if (catchCallback) {
|
|
catchCallback(error);
|
|
} else {
|
|
console.error(error);
|
|
}
|
|
} finally {
|
|
invocation.endInvocation();
|
|
finallyCallback?.();
|
|
}
|
|
}
|
|
}
|