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,28 +45,30 @@ export class PrismDerivation<V> extends AbstractDerivation<V> {
const newDeps: Set<IDerivation<unknown>> = new Set()
this._cacheOfDendencyValues.clear()
collectObservedDependencies(
() => {
hookScopeStack.push(this._prismScope)
try {
value = this._fn()
} catch (error) {
console.error(error)
} finally {
const topOfTheStack = hookScopeStack.pop()
if (topOfTheStack !== this._prismScope) {
console.warn(
// @todo guide the user to report the bug in an issue
`The Prism hook stack has slipped. This is a bug.`,
)
}
}
},
(observedDep) => {
newDeps.add(observedDep)
this._addDependency(observedDep)
},
)
const collector = (observedDep: IDerivation<unknown>): void => {
newDeps.add(observedDep)
this._addDependency(observedDep)
}
pushCollector(collector)
hookScopeStack.push(this._prismScope)
try {
value = this._fn()
} catch (error) {
console.error(error)
} finally {
const topOfTheStack = hookScopeStack.pop()
if (topOfTheStack !== this._prismScope) {
console.warn(
// @todo guide the user to report the bug in an issue
`The Prism hook stack has slipped. This is a bug.`,
)
}
}
popCollector(collector)
this._dependencies.forEach((dep) => {
if (!newDeps.has(dep)) {