Skip to content

Commit

Permalink
fix(cache): clean deep suspended contexts
Browse files Browse the repository at this point in the history
  • Loading branch information
smalluban committed Jun 23, 2021
1 parent 50e31bb commit 0d08645
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/cache.js
Expand Up @@ -2,7 +2,6 @@ import * as emitter from "./emitter.js";

const entries = new WeakMap();
const suspense = new WeakSet();
const values = new WeakMap();

export function getEntry(target, key) {
let targetMap = entries.get(target);
Expand All @@ -18,6 +17,7 @@ export function getEntry(target, key) {
target,
key,
value: undefined,
lastValue: undefined,
contexts: new Set(),
deps: new Set(),
state: 0,
Expand All @@ -44,13 +44,16 @@ export function getEntries(target) {
function cleanContexts(entry) {
entry.contexts.forEach(contextEntry => {
if (suspense.has(contextEntry.target)) {
contextEntry.depState = 0;
contextEntry.resolved = false;
contextEntry.value = undefined;

values.delete(contextEntry);
Object.assign(contextEntry, {
value: undefined,
lastValue: undefined,
depState: 0,
resolved: false,
});

entry.contexts.delete(contextEntry);

cleanContexts(contextEntry);
}
});
}
Expand Down Expand Up @@ -195,7 +198,7 @@ function invalidateEntry(entry, options) {

if (options.clearValue) {
entry.value = undefined;
values.delete(entry);
entry.lastValue = undefined;
}

if (options.deleteEntry) {
Expand Down Expand Up @@ -237,12 +240,11 @@ export function observe(target, key, getter, fn) {
const entry = getEntry(target, key);

return emitter.subscribe(entry, () => {
const lastValue = values.get(entry);
const value = get(target, key, getter);

if (value !== lastValue) {
fn(target, value, lastValue);
values.set(entry, value);
if (value !== entry.lastValue) {
fn(target, value, entry.lastValue);
entry.lastValue = value;
}
});
}
Expand Down

0 comments on commit 0d08645

Please sign in to comment.