/** * A service whose code loads on first use. `Resource.lazy` is an entry of * the runtime's `resources` Layer that holds no service yet, only how to get * one: the dynamic import that is its own chunk, and the Layer that chunk * exports. The first Command that asks with `Resource.get` waits for the * import and the build, once; every later one shares what was built, until * the runtime's scope closes. */ import { Context, Data, Deferred, Effect, Layer, Option, Scope } from 'effect' declare const LazyTypeId: unique symbol /** What a Command that reads `Identifier`'s service through `Resource.get` requires. */ export interface Lazy { readonly [LazyTypeId]: Identifier } /** The import of a lazy resource's chunk failed. A later `get` tries again. */ export class LoadError extends Data.TaggedError('ResourceLoadError')<{ /** The service's key. */ readonly resource: string readonly cause: unknown }> {} interface Loader { /** The load in flight or done, started first when there is none (before the first, after a failed one). */ readonly start: Effect.Effect> } // The string key is a service's identity, so a loader made per call is the same service. const loaderOf = ( tag: Context.Key, ): Context.Service, Loader> => Context.Service, Loader>(`foldkit-bundle/Resource/${tag.key}`) /** * `tag`'s service, loaded on first use: `load` imports the chunk and returns * the Layer that builds the service. The Layer is built into this entry's * scope (the runtime's), with whatever else `resources` gives it (`RIn`). * * The Layer must not fail: a failure it can recover from is the service's * to handle, and one it cannot is a defect (`Layer.orDie`), which reaches * the Command as one. A test passes a fake the same way: * `Resource.lazy(Tag, async () => Layer.succeed(Tag, fake))`. */ export const lazy = ( tag: Context.Key, load: () => Promise>, ): Layer.Layer, never, RIn> => Layer.effect( loaderOf(tag), Effect.gen(function* () { const scope = yield* Effect.scope const context = yield* Effect.context() let loading = Option.none>() const build = Effect.tryPromise({ try: load, catch: cause => new LoadError({ resource: tag.key, cause }), }).pipe( Effect.flatMap(layer => Layer.buildWithScope(layer, scope)), Effect.map(built => Context.get(built, tag)), Effect.provideContext(context), ) // Check and claim in one synchronous step, so two Commands asking at // once share one load. The load runs in the runtime's scope, not the // asking Command's, so interrupting that Command leaves it running for // the others. const start = Effect.suspend(() => Option.match(loading, { onSome: Effect.succeed, onNone: () => { const deferred = Deferred.makeUnsafe() loading = Option.some(deferred) const load = build.pipe( // Forgotten before the waiters wake, so a retry starts a new import. Effect.onError(() => Effect.sync(() => { loading = Option.none() }), ), Deferred.into(deferred), Effect.forkIn(scope), ) // A load forked into a scope already closed never runs, and would // leave its waiters waiting for good. A closed scope runs this // finalizer at once; an open one interrupts nothing that settled. return Scope.addFinalizer(scope, Deferred.interrupt(deferred)).pipe( Effect.andThen(load), Effect.as(deferred), ) }, }), ) return { start } }), ) /** * `tag`'s service, loading its chunk first if no Command has yet. Fails with * `LoadError` when the import does; a Layer that dies dies here too. */ export const get = ( tag: Context.Key, ): Effect.Effect> => Effect.flatMap(loaderOf(tag), loader => Effect.flatMap(loader.start, Deferred.await)) /** * Starts loading `tag`'s chunk and returns at once, so the `get` after it * waits less or not at all. Starting it again, or after it has loaded, does * nothing; a failed one is forgotten, and the next `get` tries again. */ export const prefetch = ( tag: Context.Key, ): Effect.Effect> => Effect.flatMap(loaderOf(tag), loader => Effect.asVoid(loader.start))