Skip to content

Commit

Permalink
feat: Adapting signals #4 - added track() utility
Browse files Browse the repository at this point in the history
  • Loading branch information
mnasyrov committed Aug 18, 2023
1 parent 16f3524 commit 84c8937
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
20 changes: 5 additions & 15 deletions packages/rx-effects/src/signals/computed.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { BehaviorSubject, materialize, Subject, Subscription } from 'rxjs';
import { collectChanges, waitForMicrotask } from '../../test/testUtils';
import { Signal } from './common';
import { computed } from './computed';
import { computed, track } from './computed';
import { ASYNC_EFFECT_MANAGER, effect } from './effect';
import { toObservable, toSignal } from './rxjs-interop';
import { signal } from './signal';
Expand Down Expand Up @@ -346,24 +346,15 @@ describe('computed()', () => {
it('should handle recursion during store updates: Value selector', async () => {
const store = signal({ a: 0, result: { value: 0 } });

const nextResult = computed(() => ({ value: store().a }));
const nextResult = computed(() => ({ value: track(() => store().a) }));

// const a = computed(() => store().a);
// const nextResult = computed(() => ({ value: a() }));

let subscription: Subscription | undefined;

toObservable(nextResult, { onlyChanges: true }).subscribe((result) => {
const subscription = toObservable(nextResult, {
onlyChanges: true,
}).subscribe((result) => {
store.update((state) => ({ ...state, result }));
});

const changes = await collectChanges(toObservable(store), async () => {
// subscription = toObservable(nextResult, { onlyChanges: true }).subscribe(
// (result) => {
// store.update((state) => ({ ...state, result }));
// },
// );

expect(nextResult()).toEqual({ value: 0 });

store.update((state) => ({ ...state, a: 1 }));
Expand All @@ -377,7 +368,6 @@ describe('computed()', () => {

expect(changes).toEqual([
{ a: 0, result: { value: 0 } },
{ a: 1, result: { value: 0 } },
{ a: 1, result: { value: 1 } },
]);
});
Expand Down
8 changes: 8 additions & 0 deletions packages/rx-effects/src/signals/computed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -179,3 +179,11 @@ export class ComputedImpl<T> extends ReactiveNode {
return this.value;
}
}

export function track<T>(
computation: Computation<T>,
options?: CreateComputedOptions<T>,
): T {
const proxy = computed<T>(computation, options);
return proxy();
}

0 comments on commit 84c8937

Please sign in to comment.