Unify Derivation and Prism 7/n

This commit is contained in:
Aria Minaei 2022-12-01 14:41:46 +01:00
parent 859cb40e0f
commit acf34d393d
25 changed files with 134 additions and 141 deletions

View file

@ -155,17 +155,13 @@ export function onChange<P extends PointerType<$IntentionalAny>>(
callback: (value: P extends PointerType<infer T> ? T : unknown) => void,
): VoidFn {
if (isPointer(pointer)) {
const derivation = pointerToPrism(pointer)
return derivation.onChange(
getCoreTicker(),
callback as $IntentionalAny,
true,
)
const pr = pointerToPrism(pointer)
return pr.onChange(getCoreTicker(), callback as $IntentionalAny, true)
} else if (isPrism(pointer)) {
return pointer.onChange(getCoreTicker(), callback as $IntentionalAny, true)
} else {
throw new Error(
`Called onChange(p) where p is neither a pointer nor a derivation.`,
`Called onChange(p) where p is neither a pointer nor a prism.`,
)
}
}

View file

@ -7,7 +7,7 @@ import globals from '@theatre/shared/globals'
/**
* @remarks
* TODO this could be turned into a simple derivation, like:
* TODO this could be turned into a simple prism, like:
* `editor.isReady: Prism<{isReady: true} | {isReady: false, reason: 'conflictBetweenDiskStateAndBrowserState'}>`
*/
export default async function initialiseProjectState(

View file

@ -67,7 +67,7 @@ export default class Sequence {
)
this._statePointerDerivation = prism(
() => this._playbackControllerBox.derivation.getValue().statePointer,
() => this._playbackControllerBox.prism.getValue().statePointer,
)
this._positionD = prism(() => {
@ -189,7 +189,7 @@ export default class Sequence {
* @remarks
* One use case for this is to play the playback within the focus range.
*
* @param rangeD - The derivation that contains the range that will be used for the playback
* @param rangeD - The prism that contains the range that will be used for the playback
*
* @returns a promise that gets rejected if the playback stopped for whatever reason
*

View file

@ -31,7 +31,7 @@ export interface IPlaybackController {
* @remarks
* One use case for this is to play the playback within the focus range.
*
* @param rangeD - The derivation that contains the range that will be used for the playback
* @param rangeD - The prism that contains the range that will be used for the playback
*
* @returns a promise that gets rejected if the playback stopped for whatever reason
*

View file

@ -70,8 +70,8 @@ export default class SheetObject implements IdentityPrismProvider {
}
getValues(): Prism<Pointer<SheetObjectPropsValue>> {
// Cache the derivation because only one is needed per SheetObject.
// Also, if `onValuesChange()` is unsubscribed from, this derivation will go cold
// Cache the prism because only one is needed per SheetObject.
// Also, if `onValuesChange()` is unsubscribed from, this prism will go cold
// and free its resources. So it's no problem to still keep it on the cache.
return this._cache.get('getValues()', () =>
prism(() => {
@ -107,7 +107,7 @@ export default class SheetObject implements IdentityPrismProvider {
/**
* The lowest layer is the default value of the root prop. Since an object's config
* _could_ change, we read it as a derivation. Otherwise, we could have just `getDefaultsOfPropTypeConfig(this.template.staticConfig)`.
* _could_ change, we read it as a prism. Otherwise, we could have just `getDefaultsOfPropTypeConfig(this.template.staticConfig)`.
*
* Note: If studio is not present, there is no known use-case for the config of an object to change on the fly, so
* we could read this value statically.
@ -168,7 +168,7 @@ export default class SheetObject implements IdentityPrismProvider {
let sequenced
{
// NOTE: we're reading the sequenced values as a derivation to a pointer. This should be refactored
// NOTE: we're reading the sequenced values as a prism to a pointer. This should be refactored
// to a simple pointer.
const pointerToSequencedValuesD = prism.memo(
'seq',
@ -185,7 +185,7 @@ export default class SheetObject implements IdentityPrismProvider {
)
// read the sequenced values
// (val(val(x))) unwraps the pointer and the derivation
// (val(val(x))) unwraps the pointer and the prism
sequenced = val(val(pointerToSequencedValuesD))
// deep-merge the sequenced values with the previous layer
@ -243,7 +243,7 @@ export default class SheetObject implements IdentityPrismProvider {
const untaps: Array<() => void> = []
for (const {trackId, pathToProp} of tracksToProcess) {
const derivation = this._trackIdToDerivation(trackId)
const pr = this._trackIdToPrism(trackId)
const propConfig = getPropConfigByPath(
config,
pathToProp,
@ -254,7 +254,7 @@ export default class SheetObject implements IdentityPrismProvider {
propConfig.interpolate! as Interpolator<$IntentionalAny>
const updateSequenceValueFromItsDerivation = () => {
const triple = derivation.getValue()
const triple = pr.getValue()
if (!triple) return valsAtom.setIn(pathToProp, undefined)
@ -279,9 +279,7 @@ export default class SheetObject implements IdentityPrismProvider {
interpolate(left, right, triple.progression),
)
}
const untap = derivation.onStale(
updateSequenceValueFromItsDerivation,
)
const untap = pr.onStale(updateSequenceValueFromItsDerivation)
updateSequenceValueFromItsDerivation()
untaps.push(untap)
@ -299,7 +297,7 @@ export default class SheetObject implements IdentityPrismProvider {
})
}
protected _trackIdToDerivation(
protected _trackIdToPrism(
trackId: SequenceTrackId,
): Prism<InterpolationTriple | undefined> {
const trackP =

View file

@ -147,7 +147,7 @@ export default class TheatreSheetObject<
return {...privateAPI(this).address}
}
private _valuesDerivation(): Prism<this['value']> {
private _valuesPrism(): Prism<this['value']> {
return this._cache.get('onValuesChangeDerivation', () => {
const sheetObject = privateAPI(this)
const d: Prism<PropsValue<Props>> = prism(() => {
@ -158,15 +158,15 @@ export default class TheatreSheetObject<
}
onValuesChange(fn: (values: this['value']) => void): VoidFn {
return this._valuesDerivation().onChange(getCoreTicker(), fn, true)
return this._valuesPrism().onChange(getCoreTicker(), fn, true)
}
// internal: Make the deviration keepHot if directly read
get value(): PropsValue<Props> {
const der = this._valuesDerivation()
const der = this._valuesPrism()
if (KEEP_HOT_FOR_MS != null) {
if (!der.isHot) {
// derivation not hot, so keep it hot and set up `_keepHotUntapDebounce`
// prism not hot, so keep it hot and set up `_keepHotUntapDebounce`
if (this._keepHotUntapDebounce != null) {
// defensive checks
if (process.env.NODE_ENV === 'development') {

View file

@ -54,7 +54,7 @@ export default function useKeyboardShortcuts() {
const {projectId, sheetId} = seq.address
/*
* The value of this derivation is an array that contains the
* The value of this prism is an array that contains the
* range of the playback (start and end), and a boolean that is
* `true` if the playback should be played within that range.
*/
@ -162,7 +162,7 @@ const getPlaybackStateBox = memoizeFn(
)
/*
* A memoized function that returns a derivation with a boolean value.
* A memoized function that returns a prism with a boolean value.
* This value is set to `true` if:
* 1. the playback is playing and using the focus range instead of the whole sequence
* 2. the playback is stopped, but would use the focus range if it were started.
@ -171,7 +171,7 @@ export const getIsPlayheadAttachedToFocusRange = memoizeFn(
(sequence: Sequence) =>
prism<boolean>(() => {
const controlledPlaybackState =
getPlaybackStateBox(sequence).derivation.getValue()
getPlaybackStateBox(sequence).prism.getValue()
if (controlledPlaybackState) {
return controlledPlaybackState.getValue().isFollowingARange
} else {

View file

@ -199,8 +199,8 @@ const isDetailPanelHotspotActiveB = new Box<boolean>(false)
const isDetailPanelHoveredB = new Box<boolean>(false)
export const shouldShowDetailD = prism<boolean>(() => {
const isHovered = val(isDetailPanelHoveredB.derivation)
const isHotspotActive = val(isDetailPanelHotspotActiveB.derivation)
const isHovered = val(isDetailPanelHoveredB.prism)
const isHotspotActive = val(isDetailPanelHotspotActiveB.prism)
return isHovered || isHotspotActive
})

View file

@ -83,8 +83,8 @@ const isOutlinePanelHotspotActiveB = new Box<boolean>(false)
const isOutlinePanelHoveredB = new Box<boolean>(false)
export const shouldShowOutlineD = prism<boolean>(() => {
const isHovered = val(isOutlinePanelHoveredB.derivation)
const isHotspotActive = val(isOutlinePanelHotspotActiveB.derivation)
const isHovered = val(isOutlinePanelHoveredB.prism)
const isHotspotActive = val(isOutlinePanelHotspotActiveB.prism)
return isHovered || isHotspotActive
})

View file

@ -545,12 +545,12 @@ const {isCurveEditorOpenD, isConnectionEditingInCurvePopover, getLock} =
}
},
isCurveEditorOpenD: prism(() => {
return connectionsInCurvePopoverEdit.derivation.getValue().length > 0
return connectionsInCurvePopoverEdit.prism.getValue().length > 0
}),
// must be run in a prism
isConnectionEditingInCurvePopover(con: KeyframeConnectionWithAddress) {
prism.ensurePrism()
return connectionsInCurvePopoverEdit.derivation
return connectionsInCurvePopoverEdit.prism
.getValue()
.some(
({left, right}) =>

View file

@ -78,7 +78,7 @@ const stateB = new Box<
}
>({mode: 'snapToNone'})
export const snapPositionsStateD = stateB.derivation
export const snapPositionsStateD = stateB.prism
export function snapToAll() {
stateB.set({mode: 'snapToAll'})

View file

@ -40,7 +40,7 @@ const ExtensionToolsetRender: React.FC<{
if (typeof detach === 'function') return detach
}, [extension, toolbarId])
const config = useVal(toolsetConfigBox.derivation)
const config = useVal(toolsetConfigBox.prism)
return <Toolset config={config} />
}

View file

@ -41,7 +41,7 @@ export const useTooltipOpenState = (): [
const TooltipContext: React.FC<{}> = ({children}) => {
const currentTooltipId = useMemo(() => new Box(-1), [])
const cur = currentTooltipId.derivation
const cur = currentTooltipId.prism
const set = useMemo(() => {
let lastTimeout: NodeJS.Timeout | undefined = undefined

View file

@ -68,7 +68,7 @@ function createPresenceContext(options: {
const focusD = useMemo(() => {
if (!itemKey) return undefinedD
// this is the thing being hovered
const currentD = currentUserHoverItemB.derivation
const currentD = currentUserHoverItemB.prism
const primaryFocusDer = pointerToPrism(
currentUserHoverFlagItemsAtom.pointer[itemKey],
)

View file

@ -1,7 +1,7 @@
import {prism} from '@theatre/dataverse'
/**
* A derivation that holds the current mouse position.
* A prism that holds the current mouse position.
*/
const mousePositionD = prism(() => {
const [pos, setPos] = prism.state<MouseEvent | null>('pos', null)