FiberHandle
Other
awaitEmpty
Signature
declare function awaitEmpty<A, E>(self: FiberHandle<A, E>): Effect<void, E>;Signature
declare function clear<A, E>(self: FiberHandle<A, E>): Effect<void>;FiberHandle interface
Signature
interface FiberHandle<out A = unknown, out E = unknown> extends Pipeable, Inspectable {
readonly [TypeId]: typeof TypeId;
readonly deferred: Deferred<void, unknown>;
}Retrieve the fiber from the FiberHandle.
Signature
declare function get<A, E>(
self: FiberHandle<A, E>,
): Effect<RuntimeFiber<A, E>, NoSuchElementException>;isFiberHandle
Signature
declare function isFiberHandle(u: unknown): u is FiberHandle<unknown, unknown>;If any of the Fiber's in the handle terminate with a failure, the returned Effect will terminate with the first failure that occurred.
Signature
declare function join<A, E>(self: FiberHandle<A, E>): Effect<void, E>;A FiberHandle can be used to store a single fiber. When the associated Scope is closed, the contained fiber will be interrupted.
You can add a fiber to the handle using FiberHandle.run, and the fiber will be automatically removed from the FiberHandle when it completes.
Signature
declare function make<A = unknown, E = unknown>(): Effect<FiberHandle<A, E>, never, Scope>;makeRuntime
Create an Effect run function that is backed by a FiberHandle.
Signature
declare function makeRuntime<R, E = unknown, A = unknown>(): Effect<
<XE, XA>(
effect: Effect<XA, XE, R>,
options?: RunForkOptions & {
readonly onlyIfMissing?: boolean;
},
) => RuntimeFiber<XA, XE>,
never,
Scope | R
>;makeRuntimePromise
Create an Effect run function that is backed by a FiberHandle.
Signature
declare function makeRuntimePromise<R = never, A = unknown, E = unknown>(): Effect<
<XE, XA>(effect: Effect<XA, XE, R>, options?: RunForkOptions) => Promise<XA>,
never,
Scope | R
>;Run an Effect and add the forked fiber to the FiberHandle. When the fiber completes, it will be removed from the FiberHandle.
Signature
declare const run: {
<A, E>(
self: FiberHandle<A, E>,
options?: {
readonly onlyIfMissing?: boolean;
readonly propagateInterruption?: boolean;
},
): <R, XE, XA>(effect: Effect<XA, XE, R>) => Effect<RuntimeFiber<XA, XE>, never, R>;
<A, E, R, XE, XA>(
self: FiberHandle<A, E>,
effect: Effect<XA, XE, R>,
options?: {
readonly onlyIfMissing?: boolean;
readonly propagateInterruption?: boolean;
},
): Effect<RuntimeFiber<XA, XE>, never, R>;
};Capture a Runtime and use it to fork Effect's, adding the forked fibers to the FiberHandle.
Signature
declare const runtime: <A, E>(
self: FiberHandle<A, E>,
) => <R = never>() => Effect.Effect<
<XE extends E, XA extends A>(
effect: Effect.Effect<XA, XE, R>,
options?: Runtime.RunForkOptions & {
readonly onlyIfMissing?: boolean;
readonly propagateInterruption?: boolean;
},
) => Fiber.RuntimeFiber<XA, XE>,
never,
R
>;Example
import { Context, Effect, FiberHandle } from "effect"
interface Users {
readonly _: unique symbol
}
const Users = Context.GenericTag<
Users,
{
getAll: Effect.Effect<Array<unknown>>
}
>("Users")
Effect.gen(function* () {
const handle = yield* FiberHandle.make()
const run = yield* FiberHandle.runtime(handle)<Users>()
// run an effect and set the fiber in the handle
run(Effect.andThen(Users, (_) => _.getAll))
// this will interrupt the previous fiber
run(Effect.andThen(Users, (_) => _.getAll))
}).pipe(
Effect.scoped, // The fiber will be interrupted when the scope is closed
)runtimePromise
Capture a Runtime and use it to fork Effect's, adding the forked fibers to the FiberHandle.
The returned run function will return Promise's that will resolve when the fiber completes.
Signature
declare function runtimePromise<A, E>(
self: FiberHandle<A, E>,
): <R = never>() => Effect<
<XE, XA>(
effect: Effect<XA, XE, R>,
options?: RunForkOptions & {
readonly propagateInterruption?: boolean;
},
) => Promise<XA>,
never,
R
>;Set the fiber in the FiberHandle. When the fiber completes, it will be removed from the FiberHandle. If a fiber already exists in the FiberHandle, it will be interrupted unless options.onlyIfMissing is set.
Signature
declare const set: {
<A, E, XE, XA>(
fiber: RuntimeFiber<XA, XE>,
options?: {
readonly onlyIfMissing?: boolean;
readonly propagateInterruption?: boolean;
},
): (self: FiberHandle<A, E>) => Effect<void>;
<A, E, XE, XA>(
self: FiberHandle<A, E>,
fiber: RuntimeFiber<XA, XE>,
options?: {
readonly onlyIfMissing?: boolean;
readonly propagateInterruption?: boolean;
},
): Effect<void>;
};Signature
declare const TypeId: unique symbol;Signature
type TypeId = typeof TypeId;Retrieve the fiber from the FiberHandle.
Signature
declare function unsafeGet<A, E>(self: FiberHandle<A, E>): Option<RuntimeFiber<A, E>>;Set the fiber in a FiberHandle. When the fiber completes, it will be removed from the FiberHandle. If a fiber is already running, it will be interrupted unless options.onlyIfMissing is set.
Signature
declare const unsafeSet: {
<A, E, XE, XA>(
fiber: RuntimeFiber<XA, XE>,
options?: {
readonly interruptAs?: FiberId.FiberId;
readonly onlyIfMissing?: boolean;
readonly propagateInterruption?: boolean;
},
): (self: FiberHandle<A, E>) => void;
<A, E, XE, XA>(
self: FiberHandle<A, E>,
fiber: RuntimeFiber<XA, XE>,
options?: {
readonly interruptAs?: FiberId.FiberId;
readonly onlyIfMissing?: boolean;
readonly propagateInterruption?: boolean;
},
): void;
};
Wait for the fiber in the FiberHandle to complete.