MCPcopy
hub / github.com/conwnet/github1s / reuseable

Function reuseable

extensions/github1s/src/helpers/func.ts:13–29  ·  view source on GitHub ↗
(
	func: T,
	computeCacheKey: (...args: Parameters<T>) => string = defaultComputeCacheKey,
)

Source from the content-addressed store, hash-verified

11// reuse previous promise when a request call
12// and previous request not completed
13export const reuseable = <T extends (...args: any[]) => Promise<any>>(
14 func: T,
15 computeCacheKey: (...args: Parameters<T>) => string = defaultComputeCacheKey,
16) => {
17 const cache = new Map<string, ReturnType<T>>();
18
19 return function (...args: Parameters<T>): ReturnType<T> {
20 const key = computeCacheKey(...args);
21 if (cache.has(key)) {
22 return cache.get(key)!;
23 }
24
25 const promise = func.call(this, ...args);
26 cache.set(key, promise);
27 return pFinally(promise, () => cache.delete(key));
28 };
29};
30
31export const throttle = <T extends (...args: any[]) => any>(func: T, interval: number) => {
32 let timer: ReturnType<typeof setTimeout> | null = null;

Callers 8

BranchTagManagerClass · 0.90
CommitManagerClass · 0.90
CodeReviewManagerClass · 0.90
GitLabFetcherClass · 0.90

Calls 2

setMethod · 0.80
deleteMethod · 0.65

Tested by

no test coverage detected