Unify Derivation and Prism 5/n
This commit is contained in:
parent
a38d96ec95
commit
0a0c35a7b7
4 changed files with 21 additions and 29 deletions
|
@ -22,19 +22,19 @@ enum ValueTypes {
|
|||
/**
|
||||
* Interface for objects that can provide a derivation at a certain path.
|
||||
*/
|
||||
export interface IdentityDerivationProvider {
|
||||
export interface IdentityPrismProvider {
|
||||
/**
|
||||
* @internal
|
||||
* Future: We could consider using a `Symbol.for("dataverse/IdentityDerivationProvider")` as a key here, similar to
|
||||
* Future: We could consider using a `Symbol.for("dataverse/IdentityPrismProvider")` as a key here, similar to
|
||||
* how {@link Iterable} works for `of`.
|
||||
*/
|
||||
readonly $$isIdentityDerivationProvider: true
|
||||
readonly $$isIdentityPrismProvider: true
|
||||
/**
|
||||
* Returns a derivation of the value at the provided path.
|
||||
*
|
||||
* @param path - The path to create the derivation at.
|
||||
*/
|
||||
getIdentityDerivation(path: Array<string | number>): Prism<unknown>
|
||||
getIdentityPrism(path: Array<string | number>): Prism<unknown>
|
||||
}
|
||||
|
||||
const getTypeOfValue = (v: unknown): ValueTypes => {
|
||||
|
@ -115,14 +115,12 @@ class Scope {
|
|||
/**
|
||||
* Wraps an object whose (sub)properties can be individually tracked.
|
||||
*/
|
||||
export default class Atom<State extends {}>
|
||||
implements IdentityDerivationProvider
|
||||
{
|
||||
export default class Atom<State extends {}> implements IdentityPrismProvider {
|
||||
private _currentState: State
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
readonly $$isIdentityDerivationProvider = true
|
||||
readonly $$isIdentityPrismProvider = true
|
||||
private readonly _rootScope: Scope
|
||||
/**
|
||||
* Convenience property that gives you a pointer to the root of the atom.
|
||||
|
@ -246,7 +244,7 @@ export default class Atom<State extends {}>
|
|||
*
|
||||
* @param path - The path to create the derivation at.
|
||||
*/
|
||||
getIdentityDerivation(path: Array<string | number>): Prism<unknown> {
|
||||
getIdentityPrism(path: Array<string | number>): Prism<unknown> {
|
||||
const subscribe = (listener: (val: unknown) => void) =>
|
||||
this._onPathValueChange(path, listener)
|
||||
|
||||
|
@ -274,25 +272,23 @@ export const pointerToPrism = <P extends PointerType<$IntentionalAny>>(
|
|||
let derivation = identityDerivationWeakMap.get(meta)
|
||||
if (!derivation) {
|
||||
const root = meta.root
|
||||
if (!isIdentityDerivationProvider(root)) {
|
||||
if (!isIdentityPrismProvider(root)) {
|
||||
throw new Error(
|
||||
`Cannot run pointerToPrism() on a pointer whose root is not an IdentityChangeProvider`,
|
||||
`Cannot run pointerToPrism() on a pointer whose root is not an IdentityPrismProvider`,
|
||||
)
|
||||
}
|
||||
const {path} = meta
|
||||
derivation = root.getIdentityDerivation(path)
|
||||
derivation = root.getIdentityPrism(path)
|
||||
identityDerivationWeakMap.set(meta, derivation)
|
||||
}
|
||||
return derivation as $IntentionalAny
|
||||
}
|
||||
|
||||
function isIdentityDerivationProvider(
|
||||
val: unknown,
|
||||
): val is IdentityDerivationProvider {
|
||||
function isIdentityPrismProvider(val: unknown): val is IdentityPrismProvider {
|
||||
return (
|
||||
typeof val === 'object' &&
|
||||
val !== null &&
|
||||
(val as $IntentionalAny)['$$isIdentityDerivationProvider'] === true
|
||||
(val as $IntentionalAny)['$$isIdentityPrismProvider'] === true
|
||||
)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
import type {IdentityDerivationProvider} from './Atom'
|
||||
import type {IdentityPrismProvider} from './Atom'
|
||||
import {val} from './Atom'
|
||||
import type {Pointer} from './pointer'
|
||||
import pointer from './pointer'
|
||||
|
@ -15,12 +15,12 @@ import prism from './derivations/prism/prism'
|
|||
* to the proxied pointer too.
|
||||
*/
|
||||
export default class PointerProxy<O extends {}>
|
||||
implements IdentityDerivationProvider
|
||||
implements IdentityPrismProvider
|
||||
{
|
||||
/**
|
||||
* @internal
|
||||
*/
|
||||
readonly $$isIdentityDerivationProvider = true
|
||||
readonly $$isIdentityPrismProvider = true
|
||||
private readonly _currentPointerBox: IBox<Pointer<O>>
|
||||
/**
|
||||
* Convenience pointer pointing to the root of this PointerProxy.
|
||||
|
@ -48,7 +48,7 @@ export default class PointerProxy<O extends {}>
|
|||
*
|
||||
* @param path - The path to create the derivation at.
|
||||
*/
|
||||
getIdentityDerivation(path: Array<string | number>) {
|
||||
getIdentityPrism(path: Array<string | number>) {
|
||||
return prism(() => {
|
||||
const currentPointer = this._currentPointerBox.derivation.getValue()
|
||||
const subPointer = path.reduce(
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
* @packageDocumentation
|
||||
*/
|
||||
|
||||
export type {IdentityDerivationProvider} from './Atom'
|
||||
export type {IdentityPrismProvider} from './Atom'
|
||||
export {default as Atom, val, pointerToPrism} from './Atom'
|
||||
export {default as Box} from './Box'
|
||||
export type {IBox} from './Box'
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue