mirror of
https://activitypub.software/TransFem-org/Sharkey.git
synced 2024-12-16 01:55:44 +01:00
29f6ba6310
* chore: add workflow which checks if SPDX ID exists * chore: add missing SPDX ID in some files * chore: change trigger condition * chore: trigger on push * lint
57 lines
1.4 KiB
TypeScript
57 lines
1.4 KiB
TypeScript
/*
|
|
* SPDX-FileCopyrightText: syuilo and misskey-project
|
|
* SPDX-License-Identifier: AGPL-3.0-only
|
|
*/
|
|
|
|
export type FetchFunction<K, V> = (key: K) => Promise<V>;
|
|
|
|
type ResolveReject<V> = Parameters<ConstructorParameters<typeof Promise<V>>[0]>;
|
|
|
|
type ResolverPair<V> = {
|
|
resolve: ResolveReject<V>[0];
|
|
reject: ResolveReject<V>[1];
|
|
};
|
|
|
|
export class DebounceLoader<K, V> {
|
|
private resolverMap = new Map<K, ResolverPair<V>>();
|
|
private promiseMap = new Map<K, Promise<V>>();
|
|
private resolvedPromise = Promise.resolve();
|
|
constructor(private loadFn: FetchFunction<K, V>) {}
|
|
|
|
public load(key: K): Promise<V> {
|
|
const promise = this.promiseMap.get(key);
|
|
if (typeof promise !== 'undefined') {
|
|
return promise;
|
|
}
|
|
|
|
const isFirst = this.promiseMap.size === 0;
|
|
const newPromise = new Promise<V>((resolve, reject) => {
|
|
this.resolverMap.set(key, { resolve, reject });
|
|
});
|
|
this.promiseMap.set(key, newPromise);
|
|
|
|
if (isFirst) {
|
|
this.enqueueDebouncedLoadJob();
|
|
}
|
|
|
|
return newPromise;
|
|
}
|
|
|
|
private runDebouncedLoad(): void {
|
|
const resolvers = [...this.resolverMap];
|
|
this.resolverMap.clear();
|
|
this.promiseMap.clear();
|
|
|
|
for (const [key, { resolve, reject }] of resolvers) {
|
|
this.loadFn(key).then(resolve, reject);
|
|
}
|
|
}
|
|
|
|
private enqueueDebouncedLoadJob(): void {
|
|
this.resolvedPromise.then(() => {
|
|
process.nextTick(() => {
|
|
this.runDebouncedLoad();
|
|
});
|
|
});
|
|
}
|
|
}
|