Minor change to discoveryMechanism's api

This commit is contained in:
Aria Minaei 2021-07-07 11:50:23 +02:00
parent a4f83e0f84
commit b89942dbff
2 changed files with 38 additions and 36 deletions

View file

@ -10,12 +10,15 @@ function createMechanism() {
type Collector = (d: IDerivation<$IntentionalAny>) => void type Collector = (d: IDerivation<$IntentionalAny>) => void
const collectObservedDependencies = ( const pushCollector = (collector: Collector): void => {
cb: () => void,
collector: Collector,
) => {
stack.push(collector) stack.push(collector)
cb() }
const popCollector = (collector: Collector): void => {
const existing = stack.peek()
if (existing !== collector) {
throw new Error(`Popped collector is not on top of the stack`)
}
stack.pop() stack.pop()
} }
@ -46,18 +49,14 @@ function createMechanism() {
stack.pop() stack.pop()
} }
const isCollectingDependencies = () => {
return stack.peek() !== noopCollector
}
return { return {
type: 'Dataverse_discoveryMechanism' as 'Dataverse_discoveryMechanism', type: 'Dataverse_discoveryMechanism' as 'Dataverse_discoveryMechanism',
collectObservedDependencies,
startIgnoringDependencies, startIgnoringDependencies,
stopIgnoringDependencies, stopIgnoringDependencies,
reportResolutionStart, reportResolutionStart,
reportResolutionEnd, reportResolutionEnd,
isCollectingDependencies, pushCollector,
popCollector,
} }
} }
@ -85,10 +84,10 @@ function getSharedMechanism(): ReturnType<typeof createMechanism> {
} }
export const { export const {
collectObservedDependencies,
startIgnoringDependencies, startIgnoringDependencies,
stopIgnoringDependencies, stopIgnoringDependencies,
reportResolutionEnd, reportResolutionEnd,
reportResolutionStart, reportResolutionStart,
isCollectingDependencies, pushCollector,
popCollector,
} = getSharedMechanism() } = getSharedMechanism()

View file

@ -4,9 +4,10 @@ import Stack from '../../utils/Stack'
import AbstractDerivation from '../AbstractDerivation' import AbstractDerivation from '../AbstractDerivation'
import type {IDerivation} from '../IDerivation' import type {IDerivation} from '../IDerivation'
import { import {
collectObservedDependencies,
startIgnoringDependencies, startIgnoringDependencies,
stopIgnoringDependencies, stopIgnoringDependencies,
pushCollector,
popCollector,
} from './discoveryMechanism' } from './discoveryMechanism'
const voidFn = () => {} const voidFn = () => {}
@ -44,8 +45,14 @@ export class PrismDerivation<V> extends AbstractDerivation<V> {
const newDeps: Set<IDerivation<unknown>> = new Set() const newDeps: Set<IDerivation<unknown>> = new Set()
this._cacheOfDendencyValues.clear() this._cacheOfDendencyValues.clear()
collectObservedDependencies(
() => { const collector = (observedDep: IDerivation<unknown>): void => {
newDeps.add(observedDep)
this._addDependency(observedDep)
}
pushCollector(collector)
hookScopeStack.push(this._prismScope) hookScopeStack.push(this._prismScope)
try { try {
value = this._fn() value = this._fn()
@ -60,12 +67,8 @@ export class PrismDerivation<V> extends AbstractDerivation<V> {
) )
} }
} }
},
(observedDep) => { popCollector(collector)
newDeps.add(observedDep)
this._addDependency(observedDep)
},
)
this._dependencies.forEach((dep) => { this._dependencies.forEach((dep) => {
if (!newDeps.has(dep)) { if (!newDeps.has(dep)) {