Skip to content

Commit

Permalink
fix: root should not add any observers
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Nov 25, 2022
1 parent 20aa37e commit 36baad2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions src/observables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ if (__DEV__) {
export function root<T>(fn: (dispose: Dispose) => T): T {
const $root = () => {};
$root[SCOPE] = currentScope;
return compute($root, () => fn(() => dispose($root)));
return compute($root, () => fn(() => dispose($root)), undefined);
}

/**
Expand Down Expand Up @@ -191,7 +191,7 @@ export function computed<T>(fn: () => T, options?: ComputedOptions<T>): Observab

$computed[CONTEXT]?.[ERROR]?.clear();

const nextValue = compute($computed, fn);
const nextValue = compute($computed, fn, $computed);
if (isDirty(currentValue, nextValue)) {
currentValue = nextValue;
dirtyNode($computed);
Expand Down Expand Up @@ -425,7 +425,7 @@ export function dispose(fn: () => void) {
function compute<T>(
scope: (() => void) | undefined,
node: () => T,
observer: (() => void) | undefined = scope,
observer: (() => void) | undefined,
): T {
const prevScope = currentScope;
const prevObserver = currentObserver;
Expand Down
10 changes: 10 additions & 0 deletions tests/root.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
type Observable,
type ObservableSubject,
} from '../src';
import { OBSERVED_BY, OBSERVING } from '../src/symbols';

afterEach(() => tick());

Expand Down Expand Up @@ -100,3 +101,12 @@ it('should hold parent tracking', async () => {
});
});
});

it('should not observe', () => {
const $a = observable(0);
root(() => {
$a();
expect(getScope()![OBSERVING]).toBeUndefined();
expect(getScope()![OBSERVED_BY]).toBeUndefined();
});
});

0 comments on commit 36baad2

Please sign in to comment.