The best way to control Claude Code, Codex and more from any computer or phone

main
ts 86 lines 2.72 KB
Raw
1 import * as Deferred from "effect/Deferred";
2 import * as Effect from "effect/Effect";
3
4 /**
5 * Options for creating a cached function.
6 */
7 export interface CachedFunctionOptions<A> {
8 /**
9 * Function to convert arguments to a cache key string.
10 * Defaults to `JSON.stringify`.
11 */
12 readonly key?: (args: A) => string;
13 }
14
15 /**
16 * Creates a memoized version of a function that returns an Effect.
17 *
18 * The key feature is deduplication of concurrent calls with the same inputs -
19 * only one execution happens while other callers wait for and receive the same result.
20 *
21 * @example
22 * ```ts
23 * import * as Effect from "effect/Effect";
24 * import { cachedFunction } from "~/lib/cached-function";
25 *
26 * const fetchUser = (id: string) =>
27 * Effect.promise(() => fetch(`/users/${id}`).then(r => r.json()));
28 *
29 * const program = Effect.gen(function* () {
30 * const cachedFetchUser = yield* cachedFunction(fetchUser);
31 *
32 * // These concurrent calls will only trigger one fetch
33 * const [user1, user2] = yield* Effect.all([
34 * cachedFetchUser("123"),
35 * cachedFetchUser("123"),
36 * ]);
37 * });
38 * ```
39 */
40 export const cachedFunction = <A extends Array<any>, B, E, R>(
41 fn: (...args: A) => Effect.Effect<B, E, R>,
42 options?: CachedFunctionOptions<A>,
43 ): Effect.Effect<(...args: A) => Effect.Effect<B, E, R>> =>
44 Effect.sync(() => makeCachedFunction(fn, options));
45
46 /**
47 * {@link cachedFunction} for callers that own the cache's lifetime outside an
48 * Effect, such as a `Context.Reference` default.
49 */
50 export const makeCachedFunction = <A extends Array<any>, B, E, R>(
51 fn: (...args: A) => Effect.Effect<B, E, R>,
52 options?: CachedFunctionOptions<A>,
53 ): ((...args: A) => Effect.Effect<B, E, R>) => {
54 const keyFn = options?.key ?? JSON.stringify;
55 const cache = new Map<string, Deferred.Deferred<B, E>>();
56
57 return (...args: A): Effect.Effect<B, E, R> =>
58 Effect.suspend(() => {
59 const cacheKey = keyFn(args);
60 const existing = cache.get(cacheKey);
61
62 // If there's already a deferred for this key, wait on it
63 if (existing) {
64 return Deferred.await(existing);
65 }
66
67 // Create a new deferred and store it. Only the call itself is
68 // interruptible, so an interrupted first caller still settles its
69 // waiters and evicts the key instead of leaving it pending forever.
70 return Effect.uninterruptibleMask((restore) =>
71 Effect.gen(function* () {
72 const deferred = yield* Deferred.make<B, E>();
73 cache.set(cacheKey, deferred);
74
75 const exit = yield* Effect.exit(restore(fn(...args)));
76 yield* Deferred.done(deferred, exit);
77
78 if (exit._tag === "Failure") {
79 cache.delete(cacheKey);
80 }
81
82 return yield* exit;
83 }),
84 );
85 });
86 };