// ets_tracing: off import "../Operator/index.js" /** * Async is a lightweight Effect data type that support as parameters: * - R: environment * - E: error * - A: output * * And additionally supports interruption */ import * as A from "@effect-ts/system/Async" import * as E from "@effect-ts/system/Either" import { NoSuchElementException } from "@effect-ts/system/GlobalExceptions" import type { Has, Tag } from "@effect-ts/system/Has" import type * as O from "@effect-ts/system/Option" import { identity, pipe } from "../Function/index.js" import type { AsyncURI } from "../Modules/index.js" import type { URI } from "../Prelude/index.js" import * as P from "../Prelude/index.js" import type { Sync } from "../Sync/index.js" import { runEitherEnv } from "../Sync/index.js" import { isEither, isOption, isTag } from "../Utils/index.js" export { branch as if, branch_ as if_ } export type V = P.V<"R", "-"> & P.V<"E", "+"> export const Covariant = P.instance], V>>({ map: A.map }) export const Any = P.instance], V>>({ any: () => A.succeed({}) }) export const AssociativeBoth = P.instance], V>>({ both: A.zip }) export const AssociativeFlatten = P.instance], V>>({ flatten }) export const IdentityBoth = P.instance], V>>({ ...Any, ...AssociativeBoth }) export const IdentityFlatten = P.instance], V>>({ ...Any, ...AssociativeFlatten }) export const Applicative = P.instance], V>>({ ...Covariant, ...IdentityBoth }) export const Monad = P.instance], V>>({ ...Covariant, ...IdentityFlatten }) export const Fail = P.instance], V>>({ fail: A.fail }) export const Run = P.instance], V>>({ either: (x) => pipe( x, A.map(E.right), A.catchAll((e) => A.succeed(E.left(e))) ) }) export const either: ( fa: A.Async ) => A.Async> = Run.either export const getValidation = P.getValidationF({ ...Monad, ...Run, ...Applicative, ...Fail }) export const Provide = P.instance], V>>({ provide: A.provideAll }) export const Access = P.instance], V>>({ access: A.access }) const genAdapter: { (_: Tag): P.GenHKT, never, A>, A> (_: O.Option, onNone: () => E): P.GenHKT, A> (_: O.Option): P.GenHKT, A> (_: E.Either): P.GenHKT, A> (_: A.Async): P.GenHKT, A> } = (_: any, __?: any): any => { if (isTag(_)) { return new P.GenHKT(A.service(_)) } if (isEither(_)) { return new P.GenHKT(_._tag === "Left" ? A.fail(_.left) : A.succeed(_.right)) } if (isOption(_)) { return new P.GenHKT( _._tag === "None" ? A.fail(__ ? __() : new NoSuchElementException()) : A.succeed(_.value) ) } return new P.GenHKT(_) } export const gen = P.genF(Monad, { adapter: genAdapter }) export function flatten( ffa: A.Async> ): A.Async { return pipe(ffa, A.chain(identity)) } export function fromEither(_: E.Either) { return _._tag === "Left" ? A.fail(_.left) : A.succeed(_.right) } export function fromSync(_: Sync) { return A.accessM((r: R) => fromEither(runEitherEnv(r)(_))) } export const { match, matchIn, matchMorph, matchTag, matchTagIn } = P.matchers(Covariant) /** * Conditionals */ const branch = P.conditionalF(Covariant) const branch_ = P.conditionalF_(Covariant) export * from "@effect-ts/system/Async"