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
const collectObservedDependencies = (
cb: () => void,
collector: Collector,
) => {
const pushCollector = (collector: Collector): void => {
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()
}
@ -46,18 +49,14 @@ function createMechanism() {
stack.pop()
}
const isCollectingDependencies = () => {
return stack.peek() !== noopCollector
}
return {
type: 'Dataverse_discoveryMechanism' as 'Dataverse_discoveryMechanism',
collectObservedDependencies,
startIgnoringDependencies,
stopIgnoringDependencies,
reportResolutionStart,
reportResolutionEnd,
isCollectingDependencies,
pushCollector,
popCollector,
}
}
@ -85,10 +84,10 @@ function getSharedMechanism(): ReturnType<typeof createMechanism> {
}
export const {
collectObservedDependencies,
startIgnoringDependencies,
stopIgnoringDependencies,
reportResolutionEnd,
reportResolutionStart,
isCollectingDependencies,
pushCollector,
popCollector,
} = getSharedMechanism()

View file

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