Skip to content

Commit

Permalink
fix: remove auto-dispose perf overhead
Browse files Browse the repository at this point in the history
  • Loading branch information
mihar-22 committed Nov 25, 2022
1 parent 36baad2 commit bbbbbe6
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 32 deletions.
4 changes: 1 addition & 3 deletions src/observables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ export function computed<T>(fn: () => T, options?: ComputedOptions<T>): Observab

if (__DEV__) init = true;
$computed[DIRTY] = false;
if (!$computed[OBSERVING]?.size) dispose($computed);
}

return currentValue;
Expand Down Expand Up @@ -369,7 +368,7 @@ export function onError<T = Error>(handler: (error: T) => void): void {
* @see {@link https://github.com/maverick-js/observables#ondispose}
*/
export function onDispose(dispose: MaybeDispose): Dispose {
if (!dispose || !currentScope || currentScope[DISPOSED]) return NOOP;
if (!dispose || !currentScope) return NOOP;
(currentScope[DISPOSAL] ??= new Set()).add(dispose);
return () => {
(dispose as Dispose)();
Expand Down Expand Up @@ -461,7 +460,6 @@ function adopt(node: Node) {
}

function observe(observable: Node, observer: Node) {
if (observable[DISPOSED] || observer[DISPOSED]) return;
(observable[OBSERVED_BY] ??= new Set()).add(observer);
(observer[OBSERVING] ??= new Set()).add(observable);
}
Expand Down
31 changes: 2 additions & 29 deletions tests/dispose.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { computed, observable, tick, dispose, effect, root, getScope, onError } from '../src';
import { CHILDREN, DISPOSED, OBSERVED_BY } from '../src/symbols';
import { computed, observable, tick, dispose, effect, root, getScope } from '../src';
import { CHILDREN, OBSERVED_BY } from '../src/symbols';

afterEach(() => tick());

Expand Down Expand Up @@ -34,33 +34,6 @@ it('shoud remove observable from parent children set', () => {
});
});

it('should auto-dispose computed if not observing anything', () => {
const $a = computed(() => null);
$a();

const $b = computed(() => $a());
$b();

expect($a[DISPOSED]).toBeTruthy();
expect($b[DISPOSED]).toBeTruthy();
});

it('should _not_ auto-dispose effect if error is thrown', async () => {
const $a = observable(0);

let shouldThrow = true,
effectScope;

effect(() => {
onError(() => {});
effectScope = getScope();
if (shouldThrow) throw Error();
$a();
});

expect(effectScope[DISPOSED]).toBeFalsy();
});

it('should stop observing effect', async () => {
const $a = observable(0);

Expand Down

0 comments on commit bbbbbe6

Please sign in to comment.