// ets_tracing: off /** * inspired by https://github.com/tusharmath/qio/pull/22 (revised) */ import { _A, _E, _R } from "../Effect/commons.js" import type { Either } from "../Either/index.js" import { identity } from "../Function/index.js" import { NoSuchElementException } from "../GlobalExceptions/index.js" import type { Has, Tag } from "../Has/index.js" import type { Option } from "../Option/index.js" import type * as Utils from "../Utils/index.js" import { isEither, isOption, isTag } from "../Utils/index.js" import type { Sync } from "./core.js" import { chain_, fail, succeed, suspend } from "./core.js" import { accessService } from "./has.js" export class GenSync { readonly [_R]!: (_R: R) => void; readonly [_E]!: () => E; readonly [_A]!: () => A constructor(readonly effect: Sync) {} *[Symbol.iterator](): Generator, A, any> { return yield this } } const adapter = (_: any, __?: any) => { if (isTag(_)) { return new GenSync(accessService(_)(identity)) } if (isEither(_)) { return new GenSync(_._tag === "Left" ? fail(_.left) : succeed(_.right)) } if (isOption(_)) { return new GenSync( _._tag === "None" ? fail(__ ? __() : new NoSuchElementException()) : succeed(_.value) ) } return new GenSync(_) } export function gen, AEff>( f: (i: { (_: Tag): GenSync, never, A> (_: Option, onNone: () => E): GenSync (_: Option): GenSync (_: Either): GenSync (_: Sync): GenSync }) => Generator ): Sync, Utils._E, AEff> { return suspend(() => { const iterator = f(adapter as any) const state = iterator.next() function run( state: IteratorYieldResult | IteratorReturnResult ): Sync { if (state.done) { return succeed(state.value) } return chain_(state.value["effect"], (val) => { const next = iterator.next(val) return run(next) }) } return run(state) }) }