Skip to content

Commit

Permalink
fix: computed bug
Browse files Browse the repository at this point in the history
  • Loading branch information
yc-kanyun committed Dec 26, 2024
1 parent 4c9c901 commit 72d2e3f
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 1 deletion.
34 changes: 34 additions & 0 deletions packages/ccstate/src/core/__tests__/store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -768,3 +768,37 @@ it('get default store should be same', () => {
const store2 = getDefaultStore();
expect(store1).toBe(store2);
});

it('branch computed', () => {
const count$ = state(0, { debugLabel: 'count$' });
const base$ = computed((get) => get(count$), {
debugLabel: 'base$',
});
const mid$ = computed((get) => get(base$) + 1, {
debugLabel: 'mid$',
});
const branch$ = state(true, {
debugLabel: 'branch$',
});
const top$ = computed(
(get) => {
if (get(branch$)) {
return get(mid$);
}

return get(base$);
},
{
debugLabel: 'top$',
},
);

const store = createStore();
expect(store.get(top$)).toBe(1);

store.set(branch$, false);
expect(store.get(top$)).toBe(0);

store.set(count$, 1);
expect(store.get(top$)).toBe(1);
});
4 changes: 3 additions & 1 deletion packages/ccstate/src/core/signal/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ function checkEpoch<T>(
? readComputed(dep, context, mutation).epoch
: context.stateMap.get(dep)?.epoch;

return depEpoch === epoch;
if (depEpoch !== epoch) {
return false;
}
}

return true;
Expand Down

0 comments on commit 72d2e3f

Please sign in to comment.