Unify Derivation and Prism (1/n)

This commit is contained in:
Aria Minaei 2022-12-01 14:20:50 +01:00
parent e9bbb0ef41
commit 12b3f477bc
28 changed files with 109 additions and 116 deletions

View file

@ -1,7 +1,7 @@
import get from 'lodash-es/get'
import isPlainObject from 'lodash-es/isPlainObject'
import last from 'lodash-es/last'
import type {IDerivation} from './derivations/IDerivation'
import type {Prism} from './derivations/IDerivation'
import {isDerivation} from './derivations/IDerivation'
import type {Pointer, PointerType} from './pointer'
import {isPointer} from './pointer'
@ -34,7 +34,7 @@ export interface IdentityDerivationProvider {
*
* @param path - The path to create the derivation at.
*/
getIdentityDerivation(path: Array<string | number>): IDerivation<unknown>
getIdentityDerivation(path: Array<string | number>): Prism<unknown>
}
const getTypeOfValue = (v: unknown): ValueTypes => {
@ -246,7 +246,7 @@ export default class Atom<State extends {}>
*
* @param path - The path to create the derivation at.
*/
getIdentityDerivation(path: Array<string | number>): IDerivation<unknown> {
getIdentityDerivation(path: Array<string | number>): Prism<unknown> {
const subscribe = (listener: (val: unknown) => void) =>
this._onPathValueChange(path, listener)
@ -258,7 +258,7 @@ export default class Atom<State extends {}>
}
}
const identityDerivationWeakMap = new WeakMap<{}, IDerivation<unknown>>()
const identityDerivationWeakMap = new WeakMap<{}, Prism<unknown>>()
/**
* Returns a derivation of the value at the provided pointer. Derivations are
@ -268,7 +268,7 @@ const identityDerivationWeakMap = new WeakMap<{}, IDerivation<unknown>>()
*/
export const valueDerivation = <P extends PointerType<$IntentionalAny>>(
pointer: P,
): IDerivation<P extends PointerType<infer T> ? T : void> => {
): Prism<P extends PointerType<infer T> ? T : void> => {
const meta = getPointerMeta(pointer)
let derivation = identityDerivationWeakMap.get(meta)
@ -309,14 +309,14 @@ function isIdentityDerivationProvider(
export const val = <
P extends
| PointerType<$IntentionalAny>
| IDerivation<$IntentionalAny>
| Prism<$IntentionalAny>
| undefined
| null,
>(
input: P,
): P extends PointerType<infer T>
? T
: P extends IDerivation<infer T>
: P extends Prism<infer T>
? T
: P extends undefined | null
? P

View file

@ -1,4 +1,4 @@
import type {IDerivation} from './derivations/IDerivation'
import type {Prism} from './derivations/IDerivation'
import prism from './derivations/prism/prism'
import Emitter from './utils/Emitter'
@ -27,7 +27,7 @@ export interface IBox<V> {
/**
* Creates a derivation of the Box that you can use to track changes to it.
*/
derivation: IDerivation<V>
derivation: Prism<V>
}
/**
@ -39,7 +39,7 @@ export interface IBox<V> {
* reference even if the objects are structurally equal.
*/
export default class Box<V> implements IBox<V> {
private _publicDerivation: IDerivation<V>
private _publicDerivation: Prism<V>
private _emitter = new Emitter<V>()
/**

View file

@ -1,12 +1,12 @@
import type Ticker from '../Ticker'
import type {$IntentionalAny, VoidFn} from '../types'
type IDependent = (msgComingFrom: IDerivation<$IntentionalAny>) => void
type IDependent = (msgComingFrom: Prism<$IntentionalAny>) => void
/**
* Common interface for derivations.
*/
export interface IDerivation<V> {
export interface Prism<V> {
/**
* Whether the object is a derivation.
*/
@ -63,6 +63,6 @@ export interface IDerivation<V> {
/**
* Returns whether `d` is a derivation.
*/
export function isDerivation(d: any): d is IDerivation<unknown> {
export function isDerivation(d: any): d is Prism<unknown> {
return d && d.isDerivation && d.isDerivation === true
}

View file

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

View file

@ -2,15 +2,15 @@ import {valueDerivation} from '../Atom'
import type {Pointer} from '../pointer'
import {isPointer} from '../pointer'
import Ticker from '../Ticker'
import type {IDerivation} from './IDerivation'
import type {Prism} from './IDerivation'
import {isDerivation} from './IDerivation'
export default function* iterateOver<V>(
pointerOrDerivation: IDerivation<V> | Pointer<V>,
pointerOrDerivation: Prism<V> | Pointer<V>,
): Generator<V, void, void> {
let d
if (isPointer(pointerOrDerivation)) {
d = valueDerivation(pointerOrDerivation) as IDerivation<V>
d = valueDerivation(pointerOrDerivation) as Prism<V>
} else if (isDerivation(pointerOrDerivation)) {
d = pointerOrDerivation
} else {

View file

@ -1,6 +1,6 @@
import type {$IntentionalAny} from '../../types'
import Stack from '../../utils/Stack'
import type {IDerivation} from '../IDerivation'
import type {Prism} from '../IDerivation'
function createMechanism() {
const noop = () => {}
@ -8,7 +8,7 @@ function createMechanism() {
const stack = new Stack<Collector>()
const noopCollector: Collector = noop
type Collector = (d: IDerivation<$IntentionalAny>) => void
type Collector = (d: Prism<$IntentionalAny>) => void
const pushCollector = (collector: Collector): void => {
stack.push(collector)
@ -36,7 +36,7 @@ function createMechanism() {
}
}
const reportResolutionStart = (d: IDerivation<$IntentionalAny>) => {
const reportResolutionStart = (d: Prism<$IntentionalAny>) => {
const possibleCollector = stack.peek()
if (possibleCollector) {
possibleCollector(d)
@ -45,7 +45,7 @@ function createMechanism() {
stack.push(noopCollector)
}
const reportResolutionEnd = (_d: IDerivation<$IntentionalAny>) => {
const reportResolutionEnd = (_d: Prism<$IntentionalAny>) => {
stack.pop()
}

View file

@ -1,7 +1,7 @@
import type Ticker from '../../Ticker'
import type {$IntentionalAny, VoidFn} from '../../types'
import Stack from '../../utils/Stack'
import type {IDerivation} from '../IDerivation'
import type {Prism} from '../IDerivation'
import {isDerivation} from '../IDerivation'
import {
startIgnoringDependencies,
@ -12,15 +12,14 @@ import {
reportResolutionEnd,
} from './discoveryMechanism'
type IDependent = (msgComingFrom: IDerivation<$IntentionalAny>) => void
type IDependent = (msgComingFrom: Prism<$IntentionalAny>) => void
const voidFn = () => {}
class HotHandle<V> {
private _didMarkDependentsAsStale: boolean = false
private _isFresh: boolean = false
protected _cacheOfDendencyValues: Map<IDerivation<unknown>, unknown> =
new Map()
protected _cacheOfDendencyValues: Map<Prism<unknown>, unknown> = new Map()
/**
* @internal
@ -30,9 +29,9 @@ class HotHandle<V> {
/**
* @internal
*/
protected _dependencies: Set<IDerivation<$IntentionalAny>> = new Set()
protected _dependencies: Set<Prism<$IntentionalAny>> = new Set()
protected _possiblyStaleDeps = new Set<IDerivation<unknown>>()
protected _possiblyStaleDeps = new Set<Prism<unknown>>()
private _scope: HotScope = new HotScope(
this as $IntentionalAny as HotHandle<unknown>,
@ -112,10 +111,10 @@ class HotHandle<V> {
}
}
const newDeps: Set<IDerivation<unknown>> = new Set()
const newDeps: Set<Prism<unknown>> = new Set()
this._cacheOfDendencyValues.clear()
const collector = (observedDep: IDerivation<unknown>): void => {
const collector = (observedDep: Prism<unknown>): void => {
newDeps.add(observedDep)
this._addDependency(observedDep)
}
@ -161,9 +160,7 @@ class HotHandle<V> {
this._markAsStale()
}
protected _reactToDependencyGoingStale = (
which: IDerivation<$IntentionalAny>,
) => {
protected _reactToDependencyGoingStale = (which: Prism<$IntentionalAny>) => {
this._possiblyStaleDeps.add(which)
this._markAsStale()
@ -183,7 +180,7 @@ class HotHandle<V> {
/**
* @internal
*/
protected _addDependency(d: IDerivation<$IntentionalAny>) {
protected _addDependency(d: Prism<$IntentionalAny>) {
if (this._dependencies.has(d)) return
this._dependencies.add(d)
d._addDependent(this._reactToDependencyGoingStale)
@ -192,7 +189,7 @@ class HotHandle<V> {
/**
* @internal
*/
protected _removeDependency(d: IDerivation<$IntentionalAny>) {
protected _removeDependency(d: Prism<$IntentionalAny>) {
if (!this._dependencies.has(d)) return
this._dependencies.delete(d)
d._removeDependent(this._reactToDependencyGoingStale)
@ -201,7 +198,7 @@ class HotHandle<V> {
const emptyObject = {}
class PrismDerivation<V> implements IDerivation<V> {
class PrismDerivation<V> implements Prism<V> {
/**
* Whether the object is a derivation.
*/
@ -703,11 +700,9 @@ function inPrism(): boolean {
return !!hookScopeStack.peek()
}
const possibleDerivationToValue = <
P extends IDerivation<$IntentionalAny> | unknown,
>(
const possibleDerivationToValue = <P extends Prism<$IntentionalAny> | unknown>(
input: P,
): P extends IDerivation<infer T> ? T : P => {
): P extends Prism<infer T> ? T : P => {
if (isDerivation(input)) {
return input.getValue() as $IntentionalAny
} else {
@ -728,7 +723,7 @@ function source<V>(
}
type IPrismFn = {
<T>(fn: () => T): IDerivation<T>
<T>(fn: () => T): Prism<T>
ref: typeof ref
effect: typeof effect
memo: typeof memo

View file

@ -9,7 +9,7 @@ export {default as Atom, val, valueDerivation} from './Atom'
export {default as Box} from './Box'
export type {IBox} from './Box'
export {isDerivation} from './derivations/IDerivation'
export type {IDerivation} from './derivations/IDerivation'
export type {Prism} from './derivations/IDerivation'
export {default as iterateAndCountTicks} from './derivations/iterateAndCountTicks'
export {default as iterateOver} from './derivations/iterateOver'
export {default as prism} from './derivations/prism/prism'

View file

@ -4,7 +4,7 @@
* @packageDocumentation
*/
import type {IDerivation} from '@theatre/dataverse'
import type {Prism} from '@theatre/dataverse'
import {Box} from '@theatre/dataverse'
import {prism, val} from '@theatre/dataverse'
import {findIndex} from 'lodash-es'
@ -144,7 +144,7 @@ type QueueItem<T = unknown> = {
/**
* A reference to the derivation
*/
der: IDerivation<T>
der: Prism<T>
/**
* The last value of this derivation.
*/
@ -303,7 +303,7 @@ function queueIfNeeded() {
* On the off-chance that one of them still turns out to be a zombile child, `runQueue` will defer that particular
* `useDerivation()` to be read inside a normal react render phase.
*/
export function useDerivation<T>(der: IDerivation<T>, debugLabel?: string): T {
export function useDerivation<T>(der: Prism<T>, debugLabel?: string): T {
const _forceUpdate = useForceUpdate(debugLabel)
const ref = useRef<QueueItem<T>>(undefined as $IntentionalAny)
@ -386,7 +386,7 @@ export function useDerivation<T>(der: IDerivation<T>, debugLabel?: string): T {
export function usePrismWithoutReRender<T>(
fn: () => T,
deps: unknown[],
): IDerivation<T> {
): Prism<T> {
const derivation = useMemo(() => prism(fn), deps)
return useDerivationWithoutReRender(derivation)
@ -398,9 +398,7 @@ export function usePrismWithoutReRender<T>(
* return the value of the derivation, and it does not
* re-render the component if the value of the derivation changes.
*/
export function useDerivationWithoutReRender<T>(
der: IDerivation<T>,
): IDerivation<T> {
export function useDerivationWithoutReRender<T>(der: Prism<T>): Prism<T> {
useEffect(() => {
const untap = der.keepHot()