Skip to content

Commit

Permalink
fix: isObserving now requires arg
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Nov 25, 2022
1 parent 3dda0c9 commit 8fd3ade
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/observables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,10 @@ export function computed<T, R = never>(
}

/**
* Whether the current scope is actively observing for any updates.
* Whether the given function is actively observing any computations.
*/
export function isObserving(): boolean {
return [currentObserver, ...(currentObserver?.[CHILDREN] ?? [])].some(
(node) => node?.[OBSERVING]?.size,
);
export function isObserving(fn: () => void): boolean {
return [fn, ...(fn?.[CHILDREN] ?? [])].some((node) => node[OBSERVING]?.size);
}

/** @deprecated use `isObserving` */
Expand Down
8 changes: 4 additions & 4 deletions tests/isObserving.test.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
import { effect, isObserving, observable } from '../src';
import { effect, getScope, isObserving, observable } from '../src';

it('should return true if there are observers', () => {
const a = observable(0);

effect(() => {
a();
expect(isObserving()).toBeTruthy();
expect(isObserving(getScope()!)).toBeTruthy();
});
});

it('should return true if child is observing', () => {
const $a = observable(0);
effect(() => {
effect(() => void $a());
expect(isObserving()).toBeTruthy();
expect(isObserving(getScope()!)).toBeTruthy();
});
});

it('should return false if there are _no_ observers', () => {
effect(() => {
expect(isObserving()).toBeFalsy();
expect(isObserving(getScope()!)).toBeFalsy();
});
});

0 comments on commit 8fd3ade

Please sign in to comment.