cordis/client/webapp/dedup-awaiter.ts

26 lines
615 B
TypeScript

// lets you wait until it is uncorked by someone before running
export default class DedupAwaiter<T> {
private promise: Promise<T> | null;
constructor(
private func: () => Promise<T>
) {}
public async call(): Promise<T> {
if (!this.promise) {
let result: T | null = null;
let promise = new Promise<T>(async (resolve) => {
resolve(await this.func());
});
// This if statement could trigger if func is not async
// typescript is missing some fun stuff going on here :)
if (result) {
return result;
} else {
this.promise = promise;
}
}
return await this.promise;
}
}