import type { Effect } from "@effect/core/io/Effect/definition/base" import { EffectURI } from "@effect/core/io/Effect/definition/base" import type { FiberRuntime } from "@effect/core/io/Fiber/_internal/runtime" import type { Running } from "@effect/core/io/Fiber/status" export type ErasedEffect = | ISync | IAsync | IOnSuccessAndFailure | IOnSuccess | IOnFailure | IUpdateRuntimeFlags | IUpdateRuntimeFlagsDynamic | IStateful | IWhileLoop | IYieldNow | IFailure | ISuccess | ICommit export class ISync implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "Sync" constructor(readonly evaluate: () => A) {} } export interface ICommit extends Effect { readonly _tag: "ICommit" readonly commit: Effect } export class IAsync implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "Async" constructor( readonly register: (resume: (effect: Effect) => void) => void, readonly blockingOn: FiberId ) {} } export class IOnSuccessAndFailure implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "OnSuccessAndFailure" constructor( readonly first: Effect, readonly successK: (a: A) => Effect, readonly failK: (e: Cause) => Effect ) {} } export class IOnSuccess implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "OnSuccess" constructor( readonly first: Effect, readonly successK: (a: A) => Effect ) {} } export class IOnFailure implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "OnFailure" constructor( readonly first: Effect, readonly failK: (a: Cause) => Effect ) {} } export class IUpdateRuntimeFlags implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "UpdateRuntimeFlags" constructor( readonly update: RuntimeFlags.Patch ) {} } export class IUpdateRuntimeFlagsDynamic implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "UpdateRuntimeFlagsWithin" constructor( readonly update: RuntimeFlags.Patch, readonly scope: (oldRuntimeFlags: RuntimeFlags) => Effect ) {} } export class IUpdateRuntimeFlagsInterruptible implements IUpdateRuntimeFlagsDynamic { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "UpdateRuntimeFlagsWithin" readonly update: RuntimeFlags.Patch = RuntimeFlags.Patch.enable(RuntimeFlags.Interruption) readonly scope: (oldRuntimeFlags: RuntimeFlags) => Effect = () => this.effect constructor( readonly effect: Effect ) {} } export class IUpdateRuntimeFlagsUninterruptible implements IUpdateRuntimeFlagsDynamic { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "UpdateRuntimeFlagsWithin" readonly update: RuntimeFlags.Patch = RuntimeFlags.Patch.disable(RuntimeFlags.Interruption) readonly scope: (oldRuntimeFlags: RuntimeFlags) => Effect = () => this.effect constructor( readonly effect: Effect ) {} } export class IStateful implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "Stateful" constructor( readonly onState: (fiber: FiberRuntime, status: Running) => Effect ) {} } export class IWhileLoop implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "WhileLoop" constructor( readonly check: () => boolean, readonly body: () => Effect, readonly process: (a: A) => void ) {} } export class IYieldNow implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "YieldNow" } export class IFailure implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "Failure" constructor(readonly cause: Cause) {} [Hash.sym](): number { return Hash.unknown(this.cause) } [Equals.sym](that: unknown): boolean { return that instanceof IFailure && Equals.equals(this.cause, that.cause) } } export class ISuccess implements Effect { readonly [EffectURI] = { _R: (_: never) => _, _E: (_: never) => _, _A: (_: never) => _ } readonly _tag = "Success" constructor(readonly value: A) {} [Hash.sym](): number { return Hash.unknown(this.value) } [Equals.sym](that: unknown): boolean { return that instanceof ISuccess && Equals.equals(this.value, that.value) } }