/** * A `Layer` describes how to build one or more services in your * application. Services can be injected into effects via * `Effect.provideService`. Effects can require services via `Effect.service`. * * Layer can be thought of as recipes for producing bundles of services, given * their dependencies (other services). * * Construction of services can be effectful and utilize resources that must be * acquired and safely released when the services are done being utilized. * * By default layers are shared, meaning that if the same layer is used twice * the layer will only be allocated a single time. * * Because of their excellent composition properties, layers are the idiomatic * way in Effect-TS to create services that depend on other services. * * @since 1.0.0 */ import * as Context from "@effect/data/Context" import type { LazyArg } from "@effect/data/Function" import type { Pipeable } from "@effect/data/Pipeable" import type * as Cause from "@effect/io/Cause" import type * as Effect from "@effect/io/Effect" import type { FiberRef } from "@effect/io/FiberRef" import * as internal from "@effect/io/internal/layer" import type * as Runtime from "@effect/io/Runtime" import type * as Schedule from "@effect/io/Schedule" import type * as Scope from "@effect/io/Scope" /** * @since 1.0.0 * @category symbols */ export const LayerTypeId: unique symbol = internal.LayerTypeId /** * @since 1.0.0 * @category symbols */ export type LayerTypeId = typeof LayerTypeId /** * @since 1.0.0 * @category models */ export interface Layer extends Layer.Variance, Pipeable {} /** * @since 1.0.0 */ export declare namespace Layer { /** * @since 1.0.0 * @category models */ export interface Variance { readonly [LayerTypeId]: { readonly _RIn: (_: never) => RIn readonly _E: (_: never) => E readonly _ROut: (_: ROut) => void } } /** * @since 1.0.0 * @category type-level */ export type Context> = [T] extends [Layer] ? _R : never /** * @since 1.0.0 * @category type-level */ export type Error> = [T] extends [Layer] ? _E : never /** * @since 1.0.0 * @category type-level */ export type Success> = [T] extends [Layer] ? _A : never } /** * Returns `true` if the specified value is a `Layer`, `false` otherwise. * * @since 1.0.0 * @category getters */ export const isLayer: (u: unknown) => u is Layer = internal.isLayer /** * Returns `true` if the specified `Layer` is a fresh version that will not be * shared, `false` otherwise. * * @since 1.0.0 * @category getters */ export const isFresh: (self: Layer) => boolean = internal.isFresh /** * Builds a layer into a scoped value. * * @since 1.0.0 * @category destructors */ export const build: ( self: Layer ) => Effect.Effect> = internal.build /** * Builds a layer into an `Effect` value. Any resources associated with this * layer will be released when the specified scope is closed unless their scope * has been extended. This allows building layers where the lifetime of some of * the services output by the layer exceed the lifetime of the effect the * layer is provided to. * * @since 1.0.0 * @category destructors */ export const buildWithScope: { (scope: Scope.Scope): (self: Layer) => Effect.Effect> (self: Layer, scope: Scope.Scope): Effect.Effect> } = internal.buildWithScope /** * Recovers from all errors. * * @since 1.0.0 * @category error handling */ export const catchAll: { (onError: (error: E) => Layer): (self: Layer) => Layer (self: Layer, onError: (error: E) => Layer): Layer } = internal.catchAll /** * Recovers from all errors. * * @since 1.0.0 * @category error handling */ export const catchAllCause: { ( onError: (cause: Cause.Cause) => Layer ): (self: Layer) => Layer ( self: Layer, onError: (cause: Cause.Cause) => Layer ): Layer } = internal.catchAllCause /** * Constructs a `Layer` that passes along the specified context as an * output. * * @since 1.0.0 * @category constructors */ export const context: () => Layer = internal.context /** * Constructs a layer that dies with the specified defect. * * @since 1.0.0 * @category constructors */ export const die: (defect: unknown) => Layer = internal.die /** * Constructs a layer that dies with the specified defect. * * @since 1.0.0 * @category constructors */ export const dieSync: (evaluate: LazyArg) => Layer = internal.dieSync /** * Replaces the layer's output with `void` and includes the layer only for its * side-effects. * * @since 1.0.0 * @category mapping */ export const discard: (self: Layer) => Layer = internal.discard /** * Constructs a layer from the specified effect. * * @since 1.0.0 * @category constructors */ export const effect: { >( tag: T ): (effect: Effect.Effect>) => Layer> , R, E>( tag: T, effect: Effect.Effect> ): Layer> } = internal.fromEffect /** * Constructs a layer from the specified effect discarding it's output. * * @since 1.0.0 * @category constructors */ export const effectDiscard: (effect: Effect.Effect) => Layer = internal.fromEffectDiscard /** * Constructs a layer from the specified effect, which must return one or more * services. * * @since 1.0.0 * @category constructors */ export const effectContext: (effect: Effect.Effect>) => Layer = internal.fromEffectContext /** * Extends the scope of this layer, returning a new layer that when provided * to an effect will not immediately release its associated resources when * that effect completes execution but instead when the scope the resulting * effect depends on is closed. * * @since 1.0.0 * @category utils */ export const extendScope: (self: Layer) => Layer = internal.extendScope /** * Constructs a layer that fails with the specified error. * * @since 1.0.0 * @category constructors */ export const fail: (error: E) => Layer = internal.fail /** * Constructs a layer that fails with the specified error. * * @since 1.0.0 * @category constructors */ export const failSync: (evaluate: LazyArg) => Layer = internal.failSync /** * Constructs a layer that fails with the specified cause. * * @since 1.0.0 * @category constructors */ export const failCause: (cause: Cause.Cause) => Layer = internal.failCause /** * Constructs a layer that fails with the specified cause. * * @since 1.0.0 * @category constructors */ export const failCauseSync: (evaluate: LazyArg>) => Layer = internal.failCauseSync /** * Constructs a layer dynamically based on the output of this layer. * * @since 1.0.0 * @category sequencing */ export const flatMap: { ( f: (context: Context.Context) => Layer ): (self: Layer) => Layer ( self: Layer, f: (context: Context.Context) => Layer ): Layer } = internal.flatMap /** * Flattens layers nested in the context of an effect. * * @since 1.0.0 * @category sequencing */ export const flatten: { (tag: Context.Tag>): (self: Layer) => Layer (self: Layer, tag: Context.Tag>): Layer } = internal.flatten /** * Creates a fresh version of this layer that will not be shared. * * @since 1.0.0 * @category utils */ export const fresh: (self: Layer) => Layer = internal.fresh const fromFunction: , B extends Context.Tag>( tagA: A, tagB: B, f: (a: Context.Tag.Service) => Context.Tag.Service ) => Layer, never, Context.Tag.Identifier> = internal.fromFunction export { /** * Constructs a layer from the context using the specified function. * * @since 1.0.0 * @category constructors */ fromFunction as function } /** * Builds this layer and uses it until it is interrupted. This is useful when * your entire application is a layer, such as an HTTP server. * * @since 1.0.0 * @category conversions */ export const launch: (self: Layer) => Effect.Effect = internal.launch /** * Returns a new layer whose output is mapped by the specified function. * * @since 1.0.0 * @category mapping */ export const map: { (f: (context: Context.Context) => Context.Context): (self: Layer) => Layer (self: Layer, f: (context: Context.Context) => Context.Context): Layer } = internal.map /** * Returns a layer with its error channel mapped using the specified function. * * @since 1.0.0 * @category mapping */ export const mapError: { (f: (error: E) => E2): (self: Layer) => Layer (self: Layer, f: (error: E) => E2): Layer } = internal.mapError /** * Feeds the error or output services of this layer into the input of either * the specified `failure` or `success` layers, resulting in a new layer with * the inputs of this layer, and the error or outputs of the specified layer. * * @since 1.0.0 * @category folding */ export const match: { ( options: { readonly onFailure: (error: E) => Layer readonly onSuccess: (context: Context.Context) => Layer } ): (self: Layer) => Layer ( self: Layer, options: { readonly onFailure: (error: E) => Layer readonly onSuccess: (context: Context.Context) => Layer } ): Layer } = internal.match /** * Feeds the error or output services of this layer into the input of either * the specified `failure` or `success` layers, resulting in a new layer with * the inputs of this layer, and the error or outputs of the specified layer. * * @since 1.0.0 * @category folding */ export const matchCause: { ( options: { readonly onFailure: (cause: Cause.Cause) => Layer readonly onSuccess: (context: Context.Context) => Layer } ): (self: Layer) => Layer ( self: Layer, options: { readonly onFailure: (cause: Cause.Cause) => Layer readonly onSuccess: (context: Context.Context) => Layer } ): Layer } = internal.matchCause /** * Returns a scoped effect that, if evaluated, will return the lazily computed * result of this layer. * * @since 1.0.0 * @category utils */ export const memoize: ( self: Layer ) => Effect.Effect> = internal.memoize /** * Combines this layer with the specified layer, producing a new layer that * has the inputs and outputs of both. * * @since 1.0.0 * @category utils */ export const merge: { ( that: Layer ): (self: Layer) => Layer ( self: Layer, that: Layer ): Layer } = internal.merge /** * Merges all the layers together in parallel. * * @since 1.0.0 * @category zipping */ export const mergeAll: , ...Array>]>( ...layers: Layers ) => Layer< { [k in keyof Layers]: Layer.Context }[number], { [k in keyof Layers]: Layer.Error }[number], { [k in keyof Layers]: Layer.Success }[number] > = internal.mergeAll /** * Translates effect failure into death of the fiber, making all failures * unchecked and not a part of the type of the layer. * * @since 1.0.0 * @category error handling */ export const orDie: (self: Layer) => Layer = internal.orDie /** * Executes this layer and returns its output, if it succeeds, but otherwise * executes the specified layer. * * @since 1.0.0 * @category error handling */ export const orElse: { (that: LazyArg>): (self: Layer) => Layer (self: Layer, that: LazyArg>): Layer } = internal.orElse /** * Returns a new layer that produces the outputs of this layer but also * passes through the inputs. * * @since 1.0.0 * @category utils */ export const passthrough: (self: Layer) => Layer = internal.passthrough /** * Projects out part of one of the services output by this layer using the * specified function. * * @since 1.0.0 * @category utils */ export const project: { , B extends Context.Tag>( tagA: A, tagB: B, f: (a: Context.Tag.Service) => Context.Tag.Service ): (self: Layer>) => Layer> , B extends Context.Tag>( self: Layer>, tagA: A, tagB: B, f: (a: Context.Tag.Service) => Context.Tag.Service ): Layer> } = internal.project /** * Feeds the output services of this builder into the input of the specified * builder, resulting in a new builder with the inputs of this builder as * well as any leftover inputs, and the outputs of the specified builder. * * @since 1.0.0 * @category utils */ export const provide: { ( that: Layer ): (self: Layer) => Layer, E2 | E, ROut2> ( self: Layer, that: Layer ): Layer, E | E2, ROut2> } = internal.provide /** * @since 1.0.0 * @category utils */ export const locallyEffect: { ( f: (_: Effect.Effect>) => Effect.Effect> ): (self: Layer) => Layer ( self: Layer, f: (_: Effect.Effect>) => Effect.Effect> ): Layer } = internal.locallyEffect /** * @since 1.0.0 * @category utils */ export const locally: { ( ref: FiberRef, value: X ): (self: Layer) => Layer ( self: Layer, ref: FiberRef, value: X ): Layer } = internal.fiberRefLocally /** * @since 1.0.0 * @category utils */ export const locallyWith: { (ref: FiberRef, value: (_: X) => X): (self: Layer) => Layer (self: Layer, ref: FiberRef, value: (_: X) => X): Layer } = internal.fiberRefLocallyWith /** * @since 1.0.0 * @category utils */ export const locallyScoped: (self: FiberRef, value: A) => Layer = internal.fiberRefLocallyScoped /** * @since 1.0.0 * @category utils */ export const fiberRefLocallyScopedWith: (self: FiberRef, value: (_: A) => A) => Layer = internal.fiberRefLocallyScopedWith /** * Feeds the output services of this layer into the input of the specified * layer, resulting in a new layer with the inputs of this layer, and the * outputs of both layers. * * @since 1.0.0 * @category utils */ export const provideMerge: { ( that: Layer ): (self: Layer) => Layer, E2 | E, ROut2 | ROut> ( self: Layer, that: Layer ): Layer, E | E2, ROut | ROut2> } = internal.provideMerge /** * Retries constructing this layer according to the specified schedule. * * @since 1.0.0 * @category retrying */ export const retry: { ( schedule: Schedule.Schedule ): (self: Layer) => Layer ( self: Layer, schedule: Schedule.Schedule ): Layer } = internal.retry /** * A layer that constructs a scope and closes it when the workflow the layer * is provided to completes execution, whether by success, failure, or * interruption. This can be used to close a scope when providing a layer to a * workflow. * * @since 1.0.0 * @category constructors */ export const scope: Layer = internal.scope /** * Constructs a layer from the specified scoped effect. * * @since 1.0.0 * @category constructors */ export const scoped: { >( tag: T ): ( effect: Effect.Effect> ) => Layer, E, Context.Tag.Identifier> , R, E>( tag: T, effect: Effect.Effect> ): Layer, E, Context.Tag.Identifier> } = internal.scoped /** * Constructs a layer from the specified scoped effect. * * @since 1.0.0 * @category constructors */ export const scopedDiscard: (effect: Effect.Effect) => Layer, E, never> = internal.scopedDiscard /** * Constructs a layer from the specified scoped effect, which must return one * or more services. * * @since 1.0.0 * @category constructors */ export const scopedContext: ( effect: Effect.Effect> ) => Layer, E, A> = internal.scopedContext /** * Constructs a layer that accesses and returns the specified service from the * context. * * @since 1.0.0 * @category constructors */ export const service: >( tag: T ) => Layer, never, Context.Tag.Identifier> = internal.service /** * Constructs a layer from the specified value. * * @since 1.0.0 * @category constructors */ export const succeed: { >( tag: T ): (resource: Context.Tag.Service) => Layer> >( tag: T, resource: Context.Tag.Service ): Layer> } = internal.succeed /** * Constructs a layer from the specified value, which must return one or more * services. * * @since 1.0.0 * @category constructors */ export const succeedContext: (context: Context.Context) => Layer = internal.succeedContext /** * Lazily constructs a layer. This is useful to avoid infinite recursion when * creating layers that refer to themselves. * * @since 1.0.0 * @category constructors */ export const suspend: (evaluate: LazyArg>) => Layer = internal.suspend /** * Lazily constructs a layer from the specified value. * * @since 1.0.0 * @category constructors */ export const sync: { >( tag: T ): (evaluate: LazyArg>) => Layer> >( tag: T, evaluate: LazyArg> ): Layer> } = internal.sync /** * Lazily constructs a layer from the specified value, which must return one or more * services. * * @since 1.0.0 * @category constructors */ export const syncContext: (evaluate: LazyArg>) => Layer = internal.syncContext /** * Performs the specified effect if this layer succeeds. * * @since 1.0.0 * @category sequencing */ export const tap: { ( f: (context: Context.Context) => Effect.Effect ): (self: Layer) => Layer ( self: Layer, f: (context: Context.Context) => Effect.Effect ): Layer } = internal.tap /** * Performs the specified effect if this layer fails. * * @since 1.0.0 * @category sequencing */ export const tapError: { ( f: (e: XE) => Effect.Effect ): (self: Layer) => Layer ( self: Layer, f: (e: XE) => Effect.Effect ): Layer } = internal.tapError /** * Performs the specified effect if this layer fails. * * @since 1.0.0 * @category sequencing */ export const tapErrorCause: { ( f: (cause: Cause.Cause) => Effect.Effect ): (self: Layer) => Layer ( self: Layer, f: (cause: Cause.Cause) => Effect.Effect ): Layer } = internal.tapErrorCause /** * Converts a layer that requires no services into a scoped runtime, which can * be used to execute effects. * * @since 1.0.0 * @category conversions */ export const toRuntime: ( self: Layer ) => Effect.Effect> = internal.toRuntime /** * Feeds the output services of this builder into the input of the specified * builder, resulting in a new builder with the inputs of this builder as * well as any leftover inputs, and the outputs of the specified builder. * * @since 1.0.0 * @category utils */ export const use: { ( self: Layer ): (that: Layer) => Layer, E | E2, ROut2> ( that: Layer, self: Layer ): Layer, E2 | E, ROut2> } = internal.use /** * Feeds the output services of this layer into the input of the specified * layer, resulting in a new layer with the inputs of this layer, and the * outputs of both layers. * * @since 1.0.0 * @category utils */ export const useMerge: { ( self: Layer ): (that: Layer) => Layer, E | E2, ROut | ROut2> ( that: Layer, self: Layer ): Layer, E2 | E, ROut2 | ROut> } = internal.useMerge /** * Combines this layer the specified layer, producing a new layer that has the * inputs of both, and the outputs of both combined using the specified * function. * * @since 1.0.0 * @category zipping */ export const zipWithPar: { ( that: Layer, f: (a: Context.Context, b: Context.Context) => Context.Context ): (self: Layer) => Layer ( self: Layer, that: Layer, f: (a: Context.Context, b: Context.Context) => Context.Context ): Layer } = internal.zipWithPar /** * @since 1.0.0 * @category utils */ export const unwrapEffect = ( self: Effect.Effect> ): Layer => { const tag = Context.Tag>() return internal.flatMap(internal.fromEffect(tag, self), (context) => Context.get(context, tag)) } /** * @since 1.0.0 * @category utils */ export const unwrapScoped = ( self: Effect.Effect> ): Layer, E | E1, A> => { const tag = Context.Tag>() return internal.flatMap(internal.scoped(tag, self), (context) => Context.get(context, tag)) }