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