Unify Derivation and Prism 11/n

`useDerivation()` => `usePrismInstance()`
This commit is contained in:
Aria Minaei 2022-12-01 14:59:03 +01:00
parent 1236900ddf
commit b2116e9a5d
10 changed files with 51 additions and 51 deletions

View file

@ -8,7 +8,7 @@ import type {$FixMe, $IntentionalAny} from './types'
import prism from './prisms/prism/prism'
/**
* Allows creating pointer-derivations where the pointer can be switched out.
* Allows creating pointer-prisms where the pointer can be switched out.
*
* @remarks
* This allows reacting not just to value changes at a certain pointer, but changes

View file

@ -144,7 +144,7 @@ export const getPointerParts = <_>(
* Creates a pointer to a (nested) property of an {@link Atom}.
*
* @remarks
* Pointers are used to make derivations of properties or nested properties of
* Pointers are used to make prisms of properties or nested properties of
* {@link Atom|Atoms}.
*
* Pointers also allow easy construction of new pointers pointing to nested members

View file

@ -5,15 +5,15 @@ import type {Prism} from './Interface'
import {isPrism} from './Interface'
export default function* iterateAndCountTicks<V>(
pointerOrDerivation: Prism<V> | Pointer<V>,
pointerOrPrism: Prism<V> | Pointer<V>,
): Generator<{value: V; ticks: number}, void, void> {
let d
if (isPointer(pointerOrDerivation)) {
d = pointerToPrism(pointerOrDerivation) as Prism<V>
} else if (isPrism(pointerOrDerivation)) {
d = pointerOrDerivation
if (isPointer(pointerOrPrism)) {
d = pointerToPrism(pointerOrPrism) as Prism<V>
} else if (isPrism(pointerOrPrism)) {
d = pointerOrPrism
} else {
throw new Error(`Only pointers and derivations are supported`)
throw new Error(`Only pointers and prisms are supported`)
}
let ticksCountedSinceLastYield = 0

View file

@ -6,15 +6,15 @@ import type {Prism} from './Interface'
import {isPrism} from './Interface'
export default function* iterateOver<V>(
pointerOrDerivation: Prism<V> | Pointer<V>,
pointerOrPrism: Prism<V> | Pointer<V>,
): Generator<V, void, void> {
let d
if (isPointer(pointerOrDerivation)) {
d = pointerToPrism(pointerOrDerivation) as Prism<V>
} else if (isPrism(pointerOrDerivation)) {
d = pointerOrDerivation
if (isPointer(pointerOrPrism)) {
d = pointerToPrism(pointerOrPrism) as Prism<V>
} else if (isPrism(pointerOrPrism)) {
d = pointerOrPrism
} else {
throw new Error(`Only pointers and derivations are supported`)
throw new Error(`Only pointers and prisms are supported`)
}
const ticker = new Ticker()

View file

@ -96,7 +96,7 @@ describe('prism', () => {
const prsm = prism(() => {
const n = val(a.pointer.letter)
const iterationAtTimeOfCall = iteration
sequence.push({derivationCall: iterationAtTimeOfCall})
sequence.push({prismCall: iterationAtTimeOfCall})
prism.effect(
'f',
@ -116,13 +116,13 @@ describe('prism', () => {
sequence.push({change})
})
expect(sequence).toMatchObject([{derivationCall: 0}, {effectCall: 0}])
expect(sequence).toMatchObject([{prismCall: 0}, {effectCall: 0}])
sequence.length = 0
iteration++
a.setIn(['letter'], 'b')
ticker.tick()
expect(sequence).toMatchObject([{derivationCall: 1}, {change: 'b'}])
expect(sequence).toMatchObject([{prismCall: 1}, {change: 'b'}])
sequence.length = 0
deps = [1]
@ -130,7 +130,7 @@ describe('prism', () => {
a.setIn(['letter'], 'c')
ticker.tick()
expect(sequence).toMatchObject([
{derivationCall: 2},
{prismCall: 2},
{cleanupCall: 0},
{effectCall: 2},
{change: 'c'},
@ -156,7 +156,7 @@ describe('prism', () => {
const prsm = prism(() => {
const n = val(a.pointer.letter)
const iterationAtTimeOfCall = iteration
sequence.push({derivationCall: iterationAtTimeOfCall})
sequence.push({prismCall: iterationAtTimeOfCall})
const resultOfMemo = prism.memo(
'memo',
@ -177,7 +177,7 @@ describe('prism', () => {
})
expect(sequence).toMatchObject([
{derivationCall: 0},
{prismCall: 0},
{memoCall: 0},
{resultOfMemo: 0},
])
@ -187,7 +187,7 @@ describe('prism', () => {
a.setIn(['letter'], 'b')
ticker.tick()
expect(sequence).toMatchObject([
{derivationCall: 1},
{prismCall: 1},
{resultOfMemo: 0},
{change: 'b'},
])
@ -198,7 +198,7 @@ describe('prism', () => {
a.setIn(['letter'], 'c')
ticker.tick()
expect(sequence).toMatchObject([
{derivationCall: 2},
{prismCall: 2},
{memoCall: 2},
{resultOfMemo: 2},
{change: 'c'},

View file

@ -51,7 +51,7 @@ class HotHandle<V> {
constructor(
private readonly _fn: () => V,
private readonly _prismInstance: PrismDerivation<V>,
private readonly _prismInstance: PrismInstance<V>,
) {
for (const d of this._dependencies) {
d._addDependent(this._reactToDependencyGoingStale)
@ -198,7 +198,7 @@ class HotHandle<V> {
const emptyObject = {}
class PrismDerivation<V> implements Prism<V> {
class PrismInstance<V> implements Prism<V> {
/**
* Whether the object is a prism.
*/
@ -336,8 +336,8 @@ class PrismDerivation<V> implements Prism<V> {
* Design constraints:
* - This fix should not have a perf-penalty in production. Perhaps use a global flag + `process.env.NODE_ENV !== 'production'`
* to enable it.
* - In the case of `DerivationValuelessEmitter`, we don't control when the user calls
* `getValue()` (as opposed to `DerivationEmitter` which calls `getValue()` directly).
* - In the case of `onStale()`, we don't control when the user calls
* `getValue()` (as opposed to `onChange()` which calls `getValue()` directly).
* Perhaps we can disable the check in that case.
* - Probably the best place to add this check is right here in this method plus some changes to `reportResulutionStart()`,
* which would have to be changed to let the caller know if there is an actual collector (a prism)
@ -700,7 +700,7 @@ function inPrism(): boolean {
return !!hookScopeStack.peek()
}
const possibleDerivationToValue = <P extends Prism<$IntentionalAny> | unknown>(
const possiblePrismToValue = <P extends Prism<$IntentionalAny> | unknown>(
input: P,
): P extends Prism<infer T> ? T : P => {
if (isPrism(input)) {
@ -742,7 +742,7 @@ type IPrismFn = {
* @param fn - The function to rerun when the prisms referenced in it change.
*/
const prism: IPrismFn = (fn) => {
return new PrismDerivation(fn)
return new PrismInstance(fn)
}
class ColdScope implements PrismScope {