Unify Derivation and Prism 4/n

This commit is contained in:
Aria Minaei 2022-12-01 14:26:17 +01:00
parent 06808f99e9
commit a38d96ec95
10 changed files with 26 additions and 26 deletions

View file

@ -266,7 +266,7 @@ const identityDerivationWeakMap = new WeakMap<{}, Prism<unknown>>()
* *
* @param pointer - The pointer to return the derivation at. * @param pointer - The pointer to return the derivation at.
*/ */
export const valueDerivation = <P extends PointerType<$IntentionalAny>>( export const pointerToPrism = <P extends PointerType<$IntentionalAny>>(
pointer: P, pointer: P,
): Prism<P extends PointerType<infer T> ? T : void> => { ): Prism<P extends PointerType<infer T> ? T : void> => {
const meta = getPointerMeta(pointer) const meta = getPointerMeta(pointer)
@ -276,7 +276,7 @@ export const valueDerivation = <P extends PointerType<$IntentionalAny>>(
const root = meta.root const root = meta.root
if (!isIdentityDerivationProvider(root)) { if (!isIdentityDerivationProvider(root)) {
throw new Error( throw new Error(
`Cannot run valueDerivation() on a pointer whose root is not an IdentityChangeProvider`, `Cannot run pointerToPrism() on a pointer whose root is not an IdentityChangeProvider`,
) )
} }
const {path} = meta const {path} = meta
@ -322,7 +322,7 @@ export const val = <
? P ? P
: unknown => { : unknown => {
if (isPointer(input)) { if (isPointer(input)) {
return valueDerivation(input).getValue() as $IntentionalAny return pointerToPrism(input).getValue() as $IntentionalAny
} else if (isPrism(input)) { } else if (isPrism(input)) {
return input.getValue() as $IntentionalAny return input.getValue() as $IntentionalAny
} else { } else {

View file

@ -26,7 +26,7 @@ export default class PointerProxy<O extends {}>
* Convenience pointer pointing to the root of this PointerProxy. * Convenience pointer pointing to the root of this PointerProxy.
* *
* @remarks * @remarks
* Allows convenient use of {@link valueDerivation} and {@link val}. * Allows convenient use of {@link pointerToPrism} and {@link val}.
*/ */
readonly pointer: Pointer<O> readonly pointer: Pointer<O>

View file

@ -1,4 +1,4 @@
import {valueDerivation} from '../Atom' import {pointerToPrism} from '../Atom'
import type {Pointer} from '../pointer' import type {Pointer} from '../pointer'
import {isPointer} from '../pointer' import {isPointer} from '../pointer'
import type {Prism} from './Interface' import type {Prism} from './Interface'
@ -9,7 +9,7 @@ export default function* iterateAndCountTicks<V>(
): Generator<{value: V; ticks: number}, void, void> { ): Generator<{value: V; ticks: number}, void, void> {
let d let d
if (isPointer(pointerOrDerivation)) { if (isPointer(pointerOrDerivation)) {
d = valueDerivation(pointerOrDerivation) as Prism<V> d = pointerToPrism(pointerOrDerivation) as Prism<V>
} else if (isPrism(pointerOrDerivation)) { } else if (isPrism(pointerOrDerivation)) {
d = pointerOrDerivation d = pointerOrDerivation
} else { } else {

View file

@ -1,4 +1,4 @@
import {valueDerivation} from '../Atom' import {pointerToPrism} from '../Atom'
import type {Pointer} from '../pointer' import type {Pointer} from '../pointer'
import {isPointer} from '../pointer' import {isPointer} from '../pointer'
import Ticker from '../Ticker' import Ticker from '../Ticker'
@ -10,7 +10,7 @@ export default function* iterateOver<V>(
): Generator<V, void, void> { ): Generator<V, void, void> {
let d let d
if (isPointer(pointerOrDerivation)) { if (isPointer(pointerOrDerivation)) {
d = valueDerivation(pointerOrDerivation) as Prism<V> d = pointerToPrism(pointerOrDerivation) as Prism<V>
} else if (isPrism(pointerOrDerivation)) { } else if (isPrism(pointerOrDerivation)) {
d = pointerOrDerivation d = pointerOrDerivation
} else { } else {

View file

@ -5,7 +5,7 @@
*/ */
export type {IdentityDerivationProvider} from './Atom' export type {IdentityDerivationProvider} from './Atom'
export {default as Atom, val, valueDerivation} 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'
export {isPrism} from './derivations/Interface' export {isPrism} from './derivations/Interface'

View file

@ -10,7 +10,7 @@ import userReadableTypeOfValue from '@theatre/shared/utils/userReadableTypeOfVal
import deepEqual from 'fast-deep-equal' import deepEqual from 'fast-deep-equal'
import type {PointerType} from '@theatre/dataverse' import type {PointerType} from '@theatre/dataverse'
import {isPointer} from '@theatre/dataverse' import {isPointer} from '@theatre/dataverse'
import {isPrism, valueDerivation} from '@theatre/dataverse' import {isPrism, pointerToPrism} from '@theatre/dataverse'
import type {$IntentionalAny, VoidFn} from '@theatre/shared/utils/types' import type {$IntentionalAny, VoidFn} from '@theatre/shared/utils/types'
import type {ProjectId} from '@theatre/shared/utils/ids' import type {ProjectId} from '@theatre/shared/utils/ids'
import {_coreLogger} from './_coreLogger' import {_coreLogger} from './_coreLogger'
@ -155,7 +155,7 @@ export function onChange<P extends PointerType<$IntentionalAny>>(
callback: (value: P extends PointerType<infer T> ? T : unknown) => void, callback: (value: P extends PointerType<infer T> ? T : unknown) => void,
): VoidFn { ): VoidFn {
if (isPointer(pointer)) { if (isPointer(pointer)) {
const derivation = valueDerivation(pointer) const derivation = pointerToPrism(pointer)
return derivation.onChange( return derivation.onChange(
getCoreTicker(), getCoreTicker(),
callback as $IntentionalAny, callback as $IntentionalAny,
@ -189,7 +189,7 @@ export function onChange<P extends PointerType<$IntentionalAny>>(
*/ */
export function val<T>(pointer: PointerType<T>): T { export function val<T>(pointer: PointerType<T>): T {
if (isPointer(pointer)) { if (isPointer(pointer)) {
return valueDerivation(pointer).getValue() as $IntentionalAny return pointerToPrism(pointer).getValue() as $IntentionalAny
} else { } else {
throw new Error(`Called val(p) where p is not a pointer.`) throw new Error(`Called val(p) where p is not a pointer.`)
} }

View file

@ -2,7 +2,7 @@ import Scrub from '@theatre/studio/Scrub'
import type {StudioHistoricState} from '@theatre/studio/store/types/historic' import type {StudioHistoricState} from '@theatre/studio/store/types/historic'
import type UI from '@theatre/studio/UI' import type UI from '@theatre/studio/UI'
import type {Pointer} from '@theatre/dataverse' import type {Pointer} from '@theatre/dataverse'
import {Atom, PointerProxy, valueDerivation} from '@theatre/dataverse' import {Atom, PointerProxy, pointerToPrism} from '@theatre/dataverse'
import type { import type {
CommitOrDiscard, CommitOrDiscard,
ITransactionPrivateApi, ITransactionPrivateApi,
@ -171,7 +171,7 @@ export class Studio {
} }
_attachToIncomingProjects() { _attachToIncomingProjects() {
const projectsD = valueDerivation(this.projectsP) const projectsD = pointerToPrism(this.projectsP)
const attachToProjects = (projects: Record<string, Project>) => { const attachToProjects = (projects: Record<string, Project>) => {
for (const project of Object.values(projects)) { for (const project of Object.values(projects)) {

View file

@ -9,7 +9,7 @@ import type {
} from '@theatre/studio/panels/SequenceEditorPanel/layout/tree' } from '@theatre/studio/panels/SequenceEditorPanel/layout/tree'
import {usePrism, useVal} from '@theatre/react' import {usePrism, useVal} from '@theatre/react'
import type {Prism, Pointer} from '@theatre/dataverse' import type {Prism, Pointer} from '@theatre/dataverse'
import {prism, val, valueDerivation} from '@theatre/dataverse' import {prism, val, pointerToPrism} from '@theatre/dataverse'
import React, {useMemo, Fragment} from 'react' import React, {useMemo, Fragment} from 'react'
import styled from 'styled-components' import styled from 'styled-components'
import type {IContextMenuItem} from '@theatre/studio/uiComponents/simpleContextMenu/useContextMenu' import type {IContextMenuItem} from '@theatre/studio/uiComponents/simpleContextMenu/useContextMenu'
@ -302,7 +302,7 @@ function useAggregatedKeyframeTrackContextMenu(
displayName: 'Aggregate Keyframe Track', displayName: 'Aggregate Keyframe Track',
menuItems: () => { menuItems: () => {
const selectionKeyframes = const selectionKeyframes =
valueDerivation( pointerToPrism(
getStudio()!.atomP.ahistoric.clipboard.keyframesWithRelativePaths, getStudio()!.atomP.ahistoric.clipboard.keyframesWithRelativePaths,
).getValue() ?? [] ).getValue() ?? []
@ -355,7 +355,7 @@ function pasteKeyframesSheet(
if (areKeyframesAllOnSingleTrack) { if (areKeyframesAllOnSingleTrack) {
for (const object of viewModel.children.map((child) => child.sheetObject)) { for (const object of viewModel.children.map((child) => child.sheetObject)) {
const tracksByObject = valueDerivation( const tracksByObject = pointerToPrism(
getStudio().atomP.historic.coreByProject[projectId].sheetsById[sheetId] getStudio().atomP.historic.coreByProject[projectId].sheetsById[sheetId]
.sequence.tracksByObject[object.address.objectKey], .sequence.tracksByObject[object.address.objectKey],
).getValue() ).getValue()
@ -370,7 +370,7 @@ function pasteKeyframesSheet(
) )
} }
} else { } else {
const tracksByObject = valueDerivation( const tracksByObject = pointerToPrism(
getStudio().atomP.historic.coreByProject[projectId].sheetsById[sheetId] getStudio().atomP.historic.coreByProject[projectId].sheetsById[sheetId]
.sequence.tracksByObject, .sequence.tracksByObject,
).getValue() ).getValue()
@ -418,7 +418,7 @@ function pasteKeyframesObjectOrCompound(
) { ) {
const {projectId, sheetId, objectKey} = viewModel.sheetObject.address const {projectId, sheetId, objectKey} = viewModel.sheetObject.address
const trackRecords = valueDerivation( const trackRecords = pointerToPrism(
getStudio().atomP.historic.coreByProject[projectId].sheetsById[sheetId] getStudio().atomP.historic.coreByProject[projectId].sheetsById[sheetId]
.sequence.tracksByObject[objectKey], .sequence.tracksByObject[objectKey],
).getValue() ).getValue()

View file

@ -13,7 +13,7 @@ import type {
} from '@theatre/shared/utils/ids' } from '@theatre/shared/utils/ids'
import {createStudioSheetItemKey} from '@theatre/shared/utils/ids' import {createStudioSheetItemKey} from '@theatre/shared/utils/ids'
import type {$FixMe, $IntentionalAny} from '@theatre/shared/utils/types' import type {$FixMe, $IntentionalAny} from '@theatre/shared/utils/types'
import {prism, val, valueDerivation} from '@theatre/dataverse' import {prism, val, pointerToPrism} from '@theatre/dataverse'
import logger from '@theatre/shared/logger' import logger from '@theatre/shared/logger'
import {titleBarHeight} from '@theatre/studio/panels/BasePanel/common' import {titleBarHeight} from '@theatre/studio/panels/BasePanel/common'
import type {Studio} from '@theatre/studio/Studio' import type {Studio} from '@theatre/studio/Studio'
@ -116,7 +116,7 @@ export const calculateSequenceEditorTree = (
const isCollapsedP = const isCollapsedP =
collapsableItemSetP.byId[createStudioSheetItemKey.forSheet()].isCollapsed collapsableItemSetP.byId[createStudioSheetItemKey.forSheet()].isCollapsed
const isCollapsed = valueDerivation(isCollapsedP).getValue() ?? false const isCollapsed = pointerToPrism(isCollapsedP).getValue() ?? false
const tree: SequenceEditorTree = { const tree: SequenceEditorTree = {
type: 'sheet', type: 'sheet',
@ -165,7 +165,7 @@ export const calculateSequenceEditorTree = (
collapsableItemSetP.byId[ collapsableItemSetP.byId[
createStudioSheetItemKey.forSheetObject(sheetObject) createStudioSheetItemKey.forSheetObject(sheetObject)
].isCollapsed ].isCollapsed
const isCollapsed = valueDerivation(isCollapsedP).getValue() ?? false const isCollapsed = pointerToPrism(isCollapsedP).getValue() ?? false
const row: SequenceEditorTree_SheetObject = { const row: SequenceEditorTree_SheetObject = {
type: 'sheetObject', type: 'sheetObject',
@ -284,7 +284,7 @@ export const calculateSequenceEditorTree = (
collapsableItemSetP.byId[ collapsableItemSetP.byId[
createStudioSheetItemKey.forSheetObjectProp(sheetObject, pathToProp) createStudioSheetItemKey.forSheetObjectProp(sheetObject, pathToProp)
].isCollapsed ].isCollapsed
const isCollapsed = valueDerivation(isCollapsedP).getValue() ?? false const isCollapsed = pointerToPrism(isCollapsedP).getValue() ?? false
const row: SequenceEditorTree_PropWithChildren = { const row: SequenceEditorTree_PropWithChildren = {
type: 'propWithChildren', type: 'propWithChildren',

View file

@ -3,7 +3,7 @@ import type {StrictRecord} from '@theatre/shared/utils/types'
import React, {useMemo} from 'react' import React, {useMemo} from 'react'
import {useEffect} from 'react' import {useEffect} from 'react'
import {useLogger} from './useLogger' import {useLogger} from './useLogger'
import {Box, prism, valueDerivation} from '@theatre/dataverse' import {Box, prism, pointerToPrism} from '@theatre/dataverse'
import {Atom} from '@theatre/dataverse' import {Atom} from '@theatre/dataverse'
import {useDerivation} from '@theatre/react' import {useDerivation} from '@theatre/react'
import {selectClosestHTMLAncestor} from '@theatre/studio/utils/selectClosestHTMLAncestor' import {selectClosestHTMLAncestor} from '@theatre/studio/utils/selectClosestHTMLAncestor'
@ -69,10 +69,10 @@ function createPresenceContext(options: {
if (!itemKey) return undefinedD if (!itemKey) return undefinedD
// this is the thing being hovered // this is the thing being hovered
const currentD = currentUserHoverItemB.derivation const currentD = currentUserHoverItemB.derivation
const primaryFocusDer = valueDerivation( const primaryFocusDer = pointerToPrism(
currentUserHoverFlagItemsAtom.pointer[itemKey], currentUserHoverFlagItemsAtom.pointer[itemKey],
) )
const relationsDer = valueDerivation(relationsAtom.pointer[itemKey]) const relationsDer = pointerToPrism(relationsAtom.pointer[itemKey])
return prism(() => { return prism(() => {
const primary = primaryFocusDer.getValue() const primary = primaryFocusDer.getValue()
if (primary) { if (primary) {