Skip to content

Commit

Permalink
fix(signals): do not create nested signals for STATE_SIGNAL property (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
markostanimirovic authored Oct 9, 2023
1 parent 52e7dbd commit 71a9d7f
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 3 deletions.
9 changes: 9 additions & 0 deletions modules/signals/spec/signal-state.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ describe('signalState', () => {
expect((state.user.firstName as any).y).toBe(undefined);
});

it('does not modify STATE_SIGNAL', () => {
const state = signalState(initialState);

expect((state[STATE_SIGNAL] as any).user).toBe(undefined);
expect((state[STATE_SIGNAL] as any).foo).toBe(undefined);
expect((state[STATE_SIGNAL] as any).numbers).toBe(undefined);
expect((state[STATE_SIGNAL] as any).ngrx).toBe(undefined);
});

it(
'emits new values only for affected signals',
testEffects((tick) => {
Expand Down
10 changes: 7 additions & 3 deletions modules/signals/src/deep-signal.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isSignal, Signal, untracked } from '@angular/core';
import { Signal, untracked } from '@angular/core';
import { selectSignal } from './select-signal';

export type DeepSignal<T> = Signal<T> &
Expand All @@ -14,11 +14,15 @@ export function toDeepSignal<T>(signal: Signal<T>): DeepSignal<T> {

return new Proxy(signal, {
get(target: any, prop) {
if (prop in value && !target[prop]) {
if (!(prop in value)) {
return target[prop];
}

if (!target[prop]) {
target[prop] = selectSignal(() => target()[prop]);
}

return isSignal(target[prop]) ? toDeepSignal(target[prop]) : target[prop];
return toDeepSignal(target[prop]);
},
});
}
Expand Down

0 comments on commit 71a9d7f

Please sign in to comment.