import { seconds } from "@effect/data/Duration" import { dual } from "@effect/data/Function" import { globalValue } from "@effect/data/GlobalValue" import type * as Cache from "@effect/io/Cache" import type { Deferred } from "@effect/io/Deferred" import type * as Effect from "@effect/io/Effect" import * as BlockedRequests from "@effect/io/internal/blockedRequests" import { unsafeMakeWith } from "@effect/io/internal/cache" import { isInterruptedOnly } from "@effect/io/internal/cause" import * as core from "@effect/io/internal/core" import { ensuring } from "@effect/io/internal/fiberRuntime" import { Listeners } from "@effect/io/internal/request" import type * as Request from "@effect/io/Request" import type * as RequestResolver from "@effect/io/RequestResolver" type RequestCache = Cache.Cache, never, { listeners: Request.Listeners handle: Deferred }> /** @internal */ export const currentCache = core.fiberRefUnsafeMake(unsafeMakeWith, never, { listeners: Request.Listeners handle: Deferred }>( 65536, () => core.map(core.deferredMake(), (handle) => ({ listeners: new Listeners(), handle })), () => seconds(60) )) /** @internal */ export const currentCacheEnabled = globalValue( Symbol.for("@effect/io/FiberRef/currentCacheEnabled"), () => core.fiberRefUnsafeMake(false) ) /** @internal */ export const fromRequest = < A extends Request.Request, Ds extends | RequestResolver.RequestResolver | Effect.Effect> >( request: A, dataSource: Ds ): Effect.Effect< [Ds] extends [Effect.Effect] ? Effect.Effect.Context : never, Request.Request.Error, Request.Request.Success > => core.flatMap( (core.isEffect(dataSource) ? dataSource : core.succeed(dataSource)) as Effect.Effect< never, never, RequestResolver.RequestResolver >, (ds) => core.fiberIdWith((id) => { const proxy = new Proxy(request, {}) return core.fiberRefGetWith(currentCacheEnabled, (cacheEnabled) => { if (cacheEnabled) { return core.fiberRefGetWith(currentCache, (cache) => core.flatMap(cache.getEither(proxy), (orNew) => { switch (orNew._tag) { case "Left": { orNew.left.listeners.increment() return core.blocked( BlockedRequests.empty, core.flatMap(core.exit(core.deferredAwait(orNew.left.handle)), (exit) => { if (exit._tag === "Failure" && isInterruptedOnly(exit.cause)) { orNew.left.listeners.decrement() return core.flatMap( cache.invalidateWhen( proxy, (entry) => entry.handle === orNew.left.handle ), () => fromRequest(proxy, dataSource) ) } return ensuring( core.deferredAwait(orNew.left.handle), core.sync(() => orNew.left.listeners.decrement()) ) }) ) } case "Right": { orNew.right.listeners.increment() return core.blocked( BlockedRequests.single( ds as RequestResolver.RequestResolver, BlockedRequests.makeEntry({ request: proxy, result: orNew.right.handle, listeners: orNew.right.listeners, ownerId: id, state: { completed: false } }) ), core.uninterruptibleMask((restore) => core.flatMap( core.exit(restore(core.deferredAwait(orNew.right.handle))), (exit) => { orNew.right.listeners.decrement() return exit } ) ) ) } } })) } const listeners = new Listeners() listeners.increment() return core.flatMap( core.deferredMake, Request.Request.Success>(), (ref) => core.blocked( BlockedRequests.single( ds as RequestResolver.RequestResolver, BlockedRequests.makeEntry({ request: proxy, result: ref, listeners, ownerId: id, state: { completed: false } }) ), ensuring( core.deferredAwait(ref), core.sync(() => listeners.decrement()) ) ) ) }) }) ) /** @internal */ export const cacheRequest = >( request: A, result: Request.Request.Result ): Effect.Effect => { return core.fiberRefGetWith(currentCacheEnabled, (cacheEnabled) => { if (cacheEnabled) { return core.fiberRefGetWith(currentCache, (cache) => core.flatMap(cache.getEither(request), (orNew) => { switch (orNew._tag) { case "Left": { return core.unit } case "Right": { return core.deferredComplete(orNew.right.handle, result) } } })) } return core.unit }) } /** @internal */ export const withRequestCaching: { (strategy: boolean): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, strategy: boolean ): Effect.Effect } = dual< ( strategy: boolean ) => (self: Effect.Effect) => Effect.Effect, ( self: Effect.Effect, strategy: boolean ) => Effect.Effect >(2, (self, strategy) => core.fiberRefLocally(self, currentCacheEnabled, strategy)) /** @internal */ export const withRequestCache: { (cache: Request.Cache): (self: Effect.Effect) => Effect.Effect ( self: Effect.Effect, cache: Request.Cache ): Effect.Effect } = dual< ( cache: Request.Cache ) => (self: Effect.Effect) => Effect.Effect, ( self: Effect.Effect, cache: Request.Cache ) => Effect.Effect >( 2, // @ts-expect-error (self, cache) => core.fiberRefLocally(self, currentCache, cache) )