From 0a0c35a7b7c62fa997e167fcae6ce0d09cf5c054 Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Thu, 1 Dec 2022 14:28:52 +0100 Subject: [PATCH] Unify Derivation and Prism 5/n --- packages/dataverse/src/Atom.ts | 28 +++++++++----------- packages/dataverse/src/PointerProxy.ts | 8 +++--- packages/dataverse/src/index.ts | 2 +- theatre/core/src/sheetObjects/SheetObject.ts | 12 +++------ 4 files changed, 21 insertions(+), 29 deletions(-) diff --git a/packages/dataverse/src/Atom.ts b/packages/dataverse/src/Atom.ts index 22270ce..09732a5 100644 --- a/packages/dataverse/src/Atom.ts +++ b/packages/dataverse/src/Atom.ts @@ -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): Prism + getIdentityPrism(path: Array): Prism } 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 - implements IdentityDerivationProvider -{ +export default class Atom 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 * * @param path - The path to create the derivation at. */ - getIdentityDerivation(path: Array): Prism { + getIdentityPrism(path: Array): Prism { const subscribe = (listener: (val: unknown) => void) => this._onPathValueChange(path, listener) @@ -274,25 +272,23 @@ export const pointerToPrism =

>( 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 ) } diff --git a/packages/dataverse/src/PointerProxy.ts b/packages/dataverse/src/PointerProxy.ts index fefda1a..56d57de 100644 --- a/packages/dataverse/src/PointerProxy.ts +++ b/packages/dataverse/src/PointerProxy.ts @@ -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 - implements IdentityDerivationProvider + implements IdentityPrismProvider { /** * @internal */ - readonly $$isIdentityDerivationProvider = true + readonly $$isIdentityPrismProvider = true private readonly _currentPointerBox: IBox> /** * Convenience pointer pointing to the root of this PointerProxy. @@ -48,7 +48,7 @@ export default class PointerProxy * * @param path - The path to create the derivation at. */ - getIdentityDerivation(path: Array) { + getIdentityPrism(path: Array) { return prism(() => { const currentPointer = this._currentPointerBox.derivation.getValue() const subPointer = path.reduce( diff --git a/packages/dataverse/src/index.ts b/packages/dataverse/src/index.ts index e2db52b..ab5b120 100644 --- a/packages/dataverse/src/index.ts +++ b/packages/dataverse/src/index.ts @@ -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' diff --git a/theatre/core/src/sheetObjects/SheetObject.ts b/theatre/core/src/sheetObjects/SheetObject.ts index 296085d..25d09af 100644 --- a/theatre/core/src/sheetObjects/SheetObject.ts +++ b/theatre/core/src/sheetObjects/SheetObject.ts @@ -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({}) @@ -215,7 +211,7 @@ export default class SheetObject implements IdentityDerivationProvider { ) as SerializableValue as T } - getIdentityDerivation(path: Array): Prism { + getIdentityPrism(path: Array): Prism { /** * @remarks * TODO perf: Too much indirection here.