diff --git a/packages/dataverse/src/Box.ts b/packages/dataverse/src/Box.ts deleted file mode 100644 index 5ad0e6d..0000000 --- a/packages/dataverse/src/Box.ts +++ /dev/null @@ -1,94 +0,0 @@ -import type {Prism} from './prism/Interface' -import prism from './prism/prism' - -/** - * Common interface for Box types. Boxes wrap a single value. - */ -export interface IBox { - /** - * Sets the value of the Box. - * - * @param v - The value to update the Box with. - */ - - set(v: V): void - /** - * Gets the value of the Box. - * - * @remarks - * Usages of `get()` aren't tracked, they are only for retrieving the value. To track changes, you need use a prism. - * - * @see prism - */ - get(): V - - /** - * Returns a prism of the Box that you can use to track changes to it. - */ - prism: Prism -} - -/** - * Wraps a single value. - * - * @remarks - * Prisms created with {@link Box.prism} update based on strict equality (`===`) of the old value and the new one. - * This also means that property-changes of objects won't be tracked, and that for objects, updates will trigger on changes of - * reference even if the objects are structurally equal. - */ -export default class Box implements IBox { - private _publicPrism: Prism - private _changeListeners = new Set<(newVal: V) => void>() - - /** - * @param _value - The initial value of the Box. - */ - constructor( - /** - * @internal - */ - protected _value: V, - ) { - const subscribe = (listener: (val: V) => void) => { - this._changeListeners.add(listener) - return () => this._changeListeners.delete(listener) - } - - const getValue = () => this._value - this._publicPrism = prism(() => { - return prism.source(subscribe, getValue) - }) - } - - /** - * Sets the value of the Box. - * - * @param v - The value to update the Box with. - */ - set(v: V) { - if (v === this._value) return - this._value = v - this._changeListeners.forEach((listener) => { - listener(v) - }) - } - - /** - * Gets the value of the Box. - * - * Note: usages of `get()` aren't tracked, they are only for retrieving the value. To track changes, you need to - * use a prism. - * - * @see Box.prism - */ - get() { - return this._value - } - - /** - * Returns a prism of the Box that you can use to track changes to it. - */ - get prism() { - return this._publicPrism - } -} diff --git a/packages/dataverse/src/PointerProxy.ts b/packages/dataverse/src/PointerProxy.ts index b30188c..de6d9ca 100644 --- a/packages/dataverse/src/PointerProxy.ts +++ b/packages/dataverse/src/PointerProxy.ts @@ -1,9 +1,7 @@ import type {IdentityPrismProvider} from './Atom' -import {val} from './Atom' +import Atom, {val} from './Atom' import type {Pointer} from './pointer' import pointer from './pointer' -import type {IBox} from './Box' -import Box from './Box' import type {$FixMe, $IntentionalAny} from './types' import prism from './prism/prism' @@ -21,7 +19,7 @@ export default class PointerProxy * @internal */ readonly $$isIdentityPrismProvider = true - private readonly _currentPointerBox: IBox> + private readonly _currentPointerBox: Atom> /** * Convenience pointer pointing to the root of this PointerProxy. * @@ -31,7 +29,7 @@ export default class PointerProxy readonly pointer: Pointer constructor(currentPointer: Pointer) { - this._currentPointerBox = new Box(currentPointer) + this._currentPointerBox = new Atom(currentPointer) this.pointer = pointer({root: this as $FixMe, path: []}) }