veffect
Version:
powerful TypeScript validation library built on the robust foundation of Effect combining exceptional type safety, high performance, and developer experience. Taking inspiration from Effect's functional principles, VEffect delivers a balanced approach tha
191 lines • 7.13 kB
TypeScript
/**
* @since 2.0.0
*/
import * as Effect from "effect/Effect";
import type * as Scope from "effect/Scope";
import type { NoSuchElementException } from "./Cause.js";
import * as Deferred from "./Deferred.js";
import * as Fiber from "./Fiber.js";
import * as FiberId from "./FiberId.js";
import * as Inspectable from "./Inspectable.js";
import * as MutableHashMap from "./MutableHashMap.js";
import * as Option from "./Option.js";
import { type Pipeable } from "./Pipeable.js";
import * as Runtime from "./Runtime.js";
/**
* @since 2.0.0
* @categories type ids
*/
export declare const TypeId: unique symbol;
/**
* @since 2.0.0
* @categories type ids
*/
export type TypeId = typeof TypeId;
/**
* @since 2.0.0
* @categories models
*/
export interface FiberMap<in out K, out A = unknown, out E = unknown> extends Pipeable, Inspectable.Inspectable, Iterable<[K, Fiber.RuntimeFiber<A, E>]> {
readonly [TypeId]: TypeId;
readonly backing: MutableHashMap.MutableHashMap<K, Fiber.RuntimeFiber<A, E>>;
readonly deferred: Deferred.Deferred<never, unknown>;
}
/**
* @since 2.0.0
* @categories refinements
*/
export declare const isFiberMap: (u: unknown) => u is FiberMap<unknown, unknown, unknown>;
/**
* A FiberMap can be used to store a collection of fibers, indexed by some key.
* When the associated Scope is closed, all fibers in the map will be interrupted.
*
* You can add fibers to the map using `FiberMap.set` or `FiberMap.run`, and the fibers will
* be automatically removed from the FiberMap when they complete.
*
* @example
* import { Effect, FiberMap } from "effect"
*
* Effect.gen(function*(_) {
* const map = yield* _(FiberMap.make<string>())
*
* // run some effects and add the fibers to the map
* yield* _(FiberMap.run(map, "fiber a", Effect.never))
* yield* _(FiberMap.run(map, "fiber b", Effect.never))
*
* yield* _(Effect.sleep(1000))
* }).pipe(
* Effect.scoped // The fibers will be interrupted when the scope is closed
* )
*
* @since 2.0.0
* @categories constructors
*/
export declare const make: <K, A = unknown, E = unknown>() => Effect.Effect<FiberMap<K, A, E>, never, Scope.Scope>;
/**
* Create an Effect run function that is backed by a FiberMap.
*
* @since 2.0.0
* @categories constructors
*/
export declare const makeRuntime: <R, K, E = unknown, A = unknown>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: Runtime.RunForkOptions | undefined) => Fiber.RuntimeFiber<XA, XE>), never, Scope.Scope | R>;
/**
* Add a fiber to the FiberMap. When the fiber completes, it will be removed from the FiberMap.
* If the key already exists in the FiberMap, the previous fiber will be interrupted.
*
* @since 2.0.0
* @categories combinators
*/
export declare const unsafeSet: {
<K, A, E, XE extends E, XA extends A>(key: K, fiber: Fiber.RuntimeFiber<XA, XE>, interruptAs?: FiberId.FiberId): (self: FiberMap<K, A, E>) => void;
<K, A, E, XE extends E, XA extends A>(self: FiberMap<K, A, E>, key: K, fiber: Fiber.RuntimeFiber<XA, XE>, interruptAs?: FiberId.FiberId): void;
};
/**
* Add a fiber to the FiberMap. When the fiber completes, it will be removed from the FiberMap.
* If the key already exists in the FiberMap, the previous fiber will be interrupted.
*
* @since 2.0.0
* @categories combinators
*/
export declare const set: {
<K, A, E, XE extends E, XA extends A>(key: K, fiber: Fiber.RuntimeFiber<XA, XE>): (self: FiberMap<K, A, E>) => Effect.Effect<void>;
<K, A, E, XE extends E, XA extends A>(self: FiberMap<K, A, E>, key: K, fiber: Fiber.RuntimeFiber<XA, XE>): Effect.Effect<void>;
};
/**
* Retrieve a fiber from the FiberMap.
*
* @since 2.0.0
* @categories combinators
*/
export declare const unsafeGet: {
<K>(key: K): <A, E>(self: FiberMap<K, A, E>) => Option.Option<Fiber.RuntimeFiber<A, E>>;
<K, A, E>(self: FiberMap<K, A, E>, key: K): Option.Option<Fiber.RuntimeFiber<A, E>>;
};
/**
* Retrieve a fiber from the FiberMap.
*
* @since 2.0.0
* @categories combinators
*/
export declare const get: {
<K>(key: K): <A, E>(self: FiberMap<K, A, E>) => Effect.Effect<Fiber.RuntimeFiber<A, E>, NoSuchElementException>;
<K, A, E>(self: FiberMap<K, A, E>, key: K): Effect.Effect<Fiber.RuntimeFiber<A, E>, NoSuchElementException>;
};
/**
* Remove a fiber from the FiberMap, interrupting it if it exists.
*
* @since 2.0.0
* @categories combinators
*/
export declare const remove: {
<K>(key: K): <A, E>(self: FiberMap<K, A, E>) => Effect.Effect<void>;
<K, A, E>(self: FiberMap<K, A, E>, key: K): Effect.Effect<void>;
};
/**
* @since 2.0.0
* @categories combinators
*/
export declare const clear: <K, A, E>(self: FiberMap<K, A, E>) => Effect.Effect<void>;
/**
* Run an Effect and add the forked fiber to the FiberMap.
* When the fiber completes, it will be removed from the FiberMap.
*
* @since 2.0.0
* @categories combinators
*/
export declare const run: {
<K, A, E>(self: FiberMap<K, A, E>, key: K): <R, XE extends E, XA extends A>(effect: Effect.Effect<XA, XE, R>) => Effect.Effect<Fiber.RuntimeFiber<XA, XE>, never, R>;
<K, A, E, R, XE extends E, XA extends A>(self: FiberMap<K, A, E>, key: K, effect: Effect.Effect<XA, XE, R>): Effect.Effect<Fiber.RuntimeFiber<XA, XE>, never, R>;
};
/**
* Capture a Runtime and use it to fork Effect's, adding the forked fibers to the FiberMap.
*
* @example
* import { Context, Effect, FiberMap } from "effect"
*
* interface Users {
* readonly _: unique symbol
* }
* const Users = Context.GenericTag<Users, {
* getAll: Effect.Effect<Array<unknown>>
* }>("Users")
*
* Effect.gen(function*(_) {
* const map = yield* _(FiberMap.make<string>())
* const run = yield* _(FiberMap.runtime(map)<Users>())
*
* // run some effects and add the fibers to the map
* run("effect-a", Effect.andThen(Users, _ => _.getAll))
* run("effect-b", Effect.andThen(Users, _ => _.getAll))
* }).pipe(
* Effect.scoped // The fibers will be interrupted when the scope is closed
* )
*
* @since 2.0.0
* @categories combinators
*/
export declare const runtime: <K, A, E>(self: FiberMap<K, A, E>) => <R = never>() => Effect.Effect<(<XE extends E, XA extends A>(key: K, effect: Effect.Effect<XA, XE, R>, options?: Runtime.RunForkOptions | undefined) => Fiber.RuntimeFiber<XA, XE>), never, R>;
/**
* @since 2.0.0
* @categories combinators
*/
export declare const size: <K, A, E>(self: FiberMap<K, A, E>) => Effect.Effect<number>;
/**
* Join all fibers in the FiberMap. If any of the Fiber's in the map terminate with a failure,
* the returned Effect will terminate with the first failure that occurred.
*
* @since 2.0.0
* @categories combinators
* @example
* import { Effect, FiberMap } from "effect";
*
* Effect.gen(function* (_) {
* const map = yield* _(FiberMap.make());
* yield* _(FiberMap.set(map, "a", Effect.runFork(Effect.fail("error"))));
*
* // parent fiber will fail with "error"
* yield* _(FiberMap.join(map));
* });
*/
export declare const join: <K, A, E>(self: FiberMap<K, A, E>) => Effect.Effect<never, E>;
//# sourceMappingURL=FiberMap.d.ts.map