Skip to content

Commit

Permalink
fix(signals): remove usage of SIGNAL from @angular/core/primitives/si…
Browse files Browse the repository at this point in the history
…gnals package (#4530)
  • Loading branch information
brandonroberts authored Sep 24, 2024
1 parent 7ecd063 commit cae429a
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 14 deletions.
2 changes: 0 additions & 2 deletions modules/signals/spec/signal-store.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ describe('signalStore', () => {

expect(isSignal(stateSource)).toBe(true);
expect(stateSource()).toEqual({ foo: 'bar' });
expect(typeof (stateSource as any).update === 'undefined').toBe(true);
});

it('creates a store with readonly state source when protectedState option is true', () => {
Expand All @@ -60,7 +59,6 @@ describe('signalStore', () => {

expect(isSignal(stateSource)).toBe(true);
expect(stateSource()).toEqual({ foo: 'bar' });
expect(typeof (stateSource as any).update === 'undefined').toBe(true);
});

it('creates a store with writable state source when protectedState option is false', () => {
Expand Down
5 changes: 1 addition & 4 deletions modules/signals/src/signal-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1357,10 +1357,7 @@ export function signalStore(
const { stateSignals, computedSignals, methods, hooks } = innerStore;
const storeMembers = { ...stateSignals, ...computedSignals, ...methods };

(this as any)[STATE_SOURCE] =
config.protectedState === false
? innerStore[STATE_SOURCE]
: innerStore[STATE_SOURCE].asReadonly();
(this as any)[STATE_SOURCE] = innerStore[STATE_SOURCE];

for (const key in storeMembers) {
(this as any)[key] = storeMembers[key];
Expand Down
12 changes: 4 additions & 8 deletions modules/signals/src/state-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,9 @@ import {
untracked,
WritableSignal,
} from '@angular/core';
import { SIGNAL } from '@angular/core/primitives/signals';
import { Prettify } from './ts-helpers';

const STATE_WATCHERS = new WeakMap<object, Array<StateWatcher<any>>>();
const STATE_WATCHERS = new WeakMap<Signal<object>, Array<StateWatcher<any>>>();

export const STATE_SOURCE = Symbol('STATE_SOURCE');

Expand Down Expand Up @@ -79,7 +78,7 @@ export function watchState<State extends object>(
function getWatchers<State extends object>(
stateSource: StateSource<State>
): Array<StateWatcher<State>> {
return STATE_WATCHERS.get(stateSource[STATE_SOURCE][SIGNAL] as object) || [];
return STATE_WATCHERS.get(stateSource[STATE_SOURCE]) || [];
}

function notifyWatchers<State extends object>(
Expand All @@ -98,10 +97,7 @@ function addWatcher<State extends object>(
watcher: StateWatcher<State>
): void {
const watchers = getWatchers(stateSource);
STATE_WATCHERS.set(stateSource[STATE_SOURCE][SIGNAL] as object, [
...watchers,
watcher,
]);
STATE_WATCHERS.set(stateSource[STATE_SOURCE], [...watchers, watcher]);
}

function removeWatcher<State extends object>(
Expand All @@ -110,7 +106,7 @@ function removeWatcher<State extends object>(
): void {
const watchers = getWatchers(stateSource);
STATE_WATCHERS.set(
stateSource[STATE_SOURCE][SIGNAL] as object,
stateSource[STATE_SOURCE],
watchers.filter((w) => w !== watcher)
);
}

0 comments on commit cae429a

Please sign in to comment.