/** * @since 1.0.0 */ import * as Context from "effect/Context" import * as Effect from "effect/Effect" import * as FiberRef from "effect/FiberRef" import { dual } from "effect/Function" import * as Layer from "effect/Layer" import * as Mailbox from "effect/Mailbox" import { hasProperty } from "effect/Predicate" import * as Scope from "effect/Scope" import * as Stream from "effect/Stream" import type * as Atom from "./Atom.js" import type { Registry } from "./index.js" import * as internal from "./internal/registry.js" import * as Result from "./Result.js" const TypeId: TypeId = "~effect-atom/atom/Registry" type TypeId = "~effect-atom/atom/Registry" /** * @since 1.0.0 * @category guards */ export const isRegistry = (u: unknown): u is Registry => hasProperty(u, TypeId) /** * @since 1.0.0 * @category models */ export interface Registry { readonly [TypeId]: TypeId readonly getNodes: () => ReadonlyMap | string, Node> readonly get: (atom: Atom.Atom) => A readonly mount: (atom: Atom.Atom) => () => void readonly refresh: (atom: Atom.Atom) => void readonly set: (atom: Atom.Writable, value: W) => void readonly setSerializable: (key: string, encoded: unknown) => void readonly modify: (atom: Atom.Writable, f: (_: R) => [returnValue: A, nextValue: W]) => A readonly update: (atom: Atom.Writable, f: (_: R) => W) => void readonly subscribe: (atom: Atom.Atom, f: (_: A) => void, options?: { readonly immediate?: boolean }) => () => void readonly reset: () => void readonly dispose: () => void } /** * @since 1.0.0 * @category models */ interface Node { readonly atom: Atom.Atom readonly value: () => A } /** * @since 1.0.0 * @category constructors */ export const make: ( options?: { readonly initialValues?: Iterable, any]> | undefined readonly scheduleTask?: ((f: () => void) => void) | undefined readonly timeoutResolution?: number | undefined readonly defaultIdleTTL?: number | undefined } | undefined ) => Registry = internal.make /** * @since 1.0.0 * @category Tags */ export class AtomRegistry extends Context.Tag("@effect/atom/Registry/CurrentRegistry")< AtomRegistry, Registry >() {} /** * @since 1.0.0 * @category Layers */ export const layerOptions = (options?: { readonly initialValues?: Iterable, any]> | undefined readonly scheduleTask?: ((f: () => void) => void) | undefined readonly timeoutResolution?: number | undefined readonly defaultIdleTTL?: number | undefined }): Layer.Layer => Layer.scoped( AtomRegistry, Effect.gen(function*() { const scope = yield* Effect.scope const scheduler = yield* FiberRef.get(FiberRef.currentScheduler) const registry = internal.make({ ...options, scheduleTask: options?.scheduleTask ?? ((f) => scheduler.scheduleTask(f, 0)) }) yield* Scope.addFinalizer(scope, Effect.sync(() => registry.dispose())) return registry }) ) /** * @since 1.0.0 * @category Layers */ export const layer: Layer.Layer = layerOptions() // ----------------------------------------------------------------------------- // conversions // ----------------------------------------------------------------------------- /** * @since 1.0.0 * @category Conversions */ export const toStream: { (atom: Atom.Atom): (self: Registry) => Stream.Stream (self: Registry, atom: Atom.Atom): Stream.Stream } = dual( 2, (self: Registry, atom: Atom.Atom) => Stream.unwrapScoped( Effect.contextWithEffect((context: Context.Context) => { const scope = Context.get(context, Scope.Scope) return Mailbox.make().pipe( Effect.tap((mailbox) => { const cancel = self.subscribe(atom, (value) => mailbox.unsafeOffer(value), { immediate: true }) return Scope.addFinalizer( scope, Effect.suspend(() => { cancel() return mailbox.shutdown }) ) }), Effect.uninterruptible, Effect.map((mailbox) => Mailbox.toStream(mailbox)) ) }) ) ) /** * @since 1.0.0 * @category Conversions */ export const toStreamResult: { (atom: Atom.Atom>): (self: Registry) => Stream.Stream (self: Registry, atom: Atom.Atom>): Stream.Stream } = dual( 2, (self: Registry, atom: Atom.Atom>): Stream.Stream => toStream(self, atom).pipe( Stream.filter(Result.isNotInitial), Stream.mapEffect((result) => result._tag === "Success" ? Effect.succeed(result.value) : Effect.failCause(result.cause) ) ) ) /** * @since 1.0.0 * @category Conversions */ export const getResult: { (atom: Atom.Atom>, options?: { readonly suspendOnWaiting?: boolean | undefined }): (self: Registry) => Effect.Effect (self: Registry, atom: Atom.Atom>, options?: { readonly suspendOnWaiting?: boolean | undefined }): Effect.Effect } = dual( (args) => isRegistry(args[0]), (self: Registry, atom: Atom.Atom>, options?: { readonly suspendOnWaiting?: boolean | undefined }): Effect.Effect => { const suspendOnWaiting = options?.suspendOnWaiting ?? false return Effect.async((resume) => { const result = self.get(atom) if (result._tag !== "Initial" && !(suspendOnWaiting && result.waiting)) { return resume(Result.toExit(result) as any) } const cancel = self.subscribe(atom, (value) => { if (value._tag !== "Initial" && !(suspendOnWaiting && value.waiting)) { resume(Result.toExit(value) as any) cancel() } }) return Effect.sync(cancel) }) } )