Compare commits

...

1 Commits

Author SHA1 Message Date
Nicolas Dorseuil 6edf3b78f0 use dataFetcher itself as the key for the weakmap 2025-06-12 14:24:11 +02:00
2 changed files with 654 additions and 612 deletions
File diff suppressed because it is too large Load Diff
+3 -6
View File
@@ -1,20 +1,17 @@
import { cache } from 'react'; import type { GitBookDataFetcher } from './types';
// This is used to create a context specific to the current request.
// This version works both in cloudflare and in vercel.
const getRequestContext = cache(() => ({}));
/** /**
* Wrap a function by preventing concurrent executions of the same function. * Wrap a function by preventing concurrent executions of the same function.
* With a logic to work per-request in Cloudflare Workers. * With a logic to work per-request in Cloudflare Workers.
*/ */
export function withoutConcurrentExecution<ArgsType extends any[], ReturnType>( export function withoutConcurrentExecution<ArgsType extends any[], ReturnType>(
dataFetcherObject: GitBookDataFetcher,
wrapped: (key: string, ...args: ArgsType) => Promise<ReturnType> wrapped: (key: string, ...args: ArgsType) => Promise<ReturnType>
): (cacheKey: string, ...args: ArgsType) => Promise<ReturnType> { ): (cacheKey: string, ...args: ArgsType) => Promise<ReturnType> {
const globalPromiseCache = new WeakMap<object, Map<string, Promise<ReturnType>>>(); const globalPromiseCache = new WeakMap<object, Map<string, Promise<ReturnType>>>();
return (key: string, ...args: ArgsType) => { return (key: string, ...args: ArgsType) => {
const globalContext = getRequestContext(); const globalContext = dataFetcherObject;
/** /**
* Cache storage that is scoped to the current request when executed in Cloudflare Workers, * Cache storage that is scoped to the current request when executed in Cloudflare Workers,