Fully retire Box

This commit is contained in:
Aria Minaei 2022-12-01 17:56:34 +00:00
parent ac9d8b4481
commit 41ce858c2b
2 changed files with 3 additions and 99 deletions

View file

@ -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<V> {
/**
* 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<V>
}
/**
* 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<V> implements IBox<V> {
private _publicPrism: Prism<V>
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
}
}

View file

@ -1,9 +1,7 @@
import type {IdentityPrismProvider} from './Atom' import type {IdentityPrismProvider} from './Atom'
import {val} from './Atom' import Atom, {val} from './Atom'
import type {Pointer} from './pointer' import type {Pointer} from './pointer'
import pointer from './pointer' import pointer from './pointer'
import type {IBox} from './Box'
import Box from './Box'
import type {$FixMe, $IntentionalAny} from './types' import type {$FixMe, $IntentionalAny} from './types'
import prism from './prism/prism' import prism from './prism/prism'
@ -21,7 +19,7 @@ export default class PointerProxy<O extends {}>
* @internal * @internal
*/ */
readonly $$isIdentityPrismProvider = true readonly $$isIdentityPrismProvider = true
private readonly _currentPointerBox: IBox<Pointer<O>> private readonly _currentPointerBox: Atom<Pointer<O>>
/** /**
* Convenience pointer pointing to the root of this PointerProxy. * Convenience pointer pointing to the root of this PointerProxy.
* *
@ -31,7 +29,7 @@ export default class PointerProxy<O extends {}>
readonly pointer: Pointer<O> readonly pointer: Pointer<O>
constructor(currentPointer: Pointer<O>) { constructor(currentPointer: Pointer<O>) {
this._currentPointerBox = new Box(currentPointer) this._currentPointerBox = new Atom(currentPointer)
this.pointer = pointer({root: this as $FixMe, path: []}) this.pointer = pointer({root: this as $FixMe, path: []})
} }