/** * @since 1.0.0 */ import type * as Data from "@effect/data/Data" import type { DurationInput } from "@effect/data/Duration" import type * as Option from "@effect/data/Option" import type * as _Cache from "@effect/io/Cache" import type { Deferred } from "@effect/io/Deferred" import type * as Effect from "@effect/io/Effect" import type * as Exit from "@effect/io/Exit" import type { FiberId } from "@effect/io/FiberId" import * as _RequestBlock from "@effect/io/internal/blockedRequests" import * as cache from "@effect/io/internal/cache" import * as core from "@effect/io/internal/core" import * as fiberRuntime from "@effect/io/internal/fiberRuntime" import * as internal from "@effect/io/internal/request" /** * @since 1.0.0 * @category symbols */ export const RequestTypeId: unique symbol = internal.RequestTypeId /** * @since 1.0.0 * @category symbols */ export type RequestTypeId = typeof RequestTypeId /** * A `Request` is a request from a data source for a value of type `A` * that may fail with an `E`. * * @since 1.0.0 * @category models */ export interface Request extends Request.Variance, Data.Case {} /** * @since 1.0.0 */ export declare namespace Request { /** * @since 1.0.0 * @category models */ export interface Variance { readonly [RequestTypeId]: { readonly _E: (_: never) => E readonly _A: (_: never) => A } } /** * @since 1.0.0 * @category models */ export interface Constructor, T extends keyof R = never> { (args: Omit, Request.Success>)>): R } /** * A utility type to extract the error type from a `Request`. * * @since 1.0.0 * @category type-level */ export type Error> = [T] extends [Request] ? _E : never /** * A utility type to extract the value type from a `Request`. * * @since 1.0.0 * @category type-level */ export type Success> = [T] extends [Request] ? _A : never /** * A utility type to extract the result type from a `Request`. * * @since 1.0.0 * @category type-level */ export type Result> = T extends Request ? Exit.Exit : never /** * A utility type to extract the optional result type from a `Request`. * * @since 1.0.0 * @category type-level */ export type OptionalResult> = T extends Request ? Exit.Exit> : never } /** * Returns `true` if the specified value is a `Request`, `false` otherwise. * * @since 1.0.0 * @category refinements */ export const isRequest: (u: unknown) => u is Request = internal.isRequest /** * Constructs a new `Request`. * * @since 1.0.0 * @category constructors */ export const of: >() => Request.Constructor = internal.of /** * Constructs a new `Request`. * * @since 1.0.0 * @category constructors */ export const tagged: & { _tag: string }>( tag: R["_tag"] ) => Request.Constructor = internal.tagged /** * Complete a `Request` with the specified result. * * @since 1.0.0 * @category request completion */ export const complete: { >(result: Request.Result): (self: A) => Effect.Effect >(self: A, result: Request.Result): Effect.Effect } = internal.complete /** * Interrupts the child effect when requests are no longer needed * * @since 1.0.0 * @category request completion */ export const interruptWhenPossible: { (all: Iterable>): (self: Effect.Effect) => Effect.Effect (self: Effect.Effect, all: Iterable>): Effect.Effect } = fiberRuntime.interruptWhenPossible /** * Complete a `Request` with the specified effectful computation, failing the * request with the error from the effect workflow if it fails, and completing * the request with the value of the effect workflow if it succeeds. * * @since 1.0.0 * @category request completion */ export const completeEffect: { , R>( effect: Effect.Effect, Request.Success> ): (self: A) => Effect.Effect , R>( self: A, effect: Effect.Effect, Request.Success> ): Effect.Effect } = internal.completeEffect /** * Complete a `Request` with the specified error. * * @since 1.0.0 * @category request completion */ export const fail: { >(error: Request.Error): (self: A) => Effect.Effect >(self: A, error: Request.Error): Effect.Effect } = internal.fail /** * Complete a `Request` with the specified value. * * @since 1.0.0 * @category request completion */ export const succeed: { >(value: Request.Success): (self: A) => Effect.Effect >(self: A, value: Request.Success): Effect.Effect } = internal.succeed /** * @category models * @since 1.0.0 */ export interface Listeners { count: number observers: Set<(count: number) => void> addObserver(f: (count: number) => void): void removeObserver(f: (count: number) => void): void increment(): void decrement(): void } /** * @category models * @since 1.0.0 */ export interface Cache extends _Cache.ConsumerCache, never, { listeners: Listeners handle: Deferred }> {} /** * @since 1.0.0 * @category models */ export const makeCache = ( options: { readonly capacity: number readonly timeToLive: DurationInput } ): Effect.Effect => cache.make({ ...options, lookup: () => core.map(core.deferredMake(), (handle) => ({ listeners: new internal.Listeners(), handle })) }) /** * @since 1.0.0 * @category symbols */ export const EntryTypeId = Symbol.for("@effect/io/RequestBlock.Entry") /** * @since 1.0.0 * @category symbols */ export type EntryTypeId = typeof EntryTypeId /** * A `Entry` keeps track of a request of type `A` along with a * `Ref` containing the result of the request, existentially hiding the result * type. This is used internally by the library to support data sources that * return different result types for different requests while guaranteeing that * results will be of the type requested. * * @since 1.0.0 * @category models */ export interface Entry extends Entry.Variance { readonly request: R readonly result: Deferred< [R] extends [Request] ? _E : never, [R] extends [Request] ? _A : never > readonly listeners: Listeners readonly ownerId: FiberId readonly state: { completed: boolean } } /** * @since 1.0.0 * @category models */ export declare namespace Entry { /** * @since 1.0.0 * @category models */ export interface Variance { readonly [EntryTypeId]: { readonly _R: (_: never) => R } } } /** * @since 1.0.0 * @category guards */ export const isEntry = _RequestBlock.isEntry /** * @since 1.0.0 * @category constructors */ export const makeEntry = _RequestBlock.makeEntry