Skip to content

Commit

Permalink
fix: observe not running reaction if selector is an object or array
Browse files Browse the repository at this point in the history
  • Loading branch information
jmeistrich committed Dec 4, 2023
1 parent 418c6fa commit 0a1df15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/observe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export function observe<T>(
if (
reaction &&
(e.num > 0 || !isEvent(selectorOrRun as any)) &&
(e.previous !== e.value || options?.fromComputed)
(e.previous !== e.value || options?.fromComputed || typeof e.value === 'object')
) {
reaction(e);
}
Expand Down
15 changes: 15 additions & 0 deletions tests/tests.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2751,6 +2751,21 @@ describe('Observe', () => {
obsOther.set(1);
expect(count).toEqual(1);
});
test('Observe with reaction and array', () => {
let lastLength = 0;
const state$ = observable({
arr: [1, 2, 3, 4],
});

observe(state$.arr, () => {
const num = state$.arr.peek();
lastLength = num.length;
});

expect(lastLength).toEqual(4);
state$.arr.push(10);
expect(lastLength).toEqual(5);
});
});
describe('Error detection', () => {
test('Circular objects in set', () => {
Expand Down

0 comments on commit 0a1df15

Please sign in to comment.