23 lines
494 B
TypeScript
23 lines
494 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) {
|
|
this.promise = new Promise<T>(async (resolve) => {
|
|
resolve(await this.func());
|
|
});
|
|
}
|
|
let promise = this.promise;
|
|
let result = await promise;
|
|
if (promise === this.promise) {
|
|
this.promise = null;
|
|
}
|
|
return result;
|
|
}
|
|
}
|