Unify Derivation and Prism 5/n

This commit is contained in:
Aria Minaei 2022-12-01 14:28:52 +01:00
parent a38d96ec95
commit 0a0c35a7b7
4 changed files with 21 additions and 29 deletions

View file

@ -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
)
}

View file

@ -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(

View file

@ -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'

View file

@ -14,11 +14,7 @@ import type {
SerializableValue,
} from '@theatre/shared/utils/types'
import {valToAtom} from '@theatre/shared/utils/valToAtom'
import type {
IdentityDerivationProvider,
Prism,
Pointer,
} from '@theatre/dataverse'
import type {IdentityPrismProvider, Prism, Pointer} from '@theatre/dataverse'
import {Atom, getPointerParts, pointer, prism, val} from '@theatre/dataverse'
import type SheetObjectTemplate from './SheetObjectTemplate'
@ -42,11 +38,11 @@ type SheetObjectPropsValue = SerializableMap
* Note that this cannot be generic over `Props`, since the user is
* 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' {
return 'Theatre_SheetObject'
}
readonly $$isIdentityDerivationProvider: true = true
readonly $$isIdentityPrismProvider: true = true
readonly address: SheetObjectAddress
readonly publicApi: TheatreSheetObject
private readonly _initialValue = new Atom<SheetObjectPropsValue>({})
@ -215,7 +211,7 @@ export default class SheetObject implements IdentityDerivationProvider {
) as SerializableValue as T
}
getIdentityDerivation(path: Array<string | number>): Prism<unknown> {
getIdentityPrism(path: Array<string | number>): Prism<unknown> {
/**
* @remarks
* TODO perf: Too much indirection here.