Skip to content

Commit

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

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

/** @deprecated use `isObserving` */
export const isObserved = isObserving;

/**
* Invokes the given function each time any of the observables that are read inside are updated
* (i.e., their value changes). The effect is immediately invoked on initialization.
Expand Down
16 changes: 0 additions & 16 deletions tests/isObserved.test.ts

This file was deleted.

24 changes: 24 additions & 0 deletions tests/isObserving.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { effect, isObserving, observable } from '../src';

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

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

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

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

0 comments on commit 7a96166

Please sign in to comment.