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.
|
* Interface for objects that can provide a derivation at a certain path.
|
||||||
*/
|
*/
|
||||||
export interface IdentityDerivationProvider {
|
export interface IdentityPrismProvider {
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @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`.
|
* how {@link Iterable} works for `of`.
|
||||||
*/
|
*/
|
||||||
readonly $$isIdentityDerivationProvider: true
|
readonly $$isIdentityPrismProvider: true
|
||||||
/**
|
/**
|
||||||
* Returns a derivation of the value at the provided path.
|
* Returns a derivation of the value at the provided path.
|
||||||
*
|
*
|
||||||
* @param path - The path to create the derivation at.
|
* @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 => {
|
const getTypeOfValue = (v: unknown): ValueTypes => {
|
||||||
|
@ -115,14 +115,12 @@ class Scope {
|
||||||
/**
|
/**
|
||||||
* Wraps an object whose (sub)properties can be individually tracked.
|
* Wraps an object whose (sub)properties can be individually tracked.
|
||||||
*/
|
*/
|
||||||
export default class Atom<State extends {}>
|
export default class Atom<State extends {}> implements IdentityPrismProvider {
|
||||||
implements IdentityDerivationProvider
|
|
||||||
{
|
|
||||||
private _currentState: State
|
private _currentState: State
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
readonly $$isIdentityDerivationProvider = true
|
readonly $$isIdentityPrismProvider = true
|
||||||
private readonly _rootScope: Scope
|
private readonly _rootScope: Scope
|
||||||
/**
|
/**
|
||||||
* Convenience property that gives you a pointer to the root of the atom.
|
* 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.
|
* @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) =>
|
const subscribe = (listener: (val: unknown) => void) =>
|
||||||
this._onPathValueChange(path, listener)
|
this._onPathValueChange(path, listener)
|
||||||
|
|
||||||
|
@ -274,25 +272,23 @@ export const pointerToPrism = <P extends PointerType<$IntentionalAny>>(
|
||||||
let derivation = identityDerivationWeakMap.get(meta)
|
let derivation = identityDerivationWeakMap.get(meta)
|
||||||
if (!derivation) {
|
if (!derivation) {
|
||||||
const root = meta.root
|
const root = meta.root
|
||||||
if (!isIdentityDerivationProvider(root)) {
|
if (!isIdentityPrismProvider(root)) {
|
||||||
throw new Error(
|
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
|
const {path} = meta
|
||||||
derivation = root.getIdentityDerivation(path)
|
derivation = root.getIdentityPrism(path)
|
||||||
identityDerivationWeakMap.set(meta, derivation)
|
identityDerivationWeakMap.set(meta, derivation)
|
||||||
}
|
}
|
||||||
return derivation as $IntentionalAny
|
return derivation as $IntentionalAny
|
||||||
}
|
}
|
||||||
|
|
||||||
function isIdentityDerivationProvider(
|
function isIdentityPrismProvider(val: unknown): val is IdentityPrismProvider {
|
||||||
val: unknown,
|
|
||||||
): val is IdentityDerivationProvider {
|
|
||||||
return (
|
return (
|
||||||
typeof val === 'object' &&
|
typeof val === 'object' &&
|
||||||
val !== null &&
|
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 {val} from './Atom'
|
||||||
import type {Pointer} from './pointer'
|
import type {Pointer} from './pointer'
|
||||||
import pointer from './pointer'
|
import pointer from './pointer'
|
||||||
|
@ -15,12 +15,12 @@ import prism from './derivations/prism/prism'
|
||||||
* to the proxied pointer too.
|
* to the proxied pointer too.
|
||||||
*/
|
*/
|
||||||
export default class PointerProxy<O extends {}>
|
export default class PointerProxy<O extends {}>
|
||||||
implements IdentityDerivationProvider
|
implements IdentityPrismProvider
|
||||||
{
|
{
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
readonly $$isIdentityDerivationProvider = true
|
readonly $$isIdentityPrismProvider = true
|
||||||
private readonly _currentPointerBox: IBox<Pointer<O>>
|
private readonly _currentPointerBox: IBox<Pointer<O>>
|
||||||
/**
|
/**
|
||||||
* Convenience pointer pointing to the root of this PointerProxy.
|
* 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.
|
* @param path - The path to create the derivation at.
|
||||||
*/
|
*/
|
||||||
getIdentityDerivation(path: Array<string | number>) {
|
getIdentityPrism(path: Array<string | number>) {
|
||||||
return prism(() => {
|
return prism(() => {
|
||||||
const currentPointer = this._currentPointerBox.derivation.getValue()
|
const currentPointer = this._currentPointerBox.derivation.getValue()
|
||||||
const subPointer = path.reduce(
|
const subPointer = path.reduce(
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
* @packageDocumentation
|
* @packageDocumentation
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export type {IdentityDerivationProvider} from './Atom'
|
export type {IdentityPrismProvider} from './Atom'
|
||||||
export {default as Atom, val, pointerToPrism} from './Atom'
|
export {default as Atom, val, pointerToPrism} from './Atom'
|
||||||
export {default as Box} from './Box'
|
export {default as Box} from './Box'
|
||||||
export type {IBox} from './Box'
|
export type {IBox} from './Box'
|
||||||
|
|
|
@ -14,11 +14,7 @@ import type {
|
||||||
SerializableValue,
|
SerializableValue,
|
||||||
} from '@theatre/shared/utils/types'
|
} from '@theatre/shared/utils/types'
|
||||||
import {valToAtom} from '@theatre/shared/utils/valToAtom'
|
import {valToAtom} from '@theatre/shared/utils/valToAtom'
|
||||||
import type {
|
import type {IdentityPrismProvider, Prism, Pointer} from '@theatre/dataverse'
|
||||||
IdentityDerivationProvider,
|
|
||||||
Prism,
|
|
||||||
Pointer,
|
|
||||||
} from '@theatre/dataverse'
|
|
||||||
|
|
||||||
import {Atom, getPointerParts, pointer, prism, val} from '@theatre/dataverse'
|
import {Atom, getPointerParts, pointer, prism, val} from '@theatre/dataverse'
|
||||||
import type SheetObjectTemplate from './SheetObjectTemplate'
|
import type SheetObjectTemplate from './SheetObjectTemplate'
|
||||||
|
@ -42,11 +38,11 @@ type SheetObjectPropsValue = SerializableMap
|
||||||
* Note that this cannot be generic over `Props`, since the user is
|
* Note that this cannot be generic over `Props`, since the user is
|
||||||
* able to change prop configs for the sheet object's properties.
|
* able to change prop configs for the sheet object's properties.
|
||||||
*/
|
*/
|
||||||
export default class SheetObject implements IdentityDerivationProvider {
|
export default class SheetObject implements IdentityPrismProvider {
|
||||||
get type(): 'Theatre_SheetObject' {
|
get type(): 'Theatre_SheetObject' {
|
||||||
return 'Theatre_SheetObject'
|
return 'Theatre_SheetObject'
|
||||||
}
|
}
|
||||||
readonly $$isIdentityDerivationProvider: true = true
|
readonly $$isIdentityPrismProvider: true = true
|
||||||
readonly address: SheetObjectAddress
|
readonly address: SheetObjectAddress
|
||||||
readonly publicApi: TheatreSheetObject
|
readonly publicApi: TheatreSheetObject
|
||||||
private readonly _initialValue = new Atom<SheetObjectPropsValue>({})
|
private readonly _initialValue = new Atom<SheetObjectPropsValue>({})
|
||||||
|
@ -215,7 +211,7 @@ export default class SheetObject implements IdentityDerivationProvider {
|
||||||
) as SerializableValue as T
|
) as SerializableValue as T
|
||||||
}
|
}
|
||||||
|
|
||||||
getIdentityDerivation(path: Array<string | number>): Prism<unknown> {
|
getIdentityPrism(path: Array<string | number>): Prism<unknown> {
|
||||||
/**
|
/**
|
||||||
* @remarks
|
* @remarks
|
||||||
* TODO perf: Too much indirection here.
|
* TODO perf: Too much indirection here.
|
||||||
|
|
Loading…
Reference in a new issue