@ngxs/store 21.0.0-dev.master-f8041d1 → 21.0.0-dev.master-35d18c0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/fesm2022/ngxs-store-operators.mjs +201 -34
- package/fesm2022/ngxs-store-operators.mjs.map +1 -1
- package/fesm2022/ngxs-store.mjs +42 -26
- package/fesm2022/ngxs-store.mjs.map +1 -1
- package/index.d.ts +7 -1
- package/operators/index.d.ts +180 -13
- package/package.json +1 -1
package/index.d.ts
CHANGED
|
@@ -506,6 +506,12 @@ declare function withNgxsNoopExecutionStrategy(): i0.EnvironmentProviders;
|
|
|
506
506
|
*/
|
|
507
507
|
declare function withNgxsPendingTasks(): i0.EnvironmentProviders;
|
|
508
508
|
|
|
509
|
+
declare class StateContextDestroyedError extends Error {
|
|
510
|
+
readonly path: string;
|
|
511
|
+
readonly name = "StateContextDestroyedError";
|
|
512
|
+
constructor(path: string);
|
|
513
|
+
}
|
|
514
|
+
|
|
509
515
|
/**
|
|
510
516
|
* This function provides global store providers and initializes the store.
|
|
511
517
|
*
|
|
@@ -718,5 +724,5 @@ declare function registerNgxsPlugin(plugin: Type<NgxsPlugin> | NgxsPluginFn): vo
|
|
|
718
724
|
|
|
719
725
|
declare function ɵprovideNgxsInternalStateTokens(): i0.EnvironmentProviders;
|
|
720
726
|
|
|
721
|
-
export { Action, ActionDirector, ActionStatus, Actions, AsyncReturnType, NgxsConfig, NgxsDevelopmentModule, NgxsModule, NgxsSimpleChange, NgxsUnhandledActionsLogger, NgxsUnhandledErrorHandler, Select, Selector, SelectorOptions, State, Store, createDispatchMap, createModelSelector, createPickSelector, createPropertySelectors, createSelectMap, createSelector, dispatch, lazyProvider, ofAction, ofActionCanceled, ofActionCompleted, ofActionDispatched, ofActionErrored, ofActionSuccessful, provideStates, provideStore, registerNgxsPlugin, select, withNgxsDevelopmentOptions, withNgxsNoopExecutionStrategy, withNgxsPendingTasks, withNgxsPlugin, withNgxsPreboot, NgxsFeatureModule as ɵNgxsFeatureModule, NgxsRootModule as ɵNgxsRootModule, ɵprovideNgxsInternalStateTokens };
|
|
727
|
+
export { Action, ActionDirector, ActionStatus, Actions, AsyncReturnType, NgxsConfig, NgxsDevelopmentModule, NgxsModule, NgxsSimpleChange, NgxsUnhandledActionsLogger, NgxsUnhandledErrorHandler, Select, Selector, SelectorOptions, State, StateContextDestroyedError, Store, createDispatchMap, createModelSelector, createPickSelector, createPropertySelectors, createSelectMap, createSelector, dispatch, lazyProvider, ofAction, ofActionCanceled, ofActionCompleted, ofActionDispatched, ofActionErrored, ofActionSuccessful, provideStates, provideStore, registerNgxsPlugin, select, withNgxsDevelopmentOptions, withNgxsNoopExecutionStrategy, withNgxsPendingTasks, withNgxsPlugin, withNgxsPreboot, NgxsFeatureModule as ɵNgxsFeatureModule, NgxsRootModule as ɵNgxsRootModule, ɵprovideNgxsInternalStateTokens };
|
|
722
728
|
export type { ActionCompletion, ActionContext, ActionDef, ActionMap, ActionType, NgxsAfterBootstrap, NgxsDevelopmentOptions, NgxsModuleOptions, NgxsOnChanges, NgxsOnInit, NgxsUnhandledErrorContext, PropertySelectors, SelectorMap, StateContext, TypedSelector, ɵSelectorDef, ɵSelectorFunc, ɵSelectorReturnType, ɵStateSelector };
|
package/operators/index.d.ts
CHANGED
|
@@ -55,10 +55,43 @@ type ExistingState<T> = T extends any ? ɵAsReadonly<T> : never;
|
|
|
55
55
|
type StateOperator<T> = (existing: ExistingState<T>) => T;
|
|
56
56
|
|
|
57
57
|
/**
|
|
58
|
-
*
|
|
58
|
+
* Adds items to the end of an array without mutating the original. Handles
|
|
59
|
+
* the case where the array property does not exist yet, so callers do not
|
|
60
|
+
* need to initialise it before appending. A `null`, `undefined`, or empty
|
|
61
|
+
* `items` argument is treated as a no-op to allow safe pass-through of
|
|
62
|
+
* optional data.
|
|
63
|
+
*
|
|
64
|
+
* @param items - Items to add to the end of the array.
|
|
65
|
+
*
|
|
66
|
+
* @example
|
|
67
|
+
* ```ts
|
|
68
|
+
* // Add a new zebra to the end of the list without touching the rest of state.
|
|
69
|
+
* ctx.setState(
|
|
70
|
+
* patch<AnimalsStateModel>({
|
|
71
|
+
* zebras: append<string>([action.payload])
|
|
72
|
+
* })
|
|
73
|
+
* );
|
|
74
|
+
* ```
|
|
59
75
|
*/
|
|
60
76
|
declare function append<T>(items: NoInfer<T[]>): StateOperator<T[]>;
|
|
61
77
|
|
|
78
|
+
/**
|
|
79
|
+
* Chains multiple state operators so they execute left-to-right, each
|
|
80
|
+
* receiving the output of the previous one. Useful when several independent
|
|
81
|
+
* transformations must be applied to the same state slice in a single atomic
|
|
82
|
+
* update, avoiding multiple `setState` calls.
|
|
83
|
+
*
|
|
84
|
+
* @example
|
|
85
|
+
* ```ts
|
|
86
|
+
* // Apply two independent array mutations in one atomic setState call.
|
|
87
|
+
* ctx.setState(
|
|
88
|
+
* compose<AnimalsStateModel>(
|
|
89
|
+
* patch({ zebras: append<string>([action.zebraName]) }),
|
|
90
|
+
* patch({ pandas: removeItem<string>(name => name === action.pandaToRemove) })
|
|
91
|
+
* )
|
|
92
|
+
* );
|
|
93
|
+
* ```
|
|
94
|
+
*/
|
|
62
95
|
declare function compose<T>(...operators: NoInfer<StateOperator<T>[]>): StateOperator<T>;
|
|
63
96
|
|
|
64
97
|
type Predicate<T = any> = (value: T | Readonly<T>) => boolean;
|
|
@@ -66,17 +99,62 @@ declare const isStateOperator: <T>(value: T | StateOperator<T>) => value is Stat
|
|
|
66
99
|
declare const isPredicate: <T>(value: Predicate<T> | boolean | number) => value is Predicate<T>;
|
|
67
100
|
|
|
68
101
|
/**
|
|
69
|
-
*
|
|
70
|
-
*
|
|
71
|
-
*
|
|
72
|
-
*
|
|
73
|
-
* @param
|
|
102
|
+
* Applies one of two operators (or values) based on a condition, keeping
|
|
103
|
+
* conditional logic out of action handlers and inside the state mutation
|
|
104
|
+
* pipeline where it belongs.
|
|
105
|
+
*
|
|
106
|
+
* @param condition - A boolean or a predicate receiving the current state value.
|
|
107
|
+
* Use a predicate when the decision depends on the existing state rather than
|
|
108
|
+
* external data available at dispatch time.
|
|
109
|
+
* @param trueOperatorOrValue - Applied when `condition` is truthy.
|
|
110
|
+
* @param elseOperatorOrValue - Applied when `condition` is falsy. Omit to
|
|
111
|
+
* leave the state unchanged in the false branch.
|
|
112
|
+
*
|
|
113
|
+
* @example
|
|
114
|
+
* ```ts
|
|
115
|
+
* // Only add a panda when the list has fewer than 5 — the cap is enforced
|
|
116
|
+
* // inside the operator so the action handler stays free of branching logic.
|
|
117
|
+
* ctx.setState(
|
|
118
|
+
* patch<AnimalsStateModel>({
|
|
119
|
+
* pandas: iif(
|
|
120
|
+
* pandas => pandas.length < 5,
|
|
121
|
+
* append<string>([action.payload])
|
|
122
|
+
* )
|
|
123
|
+
* })
|
|
124
|
+
* );
|
|
125
|
+
* ```
|
|
74
126
|
*/
|
|
75
127
|
declare function iif<T>(condition: NoInfer<Predicate<T>> | boolean, trueOperatorOrValue: NoInfer<StateOperator<T> | T>, elseOperatorOrValue?: NoInfer<StateOperator<T> | T>): StateOperator<T>;
|
|
76
128
|
|
|
77
129
|
/**
|
|
78
|
-
*
|
|
79
|
-
*
|
|
130
|
+
* Inserts an item into an array without mutating the original, satisfying
|
|
131
|
+
* NGXS's immutability requirement. Handles the case where the array property
|
|
132
|
+
* does not exist yet, so callers do not need to initialise it first.
|
|
133
|
+
*
|
|
134
|
+
* @param value - The item to insert. A `null` or `undefined` value is a no-op
|
|
135
|
+
* so that callers can pass through optional data safely.
|
|
136
|
+
* @param beforePosition - Index before which to insert. Omit (or pass a
|
|
137
|
+
* non-positive number) to prepend to the beginning of the array.
|
|
138
|
+
*
|
|
139
|
+
* @example
|
|
140
|
+
* ```ts
|
|
141
|
+
* // Prepend a new zebra (no position = insert at index 0).
|
|
142
|
+
* ctx.setState(
|
|
143
|
+
* patch<AnimalsStateModel>({
|
|
144
|
+
* zebras: insertItem<string>(action.payload)
|
|
145
|
+
* })
|
|
146
|
+
* );
|
|
147
|
+
* ```
|
|
148
|
+
*
|
|
149
|
+
* @example
|
|
150
|
+
* ```ts
|
|
151
|
+
* // Insert before index 2, shifting subsequent items right.
|
|
152
|
+
* ctx.setState(
|
|
153
|
+
* patch<AnimalsStateModel>({
|
|
154
|
+
* zebras: insertItem<string>(action.payload, 2)
|
|
155
|
+
* })
|
|
156
|
+
* );
|
|
157
|
+
* ```
|
|
80
158
|
*/
|
|
81
159
|
declare function insertItem<T>(value: NoInfer<T>, beforePosition?: number): StateOperator<T[]>;
|
|
82
160
|
|
|
@@ -84,20 +162,109 @@ type NotUndefined<T> = T extends undefined ? never : T;
|
|
|
84
162
|
type ɵPatchSpec<T> = {
|
|
85
163
|
[P in keyof T]?: T[P] | StateOperator<NotUndefined<T[P]>>;
|
|
86
164
|
};
|
|
165
|
+
/**
|
|
166
|
+
* Applies a partial update to a state object, only cloning it when at least
|
|
167
|
+
* one property actually changes. This preserves referential equality for
|
|
168
|
+
* unchanged states, preventing unnecessary re-renders in `OnPush` components
|
|
169
|
+
* and keeping memoized selectors from recalculating.
|
|
170
|
+
*
|
|
171
|
+
* Each property in `patchObject` can itself be a state operator, enabling
|
|
172
|
+
* nested immutable updates without manually spreading every level of the tree.
|
|
173
|
+
*
|
|
174
|
+
* @example
|
|
175
|
+
* ```ts
|
|
176
|
+
* // Add an optional property to a state slice without touching existing ones.
|
|
177
|
+
* ctx.setState(
|
|
178
|
+
* patch<AnimalsStateModel>({ monkeys: [] })
|
|
179
|
+
* );
|
|
180
|
+
* ```
|
|
181
|
+
*
|
|
182
|
+
* @example
|
|
183
|
+
* ```ts
|
|
184
|
+
* // Deep update — specify explicit types at each level so TypeScript can
|
|
185
|
+
* // catch property name mistakes in nested patches.
|
|
186
|
+
* ctx.setState(
|
|
187
|
+
* patch<AddressStateModel>({
|
|
188
|
+
* country: patch<AddressStateModel['country']>({
|
|
189
|
+
* city: patch<AddressStateModel['country']['city']>({
|
|
190
|
+
* address: patch<AddressStateModel['country']['city']['address']>({
|
|
191
|
+
* line1: action.line1
|
|
192
|
+
* })
|
|
193
|
+
* })
|
|
194
|
+
* })
|
|
195
|
+
* })
|
|
196
|
+
* );
|
|
197
|
+
* ```
|
|
198
|
+
*/
|
|
87
199
|
declare function patch<T extends Record<string, any>>(patchObject: NoInfer<ɵPatchSpec<T>>): StateOperator<T>;
|
|
88
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Like `patch`, but safe to call when the state slice is `null` or
|
|
203
|
+
* `undefined`. Treats a missing slice as an empty object so the patch is
|
|
204
|
+
* applied against a clean baseline rather than throwing. Useful for lazily
|
|
205
|
+
* initialised state properties or optional sub-states that may not have been
|
|
206
|
+
* set yet.
|
|
207
|
+
*
|
|
208
|
+
* @example
|
|
209
|
+
* ```ts
|
|
210
|
+
* // Update a nested preferences slice that starts as null — no prior
|
|
211
|
+
* // null-check needed; safePatch treats null as an empty object.
|
|
212
|
+
* ctx.setState(
|
|
213
|
+
* patch<UserStateModel>({
|
|
214
|
+
* preferences: safePatch<UserPreferences>({ theme: action.theme })
|
|
215
|
+
* })
|
|
216
|
+
* );
|
|
217
|
+
* ```
|
|
218
|
+
*/
|
|
89
219
|
declare function safePatch<T extends object>(patchSpec: NoInfer<ɵPatchSpec<T>>): StateOperator<T>;
|
|
90
220
|
|
|
91
221
|
/**
|
|
92
|
-
*
|
|
93
|
-
*
|
|
94
|
-
*
|
|
95
|
-
*
|
|
222
|
+
* Replaces or transforms a single array element without cloning elements that
|
|
223
|
+
* did not change, preserving referential equality for the rest of the array.
|
|
224
|
+
* Returns the original array reference when nothing changed, keeping
|
|
225
|
+
* memoized selectors and `OnPush` components from re-rendering unnecessarily.
|
|
226
|
+
*
|
|
227
|
+
* @param selector - The index to update, or a predicate used to locate the
|
|
228
|
+
* item. Prefer a predicate when the item's position may have shifted since the
|
|
229
|
+
* index was last known.
|
|
230
|
+
* @param operatorOrValue - The replacement value, or a state operator applied
|
|
231
|
+
* to the existing element when a derived update is needed.
|
|
232
|
+
*
|
|
233
|
+
* @example
|
|
234
|
+
* ```ts
|
|
235
|
+
* // Rename a panda — locate it by current name so the index doesn't need
|
|
236
|
+
* // to be known ahead of time.
|
|
237
|
+
* ctx.setState(
|
|
238
|
+
* patch<AnimalsStateModel>({
|
|
239
|
+
* pandas: updateItem<string>(
|
|
240
|
+
* name => name === action.payload.name,
|
|
241
|
+
* action.payload.newName
|
|
242
|
+
* )
|
|
243
|
+
* })
|
|
244
|
+
* );
|
|
245
|
+
* ```
|
|
96
246
|
*/
|
|
97
247
|
declare function updateItem<T>(selector: number | NoInfer<Predicate<T>>, operatorOrValue: NoInfer<T> | NoInfer<StateOperator<T>>): StateOperator<T[]>;
|
|
98
248
|
|
|
99
249
|
/**
|
|
100
|
-
*
|
|
250
|
+
* Removes a single element from an array without mutating the original.
|
|
251
|
+
* Returns the original array reference when no matching item is found, so
|
|
252
|
+
* memoized selectors are not invalidated by a no-op removal.
|
|
253
|
+
*
|
|
254
|
+
* @param selector - The index to remove, or a predicate used to locate the
|
|
255
|
+
* item. Prefer a predicate when the item's position is not guaranteed to be
|
|
256
|
+
* stable across concurrent state updates.
|
|
257
|
+
*
|
|
258
|
+
* @example
|
|
259
|
+
* ```ts
|
|
260
|
+
* // Remove a panda by name — a predicate is safer than a hard-coded index
|
|
261
|
+
* // because the array order may change between dispatch and execution.
|
|
262
|
+
* ctx.setState(
|
|
263
|
+
* patch<AnimalsStateModel>({
|
|
264
|
+
* pandas: removeItem<string>(name => name === action.payload)
|
|
265
|
+
* })
|
|
266
|
+
* );
|
|
267
|
+
* ```
|
|
101
268
|
*/
|
|
102
269
|
declare function removeItem<T>(selector: number | NoInfer<Predicate<T>>): StateOperator<T[]>;
|
|
103
270
|
|