import * as Effect from "effect/Effect" import * as Equal from "effect/Equal" import * as Exit from "effect/Exit" import { constVoid, pipe } from "effect/Function" import { globalValue } from "effect/GlobalValue" import * as Option from "effect/Option" import * as Queue from "effect/Queue" import * as Stream from "effect/Stream" import type * as Atom from "../Atom.js" import type * as Registry from "../Registry.js" import * as Result from "../Result.js" const constImmediate = { immediate: true } const notifyListener = (listener: () => void): void => { listener() } type TypeId = "~effect-atom/atom/Registry" const TypeId: TypeId = "~effect-atom/atom/Registry" as const /** @internal */ export const make = (options?: { readonly initialValues?: Iterable, any]> | undefined readonly scheduleTask?: ((f: () => void) => void) | undefined readonly timeoutResolution?: number | undefined readonly defaultIdleTTL?: number | undefined }): Registry.Registry => new RegistryImpl( options?.initialValues, options?.scheduleTask, options?.timeoutResolution, options?.defaultIdleTTL ) const SerializableTypeId: Atom.SerializableTypeId = "~effect-atom/atom/Atom/Serializable" const atomKey = (atom: Atom.Atom): Atom.Atom | string => SerializableTypeId in atom ? (atom as Atom.Serializable)[SerializableTypeId].key : atom class RegistryImpl implements Registry.Registry { readonly [TypeId]: TypeId readonly timeoutResolution: number constructor( initialValues?: Iterable, any]>, readonly scheduleTask = (cb: () => void): void => queueMicrotask(cb), timeoutResolution?: number, readonly defaultIdleTTL?: number ) { this[TypeId] = TypeId if (timeoutResolution === undefined && defaultIdleTTL !== undefined) { this.timeoutResolution = Math.round(defaultIdleTTL / 2) } else { this.timeoutResolution = timeoutResolution ?? 1000 } if (initialValues !== undefined) { for (const [atom, value] of initialValues) { this.ensureNode(atom).setValue(value) } } } readonly nodes = new Map | string, Node>() readonly preloadedSerializable = new Map() readonly timeoutBuckets = new Map>, handle: number]>() readonly nodeTimeoutBucket = new Map, number>() disposed = false getNodes() { return this.nodes } get(atom: Atom.Atom): A { return this.ensureNode(atom).value() } set(atom: Atom.Writable, value: W): void { atom.write(this.ensureNode(atom).writeContext, value) } setSerializable(key: string, encoded: unknown): void { this.preloadedSerializable.set(key, encoded) } modify(atom: Atom.Writable, f: (_: R) => [returnValue: A, nextValue: W]): A { const node = this.ensureNode(atom) const result = f(node.value()) atom.write(node.writeContext, result[1]) return result[0] } update(atom: Atom.Writable, f: (_: R) => W): void { const node = this.ensureNode(atom) atom.write(node.writeContext, f(node.value())) } refresh = (atom: Atom.Atom): void => { if (atom.refresh !== undefined) { atom.refresh(this.refresh) } else { this.invalidateAtom(atom) } } subscribe(atom: Atom.Atom, f: (_: A) => void, options?: { readonly immediate?: boolean }): () => void { const node = this.ensureNode(atom) if (options?.immediate) { f(node.value()) } const remove = node.subscribe(function() { f(node._value) }) return () => { remove() if (node.canBeRemoved) { this.scheduleNodeRemoval(node) } } } mount(atom: Atom.Atom) { return this.subscribe(atom, constVoid, constImmediate) } atomHasTtl(atom: Atom.Atom): boolean { return !atom.keepAlive && atom.idleTTL !== 0 && (atom.idleTTL !== undefined || this.defaultIdleTTL !== undefined) } ensureNode(atom: Atom.Atom): Node { const key = atomKey(atom) let node = this.nodes.get(key) if (node === undefined) { node = this.createNode(atom) this.nodes.set(key, node) } else if (this.atomHasTtl(atom)) { this.removeNodeTimeout(node) } if (typeof key === "string" && this.preloadedSerializable.has(key)) { const encoded = this.preloadedSerializable.get(key) this.preloadedSerializable.delete(key) const decoded = (atom as any as Atom.Serializable)[SerializableTypeId].decode(encoded) node.setValue(decoded) } return node } createNode(atom: Atom.Atom): Node { if (this.disposed) { throw new Error(`Cannot access Atom ${atom}: registry is disposed`) } if (!atom.keepAlive) { this.scheduleAtomRemoval(atom) } return new Node(this, atom) } invalidateAtom = (atom: Atom.Atom): void => { this.ensureNode(atom).invalidate() } scheduleAtomRemoval(atom: Atom.Atom): void { this.scheduleTask(() => { const node = this.nodes.get(atomKey(atom)) if (node !== undefined && node.canBeRemoved) { this.removeNode(node) } }) } scheduleNodeRemoval(node: Node): void { this.scheduleTask(() => { if (node.canBeRemoved) { this.removeNode(node) } }) } removeNode(node: Node): void { if (this.atomHasTtl(node.atom)) { this.setNodeTimeout(node) } else { this.nodes.delete(atomKey(node.atom)) node.remove() } } setNodeTimeout(node: Node): void { if (this.nodeTimeoutBucket.has(node)) { return } let idleTTL = node.atom.idleTTL ?? this.defaultIdleTTL! if (this.#currentSweepTTL !== null) { idleTTL -= this.#currentSweepTTL if (idleTTL <= 0) { this.nodes.delete(atomKey(node.atom)) node.remove() return } } const ttl = Math.ceil(idleTTL! / this.timeoutResolution) * this.timeoutResolution const timestamp = Date.now() + ttl const bucket = timestamp - (timestamp % this.timeoutResolution) + this.timeoutResolution let entry = this.timeoutBuckets.get(bucket) if (entry === undefined) { entry = [ new Set>(), setTimeout(() => this.sweepBucket(bucket), bucket - Date.now()) ] this.timeoutBuckets.set(bucket, entry) } entry[0].add(node) this.nodeTimeoutBucket.set(node, bucket) } removeNodeTimeout(node: Node): void { const bucket = this.nodeTimeoutBucket.get(node) if (bucket === undefined) return this.nodeTimeoutBucket.delete(node) this.scheduleNodeRemoval(node) const [nodes, handle] = this.timeoutBuckets.get(bucket)! nodes.delete(node) if (nodes.size === 0) { clearTimeout(handle) this.timeoutBuckets.delete(bucket) } } #currentSweepTTL: number | null = null sweepBucket(bucket: number): void { const nodes = this.timeoutBuckets.get(bucket)![0] this.timeoutBuckets.delete(bucket) nodes.forEach((node) => { this.nodeTimeoutBucket.delete(node) if (!node.canBeRemoved) return this.nodes.delete(atomKey(node.atom)) this.#currentSweepTTL = node.atom.idleTTL ?? this.defaultIdleTTL! node.remove() this.#currentSweepTTL = null }) } reset(): void { this.timeoutBuckets.forEach(([, handle]) => clearTimeout(handle)) this.timeoutBuckets.clear() this.nodeTimeoutBucket.clear() this.nodes.forEach((node) => node.remove()) this.nodes.clear() } dispose(): void { this.disposed = true this.reset() } } const enum NodeFlags { alive = 1 << 0, initialized = 1 << 1, waitingForValue = 1 << 2 } const enum NodeState { uninitialized = NodeFlags.alive | NodeFlags.waitingForValue, stale = NodeFlags.alive | NodeFlags.initialized | NodeFlags.waitingForValue, valid = NodeFlags.alive | NodeFlags.initialized, removed = 0 } class Node { constructor( readonly registry: RegistryImpl, readonly atom: Atom.Atom ) { this.writeContext = new WriteContextImpl(registry, this) } state: NodeState = NodeState.uninitialized lifetime: Lifetime | undefined writeContext: WriteContextImpl parents = new Set>() previousParents: Set> | undefined children = new Set>() listeners = new Set<() => void>() skipInvalidation = false building = false invalidatedDuringBuild = false get canBeRemoved(): boolean { return !this.atom.keepAlive && this.listeners.size === 0 && this.children.size === 0 && this.state !== 0 } _value: A = undefined as any value(): A { if ((this.state & NodeFlags.waitingForValue) !== 0) { this.lifetime = makeLifetime(this) this.building = true const value = this.atom.read(this.lifetime) this.building = false if ((this.state & NodeFlags.waitingForValue) !== 0) { this.setValue(value) } if (this.previousParents) { const parents = this.previousParents this.previousParents = undefined for (const parent of parents) { parent.removeChild(this) if (parent.canBeRemoved) { this.registry.scheduleNodeRemoval(parent) } } } } return this._value } valueOption(): Option.Option { if ((this.state & NodeFlags.initialized) === 0) { return Option.none() } return Option.some(this._value) } setValue(value: A): void { if ((this.state & NodeFlags.initialized) === 0) { this.state = NodeState.valid this._value = value if (batchState.phase === BatchPhase.collect) { batchState.notify.add(this) } else { this.notify() } return } this.state = NodeState.valid if (Equal.equals(this._value, value)) { return } this._value = value if (this.skipInvalidation) { this.skipInvalidation = false } else { this.invalidateChildren() } if (this.listeners.size > 0) { if (batchState.phase === BatchPhase.collect) { batchState.notify.add(this) } else { this.notify() } } } addParent(parent: Node): void { this.parents.add(parent) if (this.previousParents !== undefined) { this.previousParents.delete(parent) if (this.previousParents.size === 0) { this.previousParents = undefined } } if (!parent.children.has(this)) { parent.children.add(this) if (parent.skipInvalidation) { parent.skipInvalidation = false } } } removeChild(child: Node): void { this.children.delete(child) } invalidate(): void { if (this.building && batchState.phase === BatchPhase.collect) { this.invalidatedDuringBuild = true } if (this.state === NodeState.valid) { this.state = NodeState.stale this.disposeLifetime() } if (batchState.phase === BatchPhase.collect) { batchState.stale.push(this) } else if (this.atom.lazy && this.listeners.size === 0 && !childrenAreActive(this.children)) { this.invalidateChildren() this.skipInvalidation = true } else { this.value() } } invalidateChildren(): void { if (this.children.size === 0) { return } const children = this.children this.children = new Set() for (const child of children) { child.invalidate() } } notify(): void { this.listeners.forEach(notifyListener) if (batchState.phase === BatchPhase.commit) { batchState.notify.delete(this) } } disposeLifetime(): void { if (this.lifetime !== undefined) { this.lifetime.dispose() this.lifetime = undefined } if (this.parents.size !== 0) { this.previousParents = this.parents this.parents = new Set() } } remove() { this.state = NodeState.removed this.listeners.clear() this.disposeLifetime() if (this.previousParents === undefined) { return } const parents = this.previousParents this.previousParents = undefined for (const parent of parents) { parent.removeChild(this) if (parent.canBeRemoved) { this.registry.removeNode(parent) } } } subscribe(listener: () => void): () => void { this.listeners.add(listener) return () => this.listeners.delete(listener) } } function childrenAreActive(children: Set>): boolean { if (children.size === 0) { return false } let current: Set> | undefined = children let stack: Array>> | undefined let stackIndex = 0 while (current !== undefined) { for (const child of current) { if (!child.atom.lazy || child.listeners.size > 0) { return true } else if (child.children.size > 0) { if (stack === undefined) { stack = [child.children] } else { stack.push(child.children) } } } current = stack?.[stackIndex++] } return false } interface Lifetime extends Atom.Context { isFn: boolean readonly node: Node finalizers: Array<() => void> | undefined disposed: boolean readonly dispose: () => void } const disposedError = (atom: Atom.Atom): Error => new Error(`Cannot use context of disposed Atom: ${atom}`) const LifetimeProto: Omit, "node" | "finalizers" | "disposed" | "isFn"> = { get registry(): RegistryImpl { return (this as Lifetime).node.registry }, addFinalizer(this: Lifetime, f: () => void): void { if (this.disposed) { throw disposedError(this.node.atom) } this.finalizers ??= [] this.finalizers.push(f) }, get(this: Lifetime, atom: Atom.Atom): A { if (this.disposed) { throw disposedError(this.node.atom) } const parent = this.node.registry.ensureNode(atom) const value = parent.value() this.node.addParent(parent) return value }, result(this: Lifetime, atom: Atom.Atom>, options?: { readonly suspendOnWaiting?: boolean | undefined }): Effect.Effect { if (this.disposed) { throw disposedError(this.node.atom) } else if (this.isFn) { return this.resultOnce(atom, options) } const result = this.get(atom) if (options?.suspendOnWaiting && result.waiting) { return Effect.never } switch (result._tag) { case "Initial": { return Effect.never } case "Failure": { return Exit.failCause(result.cause) } case "Success": { return Effect.succeed(result.value) } } }, resultOnce(this: Lifetime, atom: Atom.Atom>, options?: { readonly suspendOnWaiting?: boolean | undefined }): Effect.Effect { if (this.disposed) { throw disposedError(this.node.atom) } return Effect.async((resume) => { const result = this.once(atom) if (result._tag !== "Initial" && !(options?.suspendOnWaiting && result.waiting)) { return resume(Result.toExit(result) as any) } const cancel = this.node.registry.subscribe(atom, (result) => { if (result._tag === "Initial" || (options?.suspendOnWaiting && result.waiting)) return cancel() resume(Result.toExit(result) as any) }, { immediate: false }) return Effect.sync(cancel) }) }, setResult(this: Lifetime, atom: Atom.Writable, W>, value: W): Effect.Effect { if (this.disposed) { throw disposedError(this.node.atom) } this.node.registry.set(atom, value) return this.resultOnce(atom, { suspendOnWaiting: true }) }, some(this: Lifetime, atom: Atom.Atom>): Effect.Effect { if (this.disposed) { throw disposedError(this.node.atom) } else if (this.isFn) { return this.someOnce(atom) } const result = this.get(atom) return result._tag === "None" ? Effect.never : Effect.succeed(result.value) }, someOnce(this: Lifetime, atom: Atom.Atom>): Effect.Effect { if (this.disposed) { throw disposedError(this.node.atom) } return Effect.async((resume) => { const result = this.once(atom) if (Option.isSome(result)) { return resume(Effect.succeed(result.value)) } const cancel = this.node.registry.subscribe(atom, (result) => { if (Option.isNone(result)) return cancel() resume(Effect.succeed(result.value)) }, { immediate: false }) return Effect.sync(cancel) }) }, once(this: Lifetime, atom: Atom.Atom): A { if (this.disposed) { throw disposedError(this.node.atom) } return this.node.registry.get(atom) }, self(this: Lifetime): Option.Option { if (this.disposed) { throw disposedError(this.node.atom) } return this.node.valueOption() as any }, refresh(this: Lifetime, atom: Atom.Atom): void { if (this.disposed) { throw disposedError(this.node.atom) } this.node.registry.refresh(atom) }, refreshSelf(this: Lifetime): void { if (this.disposed) { throw disposedError(this.node.atom) } this.node.invalidate() }, mount(this: Lifetime, atom: Atom.Atom): void { if (this.disposed) { throw disposedError(this.node.atom) } this.addFinalizer(this.node.registry.mount(atom)) }, subscribe(this: Lifetime, atom: Atom.Atom, f: (_: A) => void, options?: { readonly immediate?: boolean }): void { if (this.disposed) { throw disposedError(this.node.atom) } this.addFinalizer(this.node.registry.subscribe(atom, f, options)) }, setSelf(this: Lifetime, a: A): void { if (this.disposed) { throw disposedError(this.node.atom) } this.node.setValue(a as any) }, set(this: Lifetime, atom: Atom.Writable, value: W): void { if (this.disposed) { throw disposedError(this.node.atom) } this.node.registry.set(atom, value) }, stream(this: Lifetime, atom: Atom.Atom, options?: { readonly withoutInitialValue?: boolean readonly bufferSize?: number }) { if (this.disposed) { throw disposedError(this.node.atom) } return pipe( Effect.acquireRelease( Queue.bounded(options?.bufferSize ?? 16), Queue.shutdown ), Effect.tap((queue) => Effect.acquireRelease( Effect.sync(() => { return this.node.registry.subscribe(atom, (_) => { Queue.unsafeOffer(queue, _) }, { immediate: options?.withoutInitialValue !== true }) }), (cancel) => Effect.sync(cancel) ) ), Effect.map((queue) => Stream.fromQueue(queue)), Stream.unwrapScoped ) }, streamResult(this: Lifetime, atom: Atom.Atom>, options?: { readonly withoutInitialValue?: boolean readonly bufferSize?: number }): Stream.Stream { return pipe( this.stream(atom, options), Stream.filter(Result.isNotInitial), Stream.flatMap((result) => result._tag === "Success" ? Stream.succeed(result.value) : Stream.failCause(result.cause) ) ) }, dispose(this: Lifetime): void { this.disposed = true if (this.finalizers === undefined) { return } const finalizers = this.finalizers this.finalizers = undefined for (let i = finalizers.length - 1; i >= 0; i--) { finalizers[i]() } } } const makeLifetime = (node: Node): Lifetime => { function get(atom: Atom.Atom): A { if (get.disposed) { throw disposedError(atom) } else if (get.isFn) { return node.registry.get(atom) } const parent = node.registry.ensureNode(atom) const value = parent.value() node.addParent(parent) return value } Object.setPrototypeOf(get, LifetimeProto) get.isFn = false get.disposed = false get.finalizers = undefined get.node = node return get as any } class WriteContextImpl implements Atom.WriteContext { constructor( readonly registry: RegistryImpl, readonly node: Node ) {} get(atom: Atom.Atom): A { return this.registry.get(atom) } set(atom: Atom.Writable, value: W) { return this.registry.set(atom, value) } setSelf(value: any) { return this.node.setValue(value) } refreshSelf() { return this.node.invalidate() } } // ----------------------------------------------------------------------------- // batching // ----------------------------------------------------------------------------- /** @internal */ export const enum BatchPhase { disabled, collect, commit } /** @internal */ export const batchState = globalValue("@effect-atom/atom/Registry/batchState", () => ({ phase: BatchPhase.disabled, depth: 0, stale: [] as Array>, notify: new Set>() })) /** @internal */ export function batch(f: () => void): void { batchState.phase = BatchPhase.collect batchState.depth++ try { f() if (batchState.depth === 1) { for (let i = 0; i < batchState.stale.length; i++) { batchRebuildNode(batchState.stale[i]) } batchState.phase = BatchPhase.commit for (const node of batchState.notify) { node.notify() } batchState.notify.clear() } } finally { batchState.depth-- if (batchState.depth === 0) { batchState.phase = BatchPhase.disabled batchState.stale = [] } } } function batchRebuildNode(node: Node) { if (node.state === NodeState.valid) { if (!node.invalidatedDuringBuild) { return } node.invalidatedDuringBuild = false node.state = NodeState.stale node.disposeLifetime() } for (const parent of node.parents) { if (parent.state !== NodeState.valid) { batchRebuildNode(parent) } } // @ts-ignore if (node.state !== NodeState.valid) { node.value() } }