File tree 2 files changed +23
-1
lines changed
2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -194,4 +194,14 @@ describe('signalState', () => {
194
194
expect ( stateCounter ) . toBe ( 3 ) ;
195
195
expect ( userCounter ) . toBe ( 1 ) ;
196
196
} ) ) ;
197
+
198
+ it ( 'throws on a mutable change' , ( ) => {
199
+ const userState = signalState ( initialState ) ;
200
+ expect ( ( ) =>
201
+ patchState ( userState , ( state ) => {
202
+ state . ngrx = 'mutable change' ;
203
+ return state ;
204
+ } )
205
+ ) . toThrowError ( "Cannot assign to read only property 'ngrx' of object" ) ;
206
+ } ) ;
197
207
} ) ;
Original file line number Diff line number Diff line change @@ -10,6 +10,8 @@ import {
10
10
import { SIGNAL } from '@angular/core/primitives/signals' ;
11
11
import { Prettify } from './ts-helpers' ;
12
12
13
+ declare const ngDevMode : boolean ;
14
+
13
15
const STATE_WATCHERS = new WeakMap < object , Array < StateWatcher < any > > > ( ) ;
14
16
15
17
export const STATE_SOURCE = Symbol ( 'STATE_SOURCE' ) ;
@@ -40,7 +42,9 @@ export function patchState<State extends object>(
40
42
updaters . reduce (
41
43
( nextState : State , updater ) => ( {
42
44
...nextState ,
43
- ...( typeof updater === 'function' ? updater ( nextState ) : updater ) ,
45
+ ...( typeof updater === 'function'
46
+ ? updater ( freezeInDevMode ( nextState ) )
47
+ : updater ) ,
44
48
} ) ,
45
49
currentState
46
50
)
@@ -49,6 +53,14 @@ export function patchState<State extends object>(
49
53
notifyWatchers ( stateSource ) ;
50
54
}
51
55
56
+ function freezeInDevMode < State extends object > ( value : State ) : State {
57
+ if ( ngDevMode ) {
58
+ Object . freeze ( value ) ;
59
+ }
60
+
61
+ return value ;
62
+ }
63
+
52
64
export function getState < State extends object > (
53
65
stateSource : StateSource < State >
54
66
) : State {
You can’t perform that action at this time.
0 commit comments