import type { Maybe } from "./maybe.js"; import type { Result } from "./result.js"; /** * An `Operation` in Effection describes an abstract computation. An operation * does not do anything on its own. Rather, it only describes the steps it will * take when it runs. * * In the Effection world, `Operation` occupies the same position as `Promise` * does the world of async/await. * * An operation can be created with a generator function that only does `yield*` * to other operations: * * @example * ```javascript * import { sleep } from "effection"; * function* slow5(seconds) { * yield* sleep(seconds * 1000); * return 5; * } * ``` * * Operations can also be created using `Symbol.iterator`. The following * operation is the same as above: * * @example * ```javascript * import { sleep } from "effection"; * * const slow5 = (seconds) => ({ * *[Symbol.iterator]() { * yield* sleep(seconds * 1000); * return 5; * } * }) * ``` * * See [Operations guide](https://frontside.com/effection/docs/operations) for more information. * @since 3.0 */ export interface Operation { [Symbol.iterator](): Iterator, T, unknown>; } /** * A value that is both an {@link Operation} _and_ `Promise`. * * Futures are operations that are implicitly associated with an Effection scope * and so they can be freely `await`ed within any async functions. However, they * can also be evaluated directly within another operation, so among other * things, if the operation resolves synchronously, it will continue within the * same tick of the run loop. * * @example * ```ts * let task = run(function* () { * return 42; * }); * * // in an effection scope * let valueFromYield = yield* task; * // in an async function * let valueFromAwait = await task; * ``` * @since 3.0 */ export interface Future extends Operation, Promise { } /** * A handle to a concurrently running operation that lets you either use the * result of that operation, or shut it down. * * When it is run or spawned, an operation executes concurrently with * the rest of the program. The `Task` is both an {@link Operation} and a * `Promise` that lets you consume the result of that operation. * * @example * ```javascript * import { run, sleep } from "effection"; * * let task = run(function*() { * yield* sleep(100); * return "hello world" * }); * * console.log(await task); //=> "hello world" * ``` * * A task can also be created from within an operation by using the * {@link spawn()} operation. * * @example * ```javascript * import { run, spawn, sleep } from "effection"; * * await run(function*() { * let task = yield* spawn(function*() { * yield* sleep(100); * return "hello world"; * }); * console.log(yield* task;) //=> "hello world" * }); * ``` * * Note tasks are subject to the strict guarantees of structured concurrency * and will never outlive their parent. For example, the following spawned task * will never log any output to the console. * * @example * ```javascript * import { run, spawn, sleep } from "effection"; * * await run(function*() { * yield* spawn(function*() { * yield* sleep(100); * console.log("hello world"); * }); * // <--- returns here, so spawned task is shut down as it sleeps. * }); * ``` * * See the guide on [Scopes](https://frontside.com/effection/docs/scope) for * more detail. * * If a `Task` is halted before it finishes executing, then consuming it's * result is an Error. * * @example * ```javascript * import { run, spawn, sleep } from "effection"; * * await run(function*() { * let task = yield* spawn(function*() { * yield* sleep(100); * return "hello world"; * }); * yield* task.halt(); * let output = yield* task; //=> throws Error("halted"); * console.log(output); * }); * ``` * * @see {@link run} * @see {@link spawn} * @see {@link Scope.run} * @since 3.0 */ export interface Task extends Future { /** * Interrupt and shut down a running {@link Operation} and all of its * children. * * Any errors raised by the `halt()` operation only represent problems that * occured during the teardown of the task. In other words, `halt()` can * succeed even if the task failed. * * @returns a future that only resolves when all shutdown associated with this * Task is complete. */ halt(): Future; [Symbol.asyncDispose](): Promise; } /** * The Effection equivalent of an [`AsyncIterator`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/AsyncIterator) * * A subscription acts like a stateful queue that provides a sequence of values * via the next() method. Normally a subscription is created via a * {@link Stream}. * * @example * ```ts * let subscription = yield* stream; * let next = yield* subscription.next(); * ``` * * @see https://effection.deno.dev/docs/collections#subscription * @since 3.0 */ export interface Subscription { next(): Operation>; } /** * The Effection equivalent of an [`AsyncIterable`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Iteration_protocols#the_async_iterator_and_async_iterable_protocols). * * Like async iterables, streams do not actually have state themselves, but * contain the recipe for how to create a {@link Subscription} * * @example * ```ts * let subscription = yield* stream; * let next = yield* subscription.next(); * while (!next.done) { * console.log(next.value); * next = yield* subscription.next(); * } * ``` * * @see https://frontside.com/effection/docs/collections#stream * @since 3.0 */ export type Stream = Operation>; /** * `Context` defines a value which is in effect for a given scope which is an * (action, resource, call, or spawn). * * Unless a context value is defined for a particular scope, it will inherit * its value from its parent scope. * * @example * ```ts * let User = createContext("user"); * yield* User.set("alice"); * console.log(yield* User.expect()); // "alice" * ``` * @since 3.0 */ export interface Context { /** * A unique identifier for this context. */ name: string; /** * The value returned by this context when it is not present on a scop.e */ defaultValue?: T; /** * Read the current value of this context if it exists. * * @returns an operation that yields the current value if it exists, or undefined otherwise. * @see {@link Scope#get} for reading a context value outside of a running operation */ get(): Operation; /** * Set the value of a context on the current scope. It will not effect the value of its * containing scope and will only be visible by this scope and its children. * * @returns an operation yielding the value being set * @see {@link Scope#set} for setting a context value outside of a running operation */ set(value: T): Operation; /** * Read the current value of the context or fail if it does not exist * * @returns an operation that yields the context value * @see {@link Scope#expect} for reading a required context value outside of a running operation */ expect(): Operation; /** * Remove a context value from the current scope. This will only effect the current scope and * not its parent value. * * @returns true if the value existed uniquely on this scope. */ delete(): Operation; /** * Evaluate an operation using `value` for the context. Once the operation is completed, the context * will be reverted to its original value, or removed if it was not present originally. * * @example * ```ts * let user = yield* login(); * yield* UserContext.with(user, function*() { * //do stuff * }) * ``` * * @returns the result of evaluating the operation. */ with(value: T, operation: (value: T) => Operation): Operation; } /** * A programatic API to interact with an Effection scope from outside of an * {@link Operation}. * * Most often this is used to integrate external APIs with Effection by * capturing a `Scope` from a running operation with {@link useScope}, and then * using it to call back into itself from a callback. * * The following example calls into Effection to implement a proxy around a * google search by using [express.js](https://expressjs.com). * * @example * ```javascript * import { main, useScope, suspend } from "effection"; * import { express } from "express"; * * await main(function*() { * let scope = yield* useScope(); * express().get("/", (req, resp) => { * return scope.run(function*() { * let signal = yield* useAbortSignal(); * let response = yield* fetch(`https://google.com?q=${req.params.q}`, { signal }); * resp.send(yield* response.text()); * }); * }); * yield* suspend(); * }); * ``` * @since 3.0 */ export interface Scope { /** * Run an {@link Operation} within `Scope`. * * This is used to create concurrent tasks from _outside_ of a running * operation. To create concurrent tasks from _within_ an already * running operation, use {@link Scope#spawn} * * @param operation - the operation to run * @returns a task of the running operation */ run(operation: () => Operation): Task; /** * Spawn an {@link Operation} within `Scope`. * * This is used to create concurrent tasks from _within_ a running * operation. To create concurrent from outside of Effection, use * {@link Scope#run} */ spawn(operation: () => Operation): Operation>; /** * Get a {@link Context} value from outside of an operation. * * @param context - the context to get * @returns the value of that context in this scope if it exists */ get(context: Context): T | undefined; /** * Set the value of a {@link Context} from outside of an operation * * @param context - the context to set * @param value - the value to set for this context * @returns - the value that was set */ set(context: Context, value: T): T; /** * Get a {@link Context} value from outside of an operation, and throw * a `MissingContextError` if this context is not specified for this scope. * * @param context - the context to get * @returns the value of that context in this scope if it exists */ expect(context: Context): T; /** * Remove a {@link Context} value from this scope. * * @param context - the context to delete */ delete(context: Context): boolean; /** * Check if scope has its own unique value for `context`. * * @returns `true` if scope has its own context, `false` if context is not present, or inherited from its parent. */ hasOwn(context: Context): boolean; /** * Enhance an {@link Api} within this scope by surrounding it with * middleware. * * @param api - the api being enhanced * @param middlewares - collection of {@link Middleware} to be added to this {@link Api} * @param options - specifies which layer of dispatch `middlewares` will be applied * @see {@link Api.around} * @since 4.1 */ around(api: Api, ...options: Parameters["around"]>): void; } /** * A set of methods and values that can be decorated on a per-scope * basis. Apis are ideal for situations that require context * sensitivity such as dependency injection, test mocking, and * instrumentation. * * @example * ```ts * import { type Operation } from "effection"; * import { createApi } from "effection/experimental"; * * interface DbApi { * query(sql: string): Operation<{ id: number; title: string }[]>; * } * * let DatabaseApi = createApi("database", { * *query(sql) { * console.log("running", sql); * return []; * }, * }); * * // scope-local decoration: logging, metrics, retries, mocks, etc. * yield* DatabaseApi.around({ * *query(args, next) { * let [sql] = args; * console.log("SQL:", sql); * return yield* next(...args); * }, * }); * * let rows = yield* DatabaseApi.operations.query("select * from posts"); * console.log(rows.length); * ``` * * @template A - core shape of the Api * @see {@link createApi} * @since 4.1 */ export interface Api { /** * Every member of `A` "lifted" into an operation that invokes that * member on the current {Scope} */ operations: { [K in keyof A]: A[K] extends Operation ? A[K] : A[K] extends (...args: infer TArgs) => infer TReturn ? TReturn extends Operation ? A[K] : (...args: TArgs) => Operation : Operation; }; /** * Enhance an {@link Api} within this scope by surrounding it with * middleware. * * @param middlewares - a set of decorators that will surround the api core * @param options - specify at which layer of dispatch, `middleware` will apply * @returns an {Operation} that installs the middleware in the current {Scope} */ around: (middlewares: Partial>, options?: { at: "min" | "max"; }) => Operation; /** * Call an API as it exists on `scope`. */ invoke: (scope: Scope, key: K, args: A[K] extends (...args: any) => unknown ? Parameters : []) => A[K] extends (...args: any) => unknown ? ReturnType : A[K]; } /** * A general function that can be used to surround any other function * or value. * * @example * ```ts * import type { Operation } from "effection"; * * function* DbMiddleware(args, next): <[string], Operation<{ id: number; title: string }[]>> { * let [sql] = args; * let start = Date.now(); * try { * return yield* next(...args); * } finally { * console.log("query ms", Date.now() - start, sql); * } * }; * ``` * * @since 4.1 */ export interface Middleware { /** * Execute a single link in the middleware stack by doing whatever * computation is necessary and then optionally delegating to the * next link. * * @param args - the arguments to the value being surrounded. * @param next - the next function in the change. It will accept the * arguments contained in `args` * @returns a value with the same shape as `next()`'s return value */ (args: TArgs, next: (...args: TArgs) => TReturn): TReturn; } /** * The shape of middlewares can surround a particular {Api} * * Members of an Api that are values are surrounded by no-arg functions. * * @example * ```ts * import type { Operation } from "effection"; * * interface DatabaseApiShape { * query(sql: string): Operation<{ id: number; title: string }[]>; * close(): Operation; * } * * let around: Partial> = { * *query(args, next) { * let [sql] = args; * if (sql.includes("select")) { * console.log("read query"); * } * return yield* next(...args); * }, * *close(args, next) { * console.log("closing db connection"); * return yield* next(...args); * }, * }; * ``` */ export type Around = { [K in keyof Api]: Api[K] extends (...args: infer TArgs) => infer TReturn ? Middleware : Middleware<[], Api[K]>; }; /** * Unwrap the type of an `Operation`. * Analogous to the built in [`Awaited`](https://www.typescriptlang.org/docs/handbook/utility-types.html#awaitedtype) type. * `Yielded> === T` * * @example * ```ts * type Text = Yielded>; * ``` * @since 3.1 */ export type Yielded> = T extends Operation ? TYield : never; /** * @ignore */ export interface Effect { description: string; enter(resolve: Resolve>, routine: Coroutine): (resolve: Resolve>) => void; } /** * @ignore */ export interface Coroutine { future: Future>>; scope: Scope; data: { exit(resolve: Resolve>): void; enqueued: boolean; critical: boolean; unwinding: boolean; resumeWith: Result; }; resume(result: Result): void; step(): IteratorResult, T>; unwind(): void; perform(effect: Effect): void; settle(outcome: Maybe>): void; } /** * @ignore */ export interface Resolve { (value: T): void; }