MCPcopy Create free account
hub / github.com/Effect-TS/effect / cachedFunction

Function cachedFunction

packages/effect/src/internal/effect/circular.ts:444–475  ·  view source on GitHub ↗
(
  f: (a: A) => Effect.Effect<B, E, R>,
  eq?: Equivalence<A>
)

Source from the content-addressed store, hash-verified

442
443/** @internal */
444export const cachedFunction = <A, B, E, R>(
445 f: (a: A) => Effect.Effect<B, E, R>,
446 eq?: Equivalence<A>
447): Effect.Effect<(a: A) => Effect.Effect<B, E, R>> => {
448 return pipe(
449 core.sync(() => MutableHashMap.empty<Key<A>, Deferred.Deferred<readonly [FiberRefsPatch.FiberRefsPatch, B], E>>()),
450 core.flatMap(makeSynchronized),
451 core.map((ref) => (a: A) =>
452 pipe(
453 ref.modifyEffect((map) => {
454 const result = pipe(map, MutableHashMap.get(new Key(a, eq)))
455 if (Option.isNone(result)) {
456 return pipe(
457 core.deferredMake<readonly [FiberRefsPatch.FiberRefsPatch, B], E>(),
458 core.tap((deferred) =>
459 pipe(
460 effect.diffFiberRefs(f(a)),
461 core.intoDeferred(deferred),
462 fiberRuntime.fork
463 )
464 ),
465 core.map((deferred) => [deferred, pipe(map, MutableHashMap.set(new Key(a, eq), deferred))] as const)
466 )
467 }
468 return core.succeed([result.value, map] as const)
469 }),
470 core.flatMap(core.deferredAwait),
471 core.flatMap(([patch, b]) => pipe(effect.patchFiberRefs(patch), core.as(b)))
472 )
473 )
474 )
475}
476
477/** @internal */
478export const raceFirst = dual<

Callers

nothing calls this directly

Calls 7

syncMethod · 0.80
pipeFunction · 0.70
mapMethod · 0.65
modifyEffectMethod · 0.65
getMethod · 0.65
setMethod · 0.65
fFunction · 0.50

Tested by

no test coverage detected