diff --git a/packages/dataverse/src/derivations/prism/prism.ts b/packages/dataverse/src/derivations/prism/prism.ts index b6f2acf..121119c 100644 --- a/packages/dataverse/src/derivations/prism/prism.ts +++ b/packages/dataverse/src/derivations/prism/prism.ts @@ -108,6 +108,8 @@ class PrismScope { isPrismScope = true private _subs: Record = {} readonly effects: Map = new Map() + readonly memos: Map = new Map() + readonly refs: Map> = new Map() sub(key: string) { if (!this._subs[key]) { @@ -153,8 +155,6 @@ function safelyRun( const hookScopeStack = new Stack() -const refsWeakMap = new WeakMap>>() - type IRef = { current: T } @@ -164,8 +164,6 @@ type IEffect = { cleanup: VoidFn } -const memosWeakMap = new WeakMap>() - type IMemo = { deps: undefined | unknown[] | ReadonlyArray cachedValue: unknown @@ -176,20 +174,15 @@ function ref(key: string, initialValue: T): IRef { if (!scope) { throw new Error(`prism.ref() is called outside of a prism() call.`) } - let refs = refsWeakMap.get(scope) - if (refs === undefined) { - refs = new Map() - refsWeakMap.set(scope, refs) - } - let ref = refs.get(key) + let ref = scope.refs.get(key) if (ref !== undefined) { return ref as $IntentionalAny as IRef } else { const ref = { current: initialValue, } - refs.set(key, ref) + scope.refs.set(key, ref) return ref } } @@ -262,21 +255,14 @@ function memo( throw new Error(`prism.memo() is called outside of a prism() call.`) } - let memos = memosWeakMap.get(scope) - - if (!memos) { - memos = new Map() - memosWeakMap.set(scope, memos) - } - - let memo = memos.get(key) + let memo = scope.memos.get(key) if (memo === undefined) { memo = { cachedValue: null, // undefined will always indicate "deps have changed", so we set it's initial value as such deps: undefined, } - memos.set(key, memo) + scope.memos.set(key, memo) } if (depsHaveChanged(memo.deps, deps)) {