UNPKG

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

235 lines 7.41 kB
/** * @since 2.0.0 */ import * as Effect from "effect/Effect"; import * as Cause from "./Cause.js"; import * as Deferred from "./Deferred.js"; import * as Exit from "./Exit.js"; import * as Fiber from "./Fiber.js"; import * as FiberId from "./FiberId.js"; import * as FiberRef from "./FiberRef.js"; import { dual } from "./Function.js"; import * as Inspectable from "./Inspectable.js"; import * as MutableHashMap from "./MutableHashMap.js"; import * as Option from "./Option.js"; import { pipeArguments } from "./Pipeable.js"; import * as Predicate from "./Predicate.js"; import * as Runtime from "./Runtime.js"; /** * @since 2.0.0 * @categories type ids */ export const TypeId = /*#__PURE__*/Symbol.for("effect/FiberMap"); /** * @since 2.0.0 * @categories refinements */ export const isFiberMap = u => Predicate.hasProperty(u, TypeId); const Proto = { [TypeId]: TypeId, [Symbol.iterator]() { return this.backing[Symbol.iterator](); }, toString() { return Inspectable.format(this.toJSON()); }, toJSON() { return { _id: "FiberMap", backing: this.backing.toJSON() }; }, [Inspectable.NodeInspectSymbol]() { return this.toJSON(); }, pipe() { return pipeArguments(this, arguments); } }; const unsafeMake = (backing, deferred) => { const self = Object.create(Proto); self.backing = backing; self.deferred = deferred; return self; }; /** * 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 const make = () => Effect.acquireRelease(Effect.map(Deferred.make(), deferred => unsafeMake(MutableHashMap.empty(), deferred)), clear); /** * Create an Effect run function that is backed by a FiberMap. * * @since 2.0.0 * @categories constructors */ export const makeRuntime = () => Effect.flatMap(make(), self => runtime(self)()); /** * 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 const unsafeSet = /*#__PURE__*/dual(args => isFiberMap(args[0]), (self, key, fiber, interruptAs) => { const previous = MutableHashMap.get(self.backing, key); if (previous._tag === "Some") { if (previous.value === fiber) { return; } previous.value.unsafeInterruptAsFork(interruptAs ?? FiberId.none); } ; fiber.setFiberRef(FiberRef.unhandledErrorLogLevel, Option.none()); MutableHashMap.set(self.backing, key, fiber); fiber.addObserver(exit => { const current = MutableHashMap.get(self.backing, key); if (Option.isSome(current) && fiber === current.value) { MutableHashMap.remove(self.backing, key); } if (Exit.isFailure(exit) && !Cause.isInterruptedOnly(exit.cause)) { Deferred.unsafeDone(self.deferred, exit); } }); }); /** * 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 const set = /*#__PURE__*/dual(3, (self, key, fiber) => Effect.fiberIdWith(fiberId => Effect.sync(() => unsafeSet(self, key, fiber, fiberId)))); /** * Retrieve a fiber from the FiberMap. * * @since 2.0.0 * @categories combinators */ export const unsafeGet = /*#__PURE__*/dual(2, (self, key) => MutableHashMap.get(self.backing, key)); /** * Retrieve a fiber from the FiberMap. * * @since 2.0.0 * @categories combinators */ export const get = /*#__PURE__*/dual(2, (self, key) => Effect.suspend(() => MutableHashMap.get(self.backing, key))); /** * Remove a fiber from the FiberMap, interrupting it if it exists. * * @since 2.0.0 * @categories combinators */ export const remove = /*#__PURE__*/dual(2, (self, key) => Effect.suspend(() => { const fiber = MutableHashMap.get(self.backing, key); if (fiber._tag === "None") { return Effect.unit; } MutableHashMap.remove(self.backing, key); return Fiber.interrupt(fiber.value); })); /** * @since 2.0.0 * @categories combinators */ export const clear = self => Effect.zipRight(Effect.forEach(self.backing, ([_, fiber]) => Fiber.interrupt(fiber)), Effect.sync(() => { MutableHashMap.clear(self.backing); })); /** * 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 const run = function () { if (arguments.length === 2) { const self = arguments[0]; const key = arguments[1]; return effect => Effect.tap(Effect.forkDaemon(effect), fiber => set(self, key, fiber)); } const self = arguments[0]; const key = arguments[1]; const effect = arguments[2]; return Effect.tap(Effect.forkDaemon(effect), fiber => set(self, key, fiber)); }; /** * 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 const runtime = self => () => Effect.map(Effect.runtime(), runtime => { const runFork = Runtime.runFork(runtime); return (key, effect, options) => { const fiber = runFork(effect, options); unsafeSet(self, key, fiber); return fiber; }; }); /** * @since 2.0.0 * @categories combinators */ export const size = self => Effect.sync(() => MutableHashMap.size(self.backing)); /** * 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 const join = self => Deferred.await(self.deferred); //# sourceMappingURL=FiberMap.js.map