// ets_tracing: off import * as Tp from "../Collections/Immutable/Tuple/index.js" import * as E from "../Either/index.js" import { identity, pipe } from "../Function/index.js" import * as T from "./excl-effect.js" export const TypeId = Symbol() export type TypeId = typeof TypeId export interface XFiberRef { /** * Folds over the error and value types of the `FiberRef`. This is a highly * polymorphic method that is capable of arbitrarily transforming the error * and value types of the `FiberRef`. For most use cases one of the more * specific combinators implemented in terms of `fold` will be more ergonomic * but this method is extremely useful for implementing new combinators. */ readonly fold: ( ea: (_: EA) => EC, eb: (_: EB) => ED, ca: (_: C) => E.Either, bd: (_: B) => E.Either ) => XFiberRef /** * Folds over the error and value types of the `FiberRef`, allowing access * to the state in transforming the `set` value. This is a more powerful * version of `fold` but requires unifying the error types. */ readonly foldAll: ( ea: (_: EA) => EC, eb: (_: EB) => ED, ec: (_: EB) => EC, ca: (_: C) => (_: B) => E.Either, bd: (_: B) => E.Either ) => XFiberRef /** * Reads the value associated with the current fiber. Returns initial value if * no value was `set` or inherited from parent. */ readonly get: T.IO /** * Returns an `IO` that runs with `value` bound to the current fiber. * * Guarantees that fiber data is properly restored via `bracket`. */ readonly locally: ( value: A, use: T.Effect ) => T.Effect /** * Sets the value associated with the current fiber. */ readonly set: (value: A) => T.IO } export class Runtime implements XFiberRef { readonly _tag = "Runtime" readonly _typeId: TypeId = TypeId readonly _EA!: () => never readonly _EB!: () => never readonly _A!: (_: A) => void readonly _B!: () => A constructor( readonly initial: A, readonly fork: (_: A) => A = identity, readonly join: (a: A, a1: A) => A = (_, a) => a ) {} fold( _ea: (_: never) => EC, _eb: (_: never) => ED, ca: (_: C) => E.Either, bd: (_: A) => E.Either ): XFiberRef { return new Derived((f) => f(this, bd, ca)) } foldAll( _ea: (_: never) => EC, _eb: (_: never) => ED, _ec: (_: never) => EC, ca: (_: C) => (_: A) => E.Either, bd: (_: A) => E.Either ): XFiberRef { return new DerivedAll((f) => f( this, (s) => bd(s), (c) => (s) => ca(c)(s) ) ) } modify(f: (a: A) => Tp.Tuple<[B, A]>): T.UIO { return new T.IFiberRefModify(this, f) } get get(): T.UIO { return this.modify((v) => Tp.tuple(v, v)) } locally(a: A, use: T.Effect): T.Effect { return T.chain_(this.get, (oldValue) => T.bracket_( this.set(a), () => use, () => this.set(oldValue) ) ) } set(value: A): T.UIO { return this.modify(() => Tp.tuple(undefined, value)) } } export class Derived implements XFiberRef { readonly _tag = "Derived" readonly _typeId: TypeId = TypeId readonly _EA!: () => EA readonly _EB!: () => EB readonly _A!: (_: A) => void readonly _B!: () => B constructor( readonly use: ( f: ( value: Runtime, getEither: (s: S) => E.Either, setEither: (a: A) => E.Either ) => X ) => X ) {} fold( ea: (_: EA) => EC, eb: (_: EB) => ED, ca: (_: C) => E.Either, bd: (_: B) => E.Either ): XFiberRef { return this.use( (value, getEither, setEither) => new Derived((f) => f( value, (s) => E.fold_(getEither(s), (e) => E.left(eb(e)), bd), (c) => E.chain_(ca(c), (a) => E.fold_(setEither(a), (e) => E.left(ea(e)), E.right) ) ) ) ) } foldAll( ea: (_: EA) => EC, eb: (_: EB) => ED, ec: (_: EB) => EC, ca: (_: C) => (_: B) => E.Either, _bd: (_: B) => E.Either ): XFiberRef { return this.use( (value, getEither, setEither) => new DerivedAll((f) => f( value, (s) => E.fold_(getEither(s), (e) => E.left(eb(e)), E.right) as E.Either, (c) => (s) => pipe( getEither(s), E.fold((e) => E.widenA()(E.left(ec(e))), ca(c)), E.chain((a) => pipe( setEither(a), E.fold((e) => E.left(ea(e)), E.right) ) ) ) ) ) ) } get get(): T.IO { return this.use((value, getEither) => pipe( value.get, T.chain((s) => pipe(getEither(s), E.fold(T.fail, T.succeed))) ) ) } locally(a: A, use: T.Effect): T.Effect { return this.use((value, _getEither, setEither) => T.chain_(value.get, (old) => E.fold_( setEither(a), (e) => T.fail(e) as T.IO, (s) => pipe( value.set(s), T.bracket( () => use, () => value.set(old) ) ) ) ) ) } set(a: A): T.IO { return this.use((value, _getEither, setEither) => E.fold_(setEither(a), T.fail, (s) => value.set(s)) ) } } export class DerivedAll implements XFiberRef { readonly _tag = "DerivedAll" readonly _typeId: TypeId = TypeId readonly _EA!: () => EA readonly _EB!: () => EB readonly _A!: (_: A) => void readonly _B!: () => B constructor( readonly use: ( f: ( value: Runtime, getEither: (s: S) => E.Either, setEither: (a: A) => (s: S) => E.Either ) => X ) => X ) {} fold( ea: (_: EA) => EC, eb: (_: EB) => ED, ca: (_: C) => E.Either, bd: (_: B) => E.Either ): XFiberRef { return this.use( (value, getEither, setEither) => new DerivedAll((f) => f( value, (s) => E.fold_(getEither(s), (e) => E.left(eb(e)), bd), (c) => (s) => E.chain_(ca(c), (a) => E.fold_(setEither(a)(s), (e) => E.left(ea(e)), E.right) ) ) ) ) } foldAll( ea: (_: EA) => EC, eb: (_: EB) => ED, ec: (_: EB) => EC, ca: (_: C) => (_: B) => E.Either, bd: (_: B) => E.Either ): XFiberRef { return this.use( (value, getEither, setEither) => new DerivedAll((f) => f( value, (s) => E.fold_(getEither(s), (e) => E.left(eb(e)), bd), (c) => (s) => pipe( getEither(s), E.fold((e) => E.widenA()(E.left(ec(e))), ca(c)), E.chain((a) => E.fold_(setEither(a)(s), (e) => E.left(ea(e)), E.right)) ) ) ) ) } get get(): T.IO { return this.use((value, getEither) => pipe( value.get, T.chain((s) => pipe(getEither(s), E.fold(T.fail, T.succeed))) ) ) } locally(a: A, use: T.Effect): T.Effect { return this.use((value, _getEither, setEither) => T.chain_(value.get, (old) => E.fold_( setEither(a)(old), (e) => T.fail(e) as T.IO, (s) => pipe( value.set(s), T.bracket( () => use, () => value.set(old) ) ) ) ) ) } set(a: A): T.IO { return this.use((value, _getEither, setEither) => T.absolve( value.modify((s) => E.fold_( setEither(a)(s), (e) => Tp.tuple(E.leftW(e), s), (s) => Tp.tuple(E.right(undefined), s) ) ) ) ) } } export interface FiberRef extends XFiberRef {} /** * @ets_optimize identity */ export function concrete(_: XFiberRef) { return _ as Runtime | Derived | DerivedAll }