Remove all WeakMaps from prism's scope

... making it easier to debug prism scopes. Again, there is a perf regression here.
This commit is contained in:
Aria Minaei 2022-11-26 19:29:04 +01:00
parent 1c69cb7055
commit 6d7d461223

View file

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