import * as Either from "@effect/data/Either" import * as Cause from "@effect/io/Cause" import type * as Exit from "@effect/io/Exit" import type * as BlockedRequests from "@effect/query/internal_effect_untraced/blockedRequests" import type * as Continue from "@effect/query/internal_effect_untraced/continue" /** * A `Result` is the result of running one step of a `Query`. A result * may either by done with a value `A`, blocked on a set of requests to data * sources that require an environment `R`, or failed with an `E`. * * @internal */ export type Result = Blocked | Done | Fail /** @internal */ export interface Blocked { readonly _tag: "Blocked" readonly blockedRequests: BlockedRequests.BlockedRequests readonly continue: Continue.Continue } /** @internal */ export interface Done { readonly _tag: "Done" readonly value: A } /** @internal */ export interface Fail { readonly _tag: "Fail" readonly cause: Cause.Cause } /** * Constructs a result that is blocked on the specified requests with the * specified continuation. * * @internal */ export const blocked = ( blockedRequests: BlockedRequests.BlockedRequests, _continue: Continue.Continue ): Result => ({ _tag: "Blocked", blockedRequests, continue: _continue }) /** * Constructs a result that is done with the specified value. * * @internal */ export const done = (value: A): Result => ({ _tag: "Done", value }) /** * Constructs a result that is failed with the specified `Cause`. * * @internal */ export const fail = (cause: Cause.Cause): Result => ({ _tag: "Fail", cause }) /** * Returns `true` if the result is blocked, `false` otherwise. * * @internal */ export const isBlocked = (result: Result): result is Blocked => result._tag === "Blocked" /** * Returns `true` if the result is done, `false` otherwise. * * @internal */ export const isDone = (result: Result): result is Done => result._tag === "Done" /** * Returns `true` if the result failed, `false` otherwise. * * @internal */ export const isFail = (result: Result): result is Fail => result._tag === "Fail" /** * Lifts an `Either` into a result. * * @internal */ export const fromEither: (either: Either.Either) => Result = Either.match( (left) => fail(Cause.fail(left)), done ) /** * Lifts an `Exit` into a result. * * @internal */ export const fromExit = (exit: Exit.Exit): Result => { switch (exit._tag) { case "Failure": { return fail(exit.cause) } case "Success": { return done(exit.value) } } }