sailfork / t3code-github / .repos/ alchemy-effect/ packages/ alchemy/ src/ Util/ cached-function.ts

import * as Deferred from "effect/Deferred";
import * as Effect from "effect/Effect";

/**
 * Options for creating a cached function.
 */
export interface CachedFunctionOptions<A> {
  /**
   * Function to convert arguments to a cache key string.
   * Defaults to `JSON.stringify`.
   */
  readonly key?: (args: A) => string;
}

/**
 * Creates a memoized version of a function that returns an Effect.
 *
 * The key feature is deduplication of concurrent calls with the same inputs -
 * only one execution happens while other callers wait for and receive the same result.
 *
 * @example
 * ```ts
 * import * as Effect from "effect/Effect";
 * import { cachedFunction } from "~/lib/cached-function";
 *
 * const fetchUser = (id: string) =>
 *   Effect.promise(() => fetch(`/users/${id}`).then(r => r.json()));
 *
 * const program = Effect.gen(function* () {
 *   const cachedFetchUser = yield* cachedFunction(fetchUser);
 *
 *   // These concurrent calls will only trigger one fetch
 *   const [user1, user2] = yield* Effect.all([
 *     cachedFetchUser("123"),
 *     cachedFetchUser("123"),
 *   ]);
 * });
 * ```
 */
export const cachedFunction = <A extends Array<any>, B, E, R>(
  fn: (...args: A) => Effect.Effect<B, E, R>,
  options?: CachedFunctionOptions<A>,
): Effect.Effect<(...args: A) => Effect.Effect<B, E, R>> =>
  Effect.sync(() => makeCachedFunction(fn, options));

/**
 * {@link cachedFunction} for callers that own the cache's lifetime outside an
 * Effect, such as a `Context.Reference` default.
 */
export const makeCachedFunction = <A extends Array<any>, B, E, R>(
  fn: (...args: A) => Effect.Effect<B, E, R>,
  options?: CachedFunctionOptions<A>,
): ((...args: A) => Effect.Effect<B, E, R>) => {
  const keyFn = options?.key ?? JSON.stringify;
  const cache = new Map<string, Deferred.Deferred<B, E>>();

  return (...args: A): Effect.Effect<B, E, R> =>
    Effect.suspend(() => {
      const cacheKey = keyFn(args);
      const existing = cache.get(cacheKey);

      // If there's already a deferred for this key, wait on it
      if (existing) {
        return Deferred.await(existing);
      }

      // Create a new deferred and store it. Only the call itself is
      // interruptible, so an interrupted first caller still settles its
      // waiters and evicts the key instead of leaving it pending forever.
      return Effect.uninterruptibleMask((restore) =>
        Effect.gen(function* () {
          const deferred = yield* Deferred.make<B, E>();
          cache.set(cacheKey, deferred);

          const exit = yield* Effect.exit(restore(fn(...args)));
          yield* Deferred.done(deferred, exit);

          if (exit._tag === "Failure") {
            cache.delete(cacheKey);
          }

          return yield* exit;
        }),
      );
    });
};