import * as Context from "@effect/data/Context" import * as Duration from "@effect/data/Duration" import type { LazyArg } from "@effect/data/Function" import { dual, pipe } from "@effect/data/Function" import { pipeArguments } from "@effect/data/Pipeable" import * as Cause from "@effect/io/Cause" import * as Clock from "@effect/io/Clock" import type * as Effect from "@effect/io/Effect" import type * as Exit from "@effect/io/Exit" import type { FiberRef } from "@effect/io/FiberRef" import * as FiberRefsPatch from "@effect/io/FiberRefsPatch" import * as core from "@effect/io/internal/core" import * as effect from "@effect/io/internal/effect" import * as circular from "@effect/io/internal/effect/circular" import * as fiberRuntime from "@effect/io/internal/fiberRuntime" import * as EffectOpCodes from "@effect/io/internal/opCodes/effect" import * as OpCodes from "@effect/io/internal/opCodes/layer" import * as ref from "@effect/io/internal/ref" import * as runtime from "@effect/io/internal/runtime" import * as runtimeFlags from "@effect/io/internal/runtimeFlags" import * as synchronized from "@effect/io/internal/synchronizedRef" import type * as Layer from "@effect/io/Layer" import type * as Runtime from "@effect/io/Runtime" import type * as Schedule from "@effect/io/Schedule" import * as ScheduleDecision from "@effect/io/ScheduleDecision" import * as Intervals from "@effect/io/ScheduleIntervals" import * as Scope from "@effect/io/Scope" import type * as Synchronized from "@effect/io/SynchronizedRef" /** @internal */ const LayerSymbolKey = "@effect/io/Layer" /** @internal */ export const LayerTypeId: Layer.LayerTypeId = Symbol.for( LayerSymbolKey ) as Layer.LayerTypeId /** @internal */ const layerVariance = { _RIn: (_: never) => _, _E: (_: never) => _, _ROut: (_: unknown) => _ } /** @internal */ const proto = { [LayerTypeId]: layerVariance, pipe() { return pipeArguments(this, arguments) } } /** @internal */ export type Primitive = | ExtendScope | Fold | Fresh | FromEffect | Scoped | Suspend | Locally | ProvideTo | ZipWith | ZipWithPar /** @internal */ export type Op = Layer.Layer & Body & { readonly _tag: Tag } /** @internal */ export interface ExtendScope extends Op }> {} /** @internal */ export interface Fold extends Op readonly failureK: (cause: Cause.Cause) => Layer.Layer readonly successK: (context: Context.Context) => Layer.Layer }> {} /** @internal */ export interface Fresh extends Op }> {} /** @internal */ export interface FromEffect extends Op> }> {} /** @internal */ export interface Scoped extends Op> }> {} /** @internal */ export interface Suspend extends Op> }> {} /** @internal */ export interface Locally extends Op<"Locally", { readonly self: Layer.Layer readonly f: (_: Effect.Effect) => Effect.Effect }> {} /** @internal */ export interface ProvideTo extends Op readonly second: Layer.Layer }> {} /** @internal */ export interface ZipWith extends Op readonly second: Layer.Layer readonly zipK: ( left: Context.Context, right: Context.Context ) => Context.Context }> {} /** @internal */ export interface ZipWithPar extends Op readonly second: Layer.Layer readonly zipK: ( left: Context.Context, right: Context.Context ) => Context.Context }> {} /** @internal */ export const isLayer = (u: unknown): u is Layer.Layer => { return typeof u === "object" && u != null && LayerTypeId in u } /** @internal */ export const isFresh = (self: Layer.Layer): boolean => { return (self as Primitive)._tag === OpCodes.OP_FRESH } // ----------------------------------------------------------------------------- // MemoMap // ----------------------------------------------------------------------------- /** @internal */ class MemoMap { constructor( readonly ref: Synchronized.SynchronizedRef< Map< Layer.Layer, readonly [Effect.Effect, Scope.Scope.Finalizer] > > ) { } /** * Checks the memo map to see if a layer exists. If it is, immediately * returns it. Otherwise, obtains the layer, stores it in the memo map, * and adds a finalizer to the `Scope`. */ getOrElseMemoize( layer: Layer.Layer, scope: Scope.Scope ): Effect.Effect> { return pipe( synchronized.modifyEffect(this.ref, (map) => { const inMap = map.get(layer) if (inMap !== undefined) { const [acquire, release] = inMap const cached: Effect.Effect> = pipe( acquire as Effect.Effect]>, core.flatMap(([patch, b]) => pipe(effect.patchFiberRefs(patch), core.as(b))), core.onExit(core.exitMatch({ onFailure: () => core.unit, onSuccess: () => core.scopeAddFinalizerExit(scope, release) })) ) return core.succeed([cached, map] as const) } return pipe( ref.make(0), core.flatMap((observers) => pipe( core.deferredMake]>(), core.flatMap((deferred) => pipe( ref.make(() => core.unit), core.map((finalizerRef) => { const resource = core.uninterruptibleMask((restore) => pipe( fiberRuntime.scopeMake(), core.flatMap((innerScope) => pipe( restore(core.flatMap( withScope(layer, innerScope), (f) => effect.diffFiberRefs(f(this)) )), core.exit, core.flatMap((exit) => { switch (exit._tag) { case EffectOpCodes.OP_FAILURE: { return pipe( core.deferredFailCause(deferred, exit.i0), core.zipRight(core.scopeClose(innerScope, exit)), core.zipRight(core.failCause(exit.i0)) ) } case EffectOpCodes.OP_SUCCESS: { return pipe( ref.set(finalizerRef, (exit) => pipe( core.scopeClose(innerScope, exit), core.whenEffect( ref.modify(observers, (n) => [n === 1, n - 1] as const) ), core.asUnit )), core.zipRight(ref.update(observers, (n) => n + 1)), core.zipRight( core.scopeAddFinalizerExit(scope, (exit) => pipe( ref.get(finalizerRef), core.flatMap((finalizer) => finalizer(exit)) )) ), core.zipRight(core.deferredSucceed(deferred, exit.i0)), core.as(exit.i0[1]) ) } } }) ) ) ) ) const memoized = [ pipe( core.deferredAwait(deferred), core.onExit(core.exitMatchEffect({ onFailure: () => core.unit, onSuccess: () => ref.update(observers, (n) => n + 1) })) ), (exit: Exit.Exit) => pipe( ref.get(finalizerRef), core.flatMap((finalizer) => finalizer(exit)) ) ] as const return [ resource, isFresh(layer) ? map : map.set(layer, memoized) ] as const }) ) ) ) ) ) }), core.flatten ) } } const makeMemoMap = (): Effect.Effect => core.map( circular.makeSynchronized< Map< Layer.Layer, readonly [ Effect.Effect, Scope.Scope.Finalizer ] > >(new Map()), (ref) => new MemoMap(ref) ) /** @internal */ export const build = ( self: Layer.Layer ): Effect.Effect> => fiberRuntime.scopeWith((scope) => buildWithScope(self, scope)) /** @internal */ export const buildWithScope = dual< ( scope: Scope.Scope ) => (self: Layer.Layer) => Effect.Effect>, ( self: Layer.Layer, scope: Scope.Scope ) => Effect.Effect> >(2, (self, scope) => core.flatMap( makeMemoMap(), (memoMap) => core.flatMap(withScope(self, scope), (run) => run(memoMap)) )) const withScope = ( self: Layer.Layer, scope: Scope.Scope ): Effect.Effect Effect.Effect>> => { const op = self as Primitive switch (op._tag) { case "Locally": { return core.sync(() => (memoMap: MemoMap) => op.f(memoMap.getOrElseMemoize(op.self, scope))) } case "ExtendScope": { return core.sync(() => (memoMap: MemoMap) => fiberRuntime.scopeWith( (scope) => memoMap.getOrElseMemoize(op.layer, scope) ) as unknown as Effect.Effect> ) } case "Fold": { return core.sync(() => (memoMap: MemoMap) => pipe( memoMap.getOrElseMemoize(op.layer, scope), core.matchCauseEffect({ onFailure: (cause) => memoMap.getOrElseMemoize(op.failureK(cause), scope), onSuccess: (value) => memoMap.getOrElseMemoize(op.successK(value), scope) }) ) ) } case "Fresh": { return core.sync(() => (_: MemoMap) => pipe(op.layer, buildWithScope(scope))) } case "FromEffect": { return core.sync(() => (_: MemoMap) => op.effect as Effect.Effect>) } case "ProvideTo": { return core.sync(() => (memoMap: MemoMap) => pipe( memoMap.getOrElseMemoize(op.first, scope), core.flatMap((env) => pipe( memoMap.getOrElseMemoize(op.second, scope), core.provideContext(env) ) ) ) ) } case "Scoped": { return core.sync(() => (_: MemoMap) => fiberRuntime.scopeExtend( op.effect as Effect.Effect>, scope ) ) } case "Suspend": { return core.sync(() => (memoMap: MemoMap) => memoMap.getOrElseMemoize( op.evaluate(), scope ) ) } case "ZipWith": { return core.sync(() => (memoMap: MemoMap) => pipe( memoMap.getOrElseMemoize(op.first, scope), core.zipWith( memoMap.getOrElseMemoize(op.second, scope), op.zipK ) ) ) } case "ZipWithPar": { return core.sync(() => (memoMap: MemoMap) => pipe( memoMap.getOrElseMemoize(op.first, scope), fiberRuntime.zipWithOptions( memoMap.getOrElseMemoize(op.second, scope), op.zipK, { concurrent: true } ) ) ) } } } // ----------------------------------------------------------------------------- // Layer // ----------------------------------------------------------------------------- /** @internal */ export const catchAll = dual< ( onError: (error: E) => Layer.Layer ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, onError: (error: E) => Layer.Layer ) => Layer.Layer >(2, (self, onFailure) => match(self, { onFailure, onSuccess: succeedContext })) /** @internal */ export const catchAllCause = dual< ( onError: (cause: Cause.Cause) => Layer.Layer ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, onError: (cause: Cause.Cause) => Layer.Layer ) => Layer.Layer >(2, (self, onFailure) => matchCause(self, { onFailure, onSuccess: succeedContext })) /** @internal */ export const die = (defect: unknown): Layer.Layer => failCause(Cause.die(defect)) /** @internal */ export const dieSync = (evaluate: LazyArg): Layer.Layer => failCauseSync(() => Cause.die(evaluate())) /** @internal */ export const discard = (self: Layer.Layer): Layer.Layer => map(self, () => Context.empty()) /** @internal */ export const context = (): Layer.Layer => fromEffectContext(core.context()) /** @internal */ export const extendScope = ( self: Layer.Layer ): Layer.Layer => { const extendScope = Object.create(proto) extendScope._tag = OpCodes.OP_EXTEND_SCOPE extendScope.layer = self return extendScope } /** @internal */ export const fail = (error: E): Layer.Layer => failCause(Cause.fail(error)) /** @internal */ export const failSync = (evaluate: LazyArg): Layer.Layer => failCauseSync(() => Cause.fail(evaluate())) /** @internal */ export const failCause = (cause: Cause.Cause): Layer.Layer => fromEffectContext(core.failCause(cause)) /** @internal */ export const failCauseSync = (evaluate: LazyArg>): Layer.Layer => fromEffectContext(core.failCauseSync(evaluate)) /** @internal */ export const flatMap = dual< ( f: (context: Context.Context) => Layer.Layer ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, f: (context: Context.Context) => Layer.Layer ) => Layer.Layer >(2, (self, f) => match(self, { onFailure: fail, onSuccess: f })) /** @internal */ export const flatten = dual< ( tag: Context.Tag> ) => ( self: Layer.Layer ) => Layer.Layer, ( self: Layer.Layer, tag: Context.Tag> ) => Layer.Layer >(2, (self, tag) => flatMap(self, Context.get(tag as any) as any)) /** @internal */ export const fresh = (self: Layer.Layer): Layer.Layer => { const fresh = Object.create(proto) fresh._tag = OpCodes.OP_FRESH fresh.layer = self return fresh } /** @internal */ export const fromEffect = dual< >( tag: T ) => ( effect: Effect.Effect> ) => Layer.Layer>, , R, E>( tag: T, effect: Effect.Effect> ) => Layer.Layer> >(2, (a, b) => { const tagFirst = Context.isTag(a) const tag = (tagFirst ? a : b) as Context.Tag const effect = tagFirst ? b : a return fromEffectContext(core.map(effect, (service) => Context.make(tag, service))) }) /** @internal */ export const fromEffectDiscard = (effect: Effect.Effect) => fromEffectContext(core.map(effect, () => Context.empty())) /** @internal */ export function fromEffectContext( effect: Effect.Effect> ): Layer.Layer { const fromEffect = Object.create(proto) fromEffect._tag = OpCodes.OP_FROM_EFFECT fromEffect.effect = effect return fromEffect } /** @internal */ export const fiberRefLocally = dual< (ref: FiberRef, value: X) => (self: Layer.Layer) => Layer.Layer, (self: Layer.Layer, ref: FiberRef, value: X) => Layer.Layer >(3, (self, ref, value) => locallyEffect(self, core.fiberRefLocally(ref, value))) /** @internal */ export const locallyEffect = dual< ( f: (_: Effect.Effect>) => Effect.Effect> ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, f: (_: Effect.Effect>) => Effect.Effect> ) => Layer.Layer >(2, (self, f) => { const locally = Object.create(proto) locally._tag = "Locally" locally.self = self locally.f = f return locally }) /** @internal */ export const fiberRefLocallyWith = dual< (ref: FiberRef, value: (_: X) => X) => (self: Layer.Layer) => Layer.Layer, (self: Layer.Layer, ref: FiberRef, value: (_: X) => X) => Layer.Layer >(3, (self, ref, value) => locallyEffect(self, core.fiberRefLocallyWith(ref, value))) /** @internal */ export const fiberRefLocallyScoped = (self: FiberRef, value: A): Layer.Layer => scopedDiscard(fiberRuntime.fiberRefLocallyScoped(self, value)) /** @internal */ export const fiberRefLocallyScopedWith = (self: FiberRef, value: (_: A) => A): Layer.Layer => scopedDiscard(fiberRuntime.fiberRefLocallyScopedWith(self, value)) /** @internal */ export const fromFunction = , B extends Context.Tag>( tagA: A, tagB: B, f: (a: Context.Tag.Service) => Context.Tag.Service ): Layer.Layer, never, Context.Tag.Identifier> => fromEffectContext(core.map(tagA, (a) => Context.make(tagB, f(a)))) /** @internal */ export const launch = (self: Layer.Layer): Effect.Effect => fiberRuntime.scopedEffect( core.zipRight( fiberRuntime.scopeWith((scope) => pipe(self, buildWithScope(scope))), core.never ) ) /** @internal */ export const map = dual< ( f: (context: Context.Context) => Context.Context ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, f: (context: Context.Context) => Context.Context ) => Layer.Layer >(2, (self, f) => flatMap(self, (context) => succeedContext(f(context)))) /** @internal */ export const mapError = dual< (f: (error: E) => E2) => (self: Layer.Layer) => Layer.Layer, (self: Layer.Layer, f: (error: E) => E2) => Layer.Layer >(2, (self, f) => catchAll(self, (error) => failSync(() => f(error)))) /** @internal */ export const matchCause = dual< ( options: { readonly onFailure: (cause: Cause.Cause) => Layer.Layer readonly onSuccess: (context: Context.Context) => Layer.Layer } ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, options: { readonly onFailure: (cause: Cause.Cause) => Layer.Layer readonly onSuccess: (context: Context.Context) => Layer.Layer } ) => Layer.Layer >(2, (self, { onFailure, onSuccess }) => { const fold = Object.create(proto) fold._tag = OpCodes.OP_FOLD fold.layer = self fold.failureK = onFailure fold.successK = onSuccess return fold }) /** @internal */ export const match = dual< ( options: { readonly onFailure: (error: E) => Layer.Layer readonly onSuccess: (context: Context.Context) => Layer.Layer } ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, options: { readonly onFailure: (error: E) => Layer.Layer readonly onSuccess: (context: Context.Context) => Layer.Layer } ) => Layer.Layer >(2, (self, { onFailure, onSuccess }) => matchCause(self, { onFailure: (cause) => { const failureOrCause = Cause.failureOrCause(cause) switch (failureOrCause._tag) { case "Left": { return onFailure(failureOrCause.left) } case "Right": { return failCause(failureOrCause.right) } } }, onSuccess })) /** @internal */ export const memoize = ( self: Layer.Layer ): Effect.Effect> => fiberRuntime.scopeWith((scope) => core.map( effect.memoize(buildWithScope(self, scope)), fromEffectContext ) ) /** @internal */ export const merge = dual< ( that: Layer.Layer ) => (self: Layer.Layer) => Layer.Layer< RIn | RIn2, E | E2, ROut | ROut2 >, (self: Layer.Layer, that: Layer.Layer) => Layer.Layer< RIn | RIn2, E | E2, ROut | ROut2 > >(2, (self, that) => zipWithPar(self, that, (a, b) => pipe(a, Context.merge(b)))) /** @internal */ export const mergeAll = , ...Array>]>( ...layers: Layers ): Layer.Layer< { [k in keyof Layers]: Layer.Layer.Context }[number], { [k in keyof Layers]: Layer.Layer.Error }[number], { [k in keyof Layers]: Layer.Layer.Success }[number] > => { let final = layers[0] for (let i = 1; i < layers.length; i++) { final = merge(layers[i])(final) } return final as any } /** @internal */ export const orDie = (self: Layer.Layer): Layer.Layer => catchAll(self, (defect) => die(defect)) /** @internal */ export const orElse = dual< ( that: LazyArg> ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, that: LazyArg> ) => Layer.Layer >(2, (self, that) => catchAll(self, that)) /** @internal */ export const passthrough = (self: Layer.Layer): Layer.Layer => merge(context(), self) /** @internal */ export const project = dual< , B extends Context.Tag>( tagA: A, tagB: B, f: (a: Context.Tag.Service) => Context.Tag.Service ) => (self: Layer.Layer>) => Layer.Layer>, , B extends Context.Tag>( self: Layer.Layer>, tagA: A, tagB: B, f: (a: Context.Tag.Service) => Context.Tag.Service ) => Layer.Layer> >(4, (self, tagA, tagB, f) => map(self, (context) => Context.make(tagB, f(Context.unsafeGet(context, tagA))))) /** @internal */ export const provide = dual< ( that: Layer.Layer ) => ( self: Layer.Layer ) => Layer.Layer, E | E2, ROut2>, ( self: Layer.Layer, that: Layer.Layer ) => Layer.Layer, E | E2, ROut2> >(2, ( self: Layer.Layer, that: Layer.Layer ) => suspend(() => { const provideTo = Object.create(proto) provideTo._tag = OpCodes.OP_PROVIDE_TO provideTo.first = Object.create(proto, { _tag: { value: OpCodes.OP_ZIP_WITH, enumerable: true }, first: { value: context>(), enumerable: true }, second: { value: self }, zipK: { value: (a: Context.Context, b: Context.Context) => Context.merge(a, b) } }) provideTo.second = that return provideTo })) /** @internal */ export const provideMerge = dual< (that: Layer.Layer) => ( self: Layer.Layer ) => Layer.Layer, E2 | E, ROut | ROut2>, ( self: Layer.Layer, that: Layer.Layer ) => Layer.Layer, E2 | E, ROut | ROut2> >(2, ( self: Layer.Layer, that: Layer.Layer ) => { const zipWith = Object.create(proto) zipWith._tag = OpCodes.OP_ZIP_WITH zipWith.first = self zipWith.second = pipe(self, provide(that)) zipWith.zipK = (a: Context.Context, b: Context.Context): Context.Context => Context.merge(a, b) return zipWith }) /** @internal */ export const retry = dual< ( schedule: Schedule.Schedule ) => ( self: Layer.Layer ) => Layer.Layer, ( self: Layer.Layer, schedule: Schedule.Schedule ) => Layer.Layer >(2, (self, schedule) => suspend(() => { const stateTag = Context.Tag<{ state: unknown }>() return pipe( succeed(stateTag, { state: schedule.initial }), flatMap((env: Context.Context<{ state: unknown }>) => retryLoop(self, schedule, stateTag, pipe(env, Context.get(stateTag)).state) ) ) })) /** @internal */ const retryLoop = ( self: Layer.Layer, schedule: Schedule.Schedule, stateTag: Context.Tag<{ state: unknown }, { state: unknown }>, state: unknown ): Layer.Layer => { return pipe( self, catchAll((error) => pipe( retryUpdate(schedule, stateTag, error, state), flatMap((env) => fresh(retryLoop(self, schedule, stateTag, pipe(env, Context.get(stateTag)).state))) ) ) ) } /** @internal */ const retryUpdate = ( schedule: Schedule.Schedule, stateTag: Context.Tag<{ state: unknown }, { state: unknown }>, error: E, state: unknown ): Layer.Layer => { return fromEffect( stateTag, pipe( Clock.currentTimeMillis, core.flatMap((now) => pipe( schedule.step(now, error, state), core.flatMap(([state, _, decision]) => ScheduleDecision.isDone(decision) ? core.fail(error) : pipe( Clock.sleep(Duration.millis(Intervals.start(decision.intervals) - now)), core.as({ state }) ) ) ) ) ) ) } /** @internal */ export const scoped = dual< >( tag: T ) => ( effect: Effect.Effect> ) => Layer.Layer, E, Context.Tag.Identifier>, , R, E>( tag: T, effect: Effect.Effect> ) => Layer.Layer, E, Context.Tag.Identifier> >(2, (a, b) => { const tagFirst = Context.isTag(a) const tag = (tagFirst ? a : b) as Context.Tag const effect = tagFirst ? b : a return scopedContext(core.map(effect, (service) => Context.make(tag, service))) }) /** @internal */ export const scopedDiscard = ( effect: Effect.Effect ): Layer.Layer, E, never> => { return scopedContext(pipe(effect, core.as(Context.empty()))) } /** @internal */ export const scopedContext = ( effect: Effect.Effect> ): Layer.Layer, E, A> => { const scoped = Object.create(proto) scoped._tag = OpCodes.OP_SCOPED scoped.effect = effect return scoped } /** @internal */ export const scope: Layer.Layer = scopedContext( core.map( fiberRuntime.acquireRelease( fiberRuntime.scopeMake(), (scope, exit) => scope.close(exit) ), (scope) => Context.make(Scope.Scope, scope) ) ) /** @internal */ export const service = >( tag: T ): Layer.Layer, never, Context.Tag.Identifier> => { return fromEffect(tag, tag) } /** @internal */ export const succeed = dual< >( tag: T ) => ( resource: Context.Tag.Service ) => Layer.Layer>, >( tag: T, resource: Context.Tag.Service ) => Layer.Layer> >(2, (a, b) => { const tagFirst = Context.isTag(a) const tag = (tagFirst ? a : b) as Context.Tag const resource = tagFirst ? b : a return fromEffectContext(core.succeed(Context.make(tag, resource))) }) /** @internal */ export const succeedContext = ( context: Context.Context ): Layer.Layer => { return fromEffectContext(core.succeed(context)) } /** @internal */ export const suspend = ( evaluate: LazyArg> ): Layer.Layer => { const suspend = Object.create(proto) suspend._tag = OpCodes.OP_SUSPEND suspend.evaluate = evaluate return suspend } /** @internal */ export const sync = dual< >( tag: T ) => ( evaluate: LazyArg> ) => Layer.Layer>, >( tag: T, evaluate: LazyArg> ) => Layer.Layer> >(2, (a, b) => { const tagFirst = Context.isTag(a) const tag = (tagFirst ? a : b) as Context.Tag const evaluate = tagFirst ? b : a return fromEffectContext(core.sync(() => Context.make(tag, evaluate()))) }) /** @internal */ export const syncContext = (evaluate: LazyArg>): Layer.Layer => { return fromEffectContext(core.sync(evaluate)) } /** @internal */ export const tap = dual< ( f: (context: Context.Context) => Effect.Effect ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, f: (context: Context.Context) => Effect.Effect ) => Layer.Layer >(2, (self, f) => flatMap(self, (context) => fromEffectContext(core.as(f(context), context)))) /** @internal */ export const tapError = dual< ( f: (e: XE) => Effect.Effect ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, f: (e: XE) => Effect.Effect ) => Layer.Layer >(2, (self, f) => catchAll( self, (e) => fromEffectContext(core.flatMap(f(e as any), () => core.fail(e))) )) /** @internal */ export const tapErrorCause = dual< ( f: (cause: Cause.Cause) => Effect.Effect ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, f: (cause: Cause.Cause) => Effect.Effect ) => Layer.Layer >(2, (self, f) => catchAllCause( self, (cause) => fromEffectContext(core.flatMap(f(cause as any), () => core.failCause(cause))) )) /** @internal */ export const toRuntime = ( self: Layer.Layer ): Effect.Effect> => { return pipe( fiberRuntime.scopeWith((scope) => pipe(self, buildWithScope(scope))), core.flatMap((context) => pipe( runtime.runtime(), core.provideContext(context) ) ) ) } /** @internal */ export const use = dual< ( self: Layer.Layer ) => ( that: Layer.Layer ) => Layer.Layer, E | E2, ROut2>, ( that: Layer.Layer, self: Layer.Layer ) => Layer.Layer, E | E2, ROut2> >(2, ( that: Layer.Layer, self: Layer.Layer ) => suspend(() => { const provideTo = Object.create(proto) provideTo._tag = OpCodes.OP_PROVIDE_TO provideTo.first = Object.create(proto, { _tag: { value: OpCodes.OP_ZIP_WITH, enumerable: true }, first: { value: context>(), enumerable: true }, second: { value: self }, zipK: { value: (a: Context.Context, b: Context.Context) => pipe(a, Context.merge(b)) } }) provideTo.second = that return provideTo })) /** @internal */ export const useMerge = dual< ( self: Layer.Layer ) => ( that: Layer.Layer ) => Layer.Layer, E2 | E, ROut | ROut2>, ( that: Layer.Layer, self: Layer.Layer ) => Layer.Layer, E2 | E, ROut | ROut2> >(2, (that: Layer.Layer, self: Layer.Layer) => { const zipWith = Object.create(proto) zipWith._tag = OpCodes.OP_ZIP_WITH zipWith.first = self zipWith.second = pipe(self, provide(that)) zipWith.zipK = (a: Context.Context, b: Context.Context): Context.Context => { return pipe(a, Context.merge(b)) } return zipWith }) /** @internal */ export const zipWithPar = dual< ( that: Layer.Layer, f: (a: Context.Context, b: Context.Context) => Context.Context ) => (self: Layer.Layer) => Layer.Layer, ( self: Layer.Layer, that: Layer.Layer, f: (a: Context.Context, b: Context.Context) => Context.Context ) => Layer.Layer >(3, (self, that, f) => suspend(() => { const zipWithPar = Object.create(proto) zipWithPar._tag = OpCodes.OP_ZIP_WITH_PAR zipWithPar.first = self zipWithPar.second = that zipWithPar.zipK = f return zipWithPar })) // circular with Effect /** @internal */ export const provideLayer = dual< (layer: Layer.Layer) => (self: Effect.Effect) => Effect.Effect, (self: Effect.Effect, layer: Layer.Layer) => Effect.Effect >(2, (self, layer) => core.acquireUseRelease( fiberRuntime.scopeMake(), (scope) => core.flatMap( buildWithScope(layer, scope), (context) => core.provideContext(self, context) ), (scope, exit) => core.scopeClose(scope, exit) )) /** @internal */ export const provideSomeRuntime: { (context: Runtime.Runtime): (self: Effect.Effect) => Effect.Effect, E, A> (self: Effect.Effect, context: Runtime.Runtime): Effect.Effect, E, A> } = dual< (context: Runtime.Runtime) => (self: Effect.Effect) => Effect.Effect, E, A>, (self: Effect.Effect, context: Runtime.Runtime) => Effect.Effect, E, A> >(2, (self, rt) => { const patchFlags = runtimeFlags.diff(runtime.defaultRuntime.runtimeFlags, rt.runtimeFlags) const inversePatchFlags = runtimeFlags.diff(rt.runtimeFlags, runtime.defaultRuntime.runtimeFlags) const patchRefs = FiberRefsPatch.diff(runtime.defaultRuntime.fiberRefs, rt.fiberRefs) const inversePatchRefs = FiberRefsPatch.diff(rt.fiberRefs, runtime.defaultRuntime.fiberRefs) return core.acquireUseRelease( core.flatMap( core.updateRuntimeFlags(patchFlags), () => effect.patchFiberRefs(patchRefs) ), () => core.provideSomeContext(self, rt.context), () => core.flatMap( core.updateRuntimeFlags(inversePatchFlags), () => effect.patchFiberRefs(inversePatchRefs) ) ) }) /** @internal */ export const effect_provide = dual< { ( layer: Layer.Layer ): (self: Effect.Effect) => Effect.Effect, E | E2, A> ( context: Context.Context ): (self: Effect.Effect) => Effect.Effect, E, A> ( runtime: Runtime.Runtime ): (self: Effect.Effect) => Effect.Effect, E, A> }, { ( self: Effect.Effect, layer: Layer.Layer ): Effect.Effect, E | E2, A> ( self: Effect.Effect, context: Context.Context ): Effect.Effect, E, A> ( self: Effect.Effect, runtime: Runtime.Runtime ): Effect.Effect, E, A> } >( 2, ( self: Effect.Effect, source: Layer.Layer | Context.Context | Runtime.Runtime ): Effect.Effect, any, any> => isLayer(source) ? provideLayer(self as any, merge(context(), source as Layer.Layer)) : Context.isContext(source) ? core.provideSomeContext(self, source) : provideSomeRuntime(self, source as Runtime.Runtime) )