From b89942dbffbf554e879d45ff7b79f24dd77eee0e Mon Sep 17 00:00:00 2001 From: Aria Minaei Date: Wed, 7 Jul 2021 11:50:23 +0200 Subject: [PATCH] Minor change to discoveryMechanism's api --- .../derivations/prism/discoveryMechanism.ts | 25 +++++----- .../dataverse/src/derivations/prism/prism.ts | 49 ++++++++++--------- 2 files changed, 38 insertions(+), 36 deletions(-) diff --git a/packages/dataverse/src/derivations/prism/discoveryMechanism.ts b/packages/dataverse/src/derivations/prism/discoveryMechanism.ts index 0ba7865..90b6e2a 100644 --- a/packages/dataverse/src/derivations/prism/discoveryMechanism.ts +++ b/packages/dataverse/src/derivations/prism/discoveryMechanism.ts @@ -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 { } export const { - collectObservedDependencies, startIgnoringDependencies, stopIgnoringDependencies, reportResolutionEnd, reportResolutionStart, - isCollectingDependencies, + pushCollector, + popCollector, } = getSharedMechanism() diff --git a/packages/dataverse/src/derivations/prism/prism.ts b/packages/dataverse/src/derivations/prism/prism.ts index 670e2a6..962aab0 100644 --- a/packages/dataverse/src/derivations/prism/prism.ts +++ b/packages/dataverse/src/derivations/prism/prism.ts @@ -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 extends AbstractDerivation { const newDeps: Set> = 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): 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)) {