MCPcopy Create free account
hub / github.com/ngrx/platform / patchState

Function patchState

modules/signals/src/state-source.ts:79–115  ·  view source on GitHub ↗
(
  stateSource: WritableStateSource<State>,
  ...updaters: Array<
    Partial<NoInfer<State>> | PartialStateUpdater<NoInfer<State>>
  >
)

Source from the content-addressed store, hash-verified

77 * ```
78 */
79export 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

Calls 3

notifyWatchersFunction · 0.85
warnMethod · 0.80
getStateFunction · 0.70

Tested by 14

addUsersFunction · 0.72
addEntitiesFunction · 0.72
loadFunction · 0.72
increaseCounterFunction · 0.72
withLoadEntitiesFunction · 0.72
setFilterFunction · 0.72
setFooFunction · 0.72
addNumberFunction · 0.72
onInitFunction · 0.72
incrementFunction · 0.72
updateUserFunction · 0.72
addBookFunction · 0.72