| 77 | * ``` |
| 78 | */ |
| 79 | export function patchState<State extends object>( |
| 80 | stateSource: WritableStateSource<State>, |
| 81 | ...updaters: Array< |
| 82 | Partial<NoInfer<State>> | PartialStateUpdater<NoInfer<State>> |
| 83 | > |
| 84 | ): void { |
| 85 | const currentState = untracked(() => getState(stateSource)); |
| 86 | const newState = updaters.reduce( |
| 87 | (nextState: State, updater) => ({ |
| 88 | ...nextState, |
| 89 | ...(typeof updater === 'function' ? updater(nextState) : updater), |
| 90 | }), |
| 91 | currentState |
| 92 | ); |
| 93 | |
| 94 | const signals = stateSource[STATE_SOURCE]; |
| 95 | const stateKeys = Reflect.ownKeys(stateSource[STATE_SOURCE]); |
| 96 | |
| 97 | for (const key of Reflect.ownKeys(newState)) { |
| 98 | if (stateKeys.includes(key)) { |
| 99 | const signalKey = key as keyof State; |
| 100 | if (currentState[signalKey] !== newState[signalKey]) { |
| 101 | signals[signalKey].set(newState[signalKey]); |
| 102 | } |
| 103 | } else if (typeof ngDevMode !== 'undefined' && ngDevMode) { |
| 104 | console.warn( |
| 105 | `@ngrx/signals: patchState was called with an unknown state slice '${String( |
| 106 | key |
| 107 | )}'.`, |
| 108 | 'Ensure that all state properties are explicitly defined in the initial state.', |
| 109 | 'Updates to properties not present in the initial state will be ignored.' |
| 110 | ); |
| 111 | } |
| 112 | } |
| 113 | |
| 114 | notifyWatchers(stateSource); |
| 115 | } |
| 116 | |
| 117 | /** |
| 118 | * @description |