/** * @since 1.0.0 */ import * as Equal from "effect/Equal" import { globalValue } from "effect/GlobalValue" import * as Hash from "effect/Hash" const TypeId: TypeId = "~effect-atom/atom/AtomRef" type TypeId = "~effect-atom/atom/AtomRef" /** * @since 1.0.0 * @category models */ export interface ReadonlyRef extends Equal.Equal { readonly [TypeId]: TypeId readonly key: string readonly value: A readonly subscribe: (f: (a: A) => void) => () => void readonly map: (f: (a: A) => B) => ReadonlyRef } /** * @since 1.0.0 * @category models */ export interface AtomRef extends ReadonlyRef { readonly prop: (prop: K) => AtomRef readonly set: (value: A) => AtomRef readonly update: (f: (value: A) => A) => AtomRef } /** * @since 1.0.0 * @category models */ export interface Collection extends ReadonlyRef>> { readonly push: (item: A) => Collection readonly insertAt: (index: number, item: A) => Collection readonly remove: (ref: AtomRef) => Collection readonly toArray: () => Array } /** * @since 1.0.0 * @category constructors */ export const make = (value: A): AtomRef => new AtomRefImpl(value) /** * @since 1.0.0 * @category constructors */ export const collection = (items: Iterable): Collection => new CollectionImpl(items) const keyState = globalValue("@effect-atom/atom/AtomRef/keyState", () => ({ count: 0, generate() { return `AtomRef-${this.count++}` } })) class ReadonlyRefImpl implements ReadonlyRef { readonly [TypeId]: TypeId readonly key = keyState.generate() constructor(public value: A) { this[TypeId] = TypeId } [Equal.symbol](that: Equal.Equal) { return Equal.equals(this.value, (that as ReadonlyRef).value) } [Hash.symbol]() { return Hash.hash(this.value) } listeners: Array<(a: A) => void> = [] listenerCount = 0 notify(a: A) { for (let i = 0; i < this.listenerCount; i++) { this.listeners[i](a) } } subscribe(f: (a: A) => void): () => void { this.listeners.push(f) this.listenerCount++ return () => { const index = this.listeners.indexOf(f) if (index !== -1) { this.listeners[index] = this.listeners[this.listenerCount - 1] this.listeners.pop() this.listenerCount-- } } } map(f: (a: A) => B): ReadonlyRef { return new MapRefImpl(this, f) } } class AtomRefImpl extends ReadonlyRefImpl implements AtomRef { prop(prop: K): AtomRef { return new PropRefImpl(this, prop) } set(value: A) { if (Equal.equals(value, this.value)) { return this } this.value = value this.notify(value) return this } update(f: (value: A) => A) { return this.set(f(this.value)) } } class MapRefImpl implements ReadonlyRef { readonly [TypeId]: TypeId readonly key = keyState.generate() constructor(readonly parent: ReadonlyRef, readonly transform: (a: A) => B) { this[TypeId] = TypeId } [Equal.symbol](that: Equal.Equal) { return Equal.equals(this.value, (that as ReadonlyRef).value) } [Hash.symbol]() { return Hash.hash(this.value) } get value() { return this.transform(this.parent.value) } subscribe(f: (a: B) => void): () => void { let previous = this.transform(this.parent.value) return this.parent.subscribe((a) => { const next = this.transform(a) if (Equal.equals(next, previous)) { return } previous = next f(next) }) } map(f: (a: B) => C): ReadonlyRef { return new MapRefImpl(this, f) } } class PropRefImpl implements AtomRef { readonly [TypeId]: TypeId readonly key = keyState.generate() private previous: A[K] constructor(readonly parent: AtomRef, readonly _prop: K) { this[TypeId] = TypeId this.previous = parent.value[_prop] } [Equal.symbol](that: Equal.Equal) { return Equal.equals(this.value, (that as ReadonlyRef).value) } [Hash.symbol]() { return Hash.hash(this.value) } get value() { if (this.parent.value && this._prop in (this.parent.value as any)) { this.previous = this.parent.value[this._prop] } return this.previous } subscribe(f: (a: A[K]) => void): () => void { let previous = this.value return this.parent.subscribe((a) => { if (!a || !(this._prop in (a as any))) { return } const next = a[this._prop] if (Equal.equals(next, previous)) { return } previous = next f(next) }) } map(f: (a: A[K]) => C): ReadonlyRef { return new MapRefImpl(this, f) } prop(prop: CK): AtomRef { return new PropRefImpl(this, prop) } set(value: A[K]): AtomRef { if (Array.isArray(this.parent.value)) { const newArray = this.parent.value.slice() newArray[this._prop as number] = value this.parent.set(newArray as A) } else { this.parent.set({ ...this.parent.value, [this._prop]: value }) } return this } update(f: (value: A[K]) => A[K]): AtomRef { if (Array.isArray(this.parent.value)) { const newArray = this.parent.value.slice() newArray[this._prop as number] = f(this.parent.value[this._prop]) this.parent.set(newArray as A) } else { this.parent.set({ ...this.parent.value, [this._prop]: f(this.parent.value[this._prop]) }) } return this } } class CollectionImpl extends ReadonlyRefImpl>> implements Collection { constructor(items: Iterable) { super([]) for (const item of items) { this.value.push(this.makeRef(item)) } } makeRef(value: A) { const ref = new AtomRefImpl(value) const notify = (value: A) => { ref.notify(value) this.notify(this.value) } return new Proxy(ref, { get(target, p, _receiver) { if (p === "notify") { return notify } return target[p as keyof AtomRef] } }) } push(item: A) { const ref = this.makeRef(item) this.value.push(ref) this.notify(this.value) return this } insertAt(index: number, item: A) { const ref = this.makeRef(item) this.value.splice(index, 0, ref) this.notify(this.value) return this } remove(ref: AtomRef) { const index = this.value.indexOf(ref) if (index !== -1) { this.value.splice(index, 1) this.notify(this.value) } return this } toArray() { return this.value.map((ref) => ref.value) } }