@ngxs/storage-plugin 3.7.4 → 3.7.5

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.
@@ -223,393 +223,441 @@
223
223
  return value;
224
224
  }
225
225
 
226
- /**
227
- * @fileoverview added by tsickle
228
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
229
- */
230
- /** @enum {number} */
231
- var StorageOption = {
232
- LocalStorage: 0,
233
- SessionStorage: 1,
234
- };
235
- /**
236
- * @record
237
- */
238
- function NgxsStoragePluginOptions() { }
239
- if (false) {
240
- /**
241
- * Key for the state slice to store in the storage engine.
242
- * @type {?|undefined}
243
- */
244
- NgxsStoragePluginOptions.prototype.key;
245
- /**
246
- * Storage engine to use. Deaults to localStorage but can provide
247
- *
248
- * sessionStorage or custom implementation of the StorageEngine interface
249
- * @type {?|undefined}
250
- */
251
- NgxsStoragePluginOptions.prototype.storage;
252
- /**
253
- * Migration strategies.
254
- * @type {?|undefined}
255
- */
256
- NgxsStoragePluginOptions.prototype.migrations;
257
- /**
258
- * Serailizer for the object before its pushed into the engine.
259
- * @param {?} obj
260
- * @return {?}
261
- */
262
- NgxsStoragePluginOptions.prototype.serialize = function (obj) { };
263
- /**
264
- * Deserializer for the object before its pulled out of the engine.
265
- * @param {?} obj
266
- * @return {?}
267
- */
268
- NgxsStoragePluginOptions.prototype.deserialize = function (obj) { };
269
- /**
270
- * Method to alter object before serialization.
271
- * @param {?} obj
272
- * @param {?} key
273
- * @return {?}
274
- */
275
- NgxsStoragePluginOptions.prototype.beforeSerialize = function (obj, key) { };
276
- /**
277
- * Method to alter object after deserialization.
278
- * @param {?} obj
279
- * @param {?} key
280
- * @return {?}
281
- */
282
- NgxsStoragePluginOptions.prototype.afterDeserialize = function (obj, key) { };
283
- }
284
- /** @type {?} */
285
- var NGXS_STORAGE_PLUGIN_OPTIONS = new core.InjectionToken('NGXS_STORAGE_PLUGIN_OPTION');
286
- /** @type {?} */
287
- var STORAGE_ENGINE = new core.InjectionToken('STORAGE_ENGINE');
288
- /**
289
- * @record
290
- */
291
- function StorageEngine() { }
292
- if (false) {
293
- /** @type {?} */
294
- StorageEngine.prototype.length;
295
- /**
296
- * @param {?} key
297
- * @return {?}
298
- */
299
- StorageEngine.prototype.getItem = function (key) { };
300
- /**
301
- * @param {?} key
302
- * @param {?} val
303
- * @return {?}
304
- */
305
- StorageEngine.prototype.setItem = function (key, val) { };
306
- /**
307
- * @param {?} key
308
- * @return {?}
309
- */
310
- StorageEngine.prototype.removeItem = function (key) { };
311
- /**
312
- * @return {?}
313
- */
314
- StorageEngine.prototype.clear = function () { };
226
+ /**
227
+ * @fileoverview added by tsickle
228
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
229
+ */
230
+ /** @enum {number} */
231
+ var StorageOption = {
232
+ LocalStorage: 0,
233
+ SessionStorage: 1,
234
+ };
235
+ /**
236
+ * @record
237
+ */
238
+ function NgxsStoragePluginOptions() { }
239
+ if (false) {
240
+ /**
241
+ * Key for the state slice to store in the storage engine.
242
+ * @type {?|undefined}
243
+ */
244
+ NgxsStoragePluginOptions.prototype.key;
245
+ /**
246
+ * Storage engine to use. Deaults to localStorage but can provide
247
+ *
248
+ * sessionStorage or custom implementation of the StorageEngine interface
249
+ * @type {?|undefined}
250
+ */
251
+ NgxsStoragePluginOptions.prototype.storage;
252
+ /**
253
+ * Migration strategies.
254
+ * @type {?|undefined}
255
+ */
256
+ NgxsStoragePluginOptions.prototype.migrations;
257
+ /**
258
+ * Serailizer for the object before its pushed into the engine.
259
+ * @param {?} obj
260
+ * @return {?}
261
+ */
262
+ NgxsStoragePluginOptions.prototype.serialize = function (obj) { };
263
+ /**
264
+ * Deserializer for the object before its pulled out of the engine.
265
+ * @param {?} obj
266
+ * @return {?}
267
+ */
268
+ NgxsStoragePluginOptions.prototype.deserialize = function (obj) { };
269
+ /**
270
+ * Method to alter object before serialization.
271
+ * @param {?} obj
272
+ * @param {?} key
273
+ * @return {?}
274
+ */
275
+ NgxsStoragePluginOptions.prototype.beforeSerialize = function (obj, key) { };
276
+ /**
277
+ * Method to alter object after deserialization.
278
+ * @param {?} obj
279
+ * @param {?} key
280
+ * @return {?}
281
+ */
282
+ NgxsStoragePluginOptions.prototype.afterDeserialize = function (obj, key) { };
283
+ }
284
+ /** @type {?} */
285
+ var NGXS_STORAGE_PLUGIN_OPTIONS = new core.InjectionToken('NGXS_STORAGE_PLUGIN_OPTION');
286
+ /** @type {?} */
287
+ var STORAGE_ENGINE = new core.InjectionToken('STORAGE_ENGINE');
288
+ /**
289
+ * @record
290
+ */
291
+ function StorageEngine() { }
292
+ if (false) {
293
+ /** @type {?} */
294
+ StorageEngine.prototype.length;
295
+ /**
296
+ * @param {?} key
297
+ * @return {?}
298
+ */
299
+ StorageEngine.prototype.getItem = function (key) { };
300
+ /**
301
+ * @param {?} key
302
+ * @param {?} val
303
+ * @return {?}
304
+ */
305
+ StorageEngine.prototype.setItem = function (key, val) { };
306
+ /**
307
+ * @param {?} key
308
+ * @return {?}
309
+ */
310
+ StorageEngine.prototype.removeItem = function (key) { };
311
+ /**
312
+ * @return {?}
313
+ */
314
+ StorageEngine.prototype.clear = function () { };
315
315
  }
316
316
 
317
- /**
318
- * @fileoverview added by tsickle
319
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
320
- */
321
- /**
322
- * If the `key` option is not provided then the below constant
323
- * will be used as a default key
324
- * @type {?}
325
- */
326
- var DEFAULT_STATE_KEY = '@@STATE';
327
- /**
328
- * This key is used to retrieve static metadatas on state classes.
329
- * This constant is taken from the core codebase
330
- * @type {?}
331
- */
332
- var META_OPTIONS_KEY = 'NGXS_OPTIONS_META';
333
- /**
334
- * @param {?} key
335
- * @return {?}
336
- */
337
- function transformKeyOption(key) {
338
- if (!Array.isArray(key)) {
339
- key = [key];
340
- }
341
- return key.map((/**
342
- * @param {?} token
343
- * @return {?}
344
- */
345
- function (token) {
346
- // If it has the `NGXS_OPTIONS_META` key then it means the developer
347
- // has provided state class like `key: [AuthState]`.
348
- if (token.hasOwnProperty(META_OPTIONS_KEY)) {
349
- // The `name` property will be an actual state name or a `StateToken`.
350
- token = ((/** @type {?} */ (token)))[META_OPTIONS_KEY].name;
351
- }
352
- return token instanceof store.StateToken ? token.getName() : ((/** @type {?} */ (token)));
353
- }));
354
- }
355
- /**
356
- * @param {?} options
357
- * @return {?}
358
- */
359
- function storageOptionsFactory(options) {
360
- if (options !== undefined && options.key) {
361
- options.key = transformKeyOption(options.key);
362
- }
363
- return __assign({ key: [DEFAULT_STATE_KEY], storage: 0 /* LocalStorage */, serialize: JSON.stringify, deserialize: JSON.parse, beforeSerialize: (/**
364
- * @param {?} obj
365
- * @return {?}
366
- */
367
- function (obj) { return obj; }), afterDeserialize: (/**
368
- * @param {?} obj
369
- * @return {?}
370
- */
371
- function (obj) { return obj; }) }, options);
372
- }
373
- /**
374
- * @param {?} options
375
- * @param {?} platformId
376
- * @return {?}
377
- */
378
- function engineFactory(options, platformId) {
379
- if (common.isPlatformServer(platformId)) {
380
- return null;
381
- }
382
- if (options.storage === 0 /* LocalStorage */) {
383
- return localStorage;
384
- }
385
- else if (options.storage === 1 /* SessionStorage */) {
386
- return sessionStorage;
387
- }
388
- return null;
317
+ /**
318
+ * @fileoverview added by tsickle
319
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
320
+ */
321
+ /**
322
+ * If the `key` option is not provided then the below constant
323
+ * will be used as a default key
324
+ * @type {?}
325
+ */
326
+ var DEFAULT_STATE_KEY = '@@STATE';
327
+ /**
328
+ * This key is used to retrieve static metadatas on state classes.
329
+ * This constant is taken from the core codebase
330
+ * @type {?}
331
+ */
332
+ var META_OPTIONS_KEY = 'NGXS_OPTIONS_META';
333
+ /**
334
+ * @param {?} key
335
+ * @return {?}
336
+ */
337
+ function transformKeyOption(key) {
338
+ if (!Array.isArray(key)) {
339
+ key = [key];
340
+ }
341
+ return key.map((/**
342
+ * @param {?} token
343
+ * @return {?}
344
+ */
345
+ function (token) {
346
+ // If it has the `NGXS_OPTIONS_META` key then it means the developer
347
+ // has provided state class like `key: [AuthState]`.
348
+ if (token.hasOwnProperty(META_OPTIONS_KEY)) {
349
+ // The `name` property will be an actual state name or a `StateToken`.
350
+ token = ((/** @type {?} */ (token)))[META_OPTIONS_KEY].name;
351
+ }
352
+ return token instanceof store.StateToken ? token.getName() : ((/** @type {?} */ (token)));
353
+ }));
354
+ }
355
+ /**
356
+ * @param {?} options
357
+ * @return {?}
358
+ */
359
+ function storageOptionsFactory(options) {
360
+ if (options !== undefined && options.key) {
361
+ options.key = transformKeyOption(options.key);
362
+ }
363
+ return __assign({ key: [DEFAULT_STATE_KEY], storage: 0 /* LocalStorage */, serialize: JSON.stringify, deserialize: JSON.parse, beforeSerialize: (/**
364
+ * @param {?} obj
365
+ * @return {?}
366
+ */
367
+ function (obj) { return obj; }), afterDeserialize: (/**
368
+ * @param {?} obj
369
+ * @return {?}
370
+ */
371
+ function (obj) { return obj; }) }, options);
372
+ }
373
+ /**
374
+ * @param {?} options
375
+ * @param {?} platformId
376
+ * @return {?}
377
+ */
378
+ function engineFactory(options, platformId) {
379
+ if (common.isPlatformServer(platformId)) {
380
+ return null;
381
+ }
382
+ if (options.storage === 0 /* LocalStorage */) {
383
+ return localStorage;
384
+ }
385
+ else if (options.storage === 1 /* SessionStorage */) {
386
+ return sessionStorage;
387
+ }
388
+ return null;
389
389
  }
390
390
 
391
- /**
392
- * @fileoverview added by tsickle
393
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
394
- */
395
- var NgxsStoragePlugin = /** @class */ (function () {
396
- function NgxsStoragePlugin(_options, _engine, _platformId) {
397
- this._options = _options;
398
- this._engine = _engine;
399
- this._platformId = _platformId;
400
- }
401
- /**
402
- * @param {?} state
403
- * @param {?} event
404
- * @param {?} next
405
- * @return {?}
406
- */
407
- NgxsStoragePlugin.prototype.handle = /**
408
- * @param {?} state
409
- * @param {?} event
410
- * @param {?} next
411
- * @return {?}
412
- */
413
- function (state, event, next) {
414
- var _this = this;
415
- var e_1, _a;
416
- if (common.isPlatformServer(this._platformId) && this._engine === null) {
417
- return next(state, event);
418
- }
419
- // We cast to `string[]` here as we're sure that this option has been
420
- // transformed by the `storageOptionsFactory` function that provided token
421
- /** @type {?} */
422
- var keys = (/** @type {?} */ (this._options.key));
423
- /** @type {?} */
424
- var matches = store.actionMatcher(event);
425
- /** @type {?} */
426
- var isInitAction = matches(store.InitState);
427
- /** @type {?} */
428
- var isUpdateAction = matches(store.UpdateState);
429
- /** @type {?} */
430
- var isInitOrUpdateAction = isInitAction || isUpdateAction;
431
- /** @type {?} */
432
- var hasMigration = false;
433
- if (isInitOrUpdateAction) {
434
- var _loop_1 = function (key) {
435
- // We're checking what states have been added by NGXS and if any of these states should be handled by
436
- // the storage plugin. For instance, we only want to deserialize the `auth` state, NGXS has added
437
- // the `user` state, the storage plugin will be rerun and will do redundant deserialization.
438
- if (isUpdateAction && event.addedStates && !event.addedStates.hasOwnProperty(key)) {
439
- return "continue";
440
- }
441
- /** @type {?} */
442
- var isMaster = key === DEFAULT_STATE_KEY;
443
- /** @type {?} */
444
- var val = this_1._engine.getItem((/** @type {?} */ (key)));
445
- if (val !== 'undefined' && val != null) {
446
- try {
447
- /** @type {?} */
448
- var newVal = (/** @type {?} */ (this_1._options.deserialize))(val);
449
- val = (/** @type {?} */ (this_1._options.afterDeserialize))(newVal, key);
450
- }
451
- catch (e) {
452
- // Caretaker note: we have still left the `typeof` condition in order to avoid
453
- // creating a breaking change for projects that still use the View Engine.
454
- if (typeof ngDevMode === 'undefined' || ngDevMode) {
455
- console.error("Error ocurred while deserializing the " + key + " store value, falling back to empty object, the value obtained from the store: ", val);
456
- }
457
- val = {};
458
- }
459
- if (this_1._options.migrations) {
460
- this_1._options.migrations.forEach((/**
461
- * @param {?} strategy
462
- * @return {?}
463
- */
464
- function (strategy) {
465
- /** @type {?} */
466
- var versionMatch = strategy.version === store.getValue(val, strategy.versionKey || 'version');
467
- /** @type {?} */
468
- var keyMatch = (!strategy.key && isMaster) || strategy.key === key;
469
- if (versionMatch && keyMatch) {
470
- val = strategy.migrate(val);
471
- hasMigration = true;
472
- }
473
- }));
474
- }
475
- if (!isMaster) {
476
- state = store.setValue(state, (/** @type {?} */ (key)), val);
477
- }
478
- else {
479
- state = __assign({}, state, val);
480
- }
481
- }
482
- };
483
- var this_1 = this;
484
- try {
485
- for (var keys_1 = __values(keys), keys_1_1 = keys_1.next(); !keys_1_1.done; keys_1_1 = keys_1.next()) {
486
- var key = keys_1_1.value;
487
- _loop_1(key);
488
- }
489
- }
490
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
491
- finally {
492
- try {
493
- if (keys_1_1 && !keys_1_1.done && (_a = keys_1.return)) _a.call(keys_1);
494
- }
495
- finally { if (e_1) throw e_1.error; }
496
- }
497
- }
498
- return next(state, event).pipe(operators.tap((/**
499
- * @param {?} nextState
500
- * @return {?}
501
- */
502
- function (nextState) {
503
- var e_2, _a;
504
- if (!isInitOrUpdateAction || (isInitOrUpdateAction && hasMigration)) {
505
- try {
506
- for (var keys_2 = __values(keys), keys_2_1 = keys_2.next(); !keys_2_1.done; keys_2_1 = keys_2.next()) {
507
- var key = keys_2_1.value;
508
- /** @type {?} */
509
- var val = nextState;
510
- if (key !== DEFAULT_STATE_KEY) {
511
- val = store.getValue(nextState, (/** @type {?} */ (key)));
512
- }
513
- try {
514
- /** @type {?} */
515
- var newVal = (/** @type {?} */ (_this._options.beforeSerialize))(val, key);
516
- _this._engine.setItem((/** @type {?} */ (key)), (/** @type {?} */ (_this._options.serialize))(newVal));
517
- }
518
- catch (e) {
519
- // Caretaker note: we have still left the `typeof` condition in order to avoid
520
- // creating a breaking change for projects that still use the View Engine.
521
- if (typeof ngDevMode === 'undefined' || ngDevMode) {
522
- console.error("Error ocurred while serializing the " + key + " store value, value not updated, the value obtained from the store: ", val);
523
- }
524
- }
525
- }
526
- }
527
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
528
- finally {
529
- try {
530
- if (keys_2_1 && !keys_2_1.done && (_a = keys_2.return)) _a.call(keys_2);
531
- }
532
- finally { if (e_2) throw e_2.error; }
533
- }
534
- }
535
- })));
536
- };
537
- NgxsStoragePlugin.decorators = [
538
- { type: core.Injectable }
539
- ];
540
- /** @nocollapse */
541
- NgxsStoragePlugin.ctorParameters = function () { return [
542
- { type: undefined, decorators: [{ type: core.Inject, args: [NGXS_STORAGE_PLUGIN_OPTIONS,] }] },
543
- { type: undefined, decorators: [{ type: core.Inject, args: [STORAGE_ENGINE,] }] },
544
- { type: String, decorators: [{ type: core.Inject, args: [core.PLATFORM_ID,] }] }
545
- ]; };
546
- return NgxsStoragePlugin;
547
- }());
548
- if (false) {
549
- /**
550
- * @type {?}
551
- * @private
552
- */
553
- NgxsStoragePlugin.prototype._options;
554
- /**
555
- * @type {?}
556
- * @private
557
- */
558
- NgxsStoragePlugin.prototype._engine;
559
- /**
560
- * @type {?}
561
- * @private
562
- */
563
- NgxsStoragePlugin.prototype._platformId;
391
+ /**
392
+ * @fileoverview added by tsickle
393
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
394
+ */
395
+ var NgxsStoragePlugin = /** @class */ (function () {
396
+ function NgxsStoragePlugin(_options, _engine, _platformId) {
397
+ this._options = _options;
398
+ this._engine = _engine;
399
+ this._platformId = _platformId;
400
+ // We cast to `string[]` here as we're sure that this option has been
401
+ // transformed by the `storageOptionsFactory` function that provided token.
402
+ this._keys = (/** @type {?} */ (this._options.key));
403
+ // We default to `[DEFAULT_STATE_KEY]` if the user explicitly does not provide the `key` option.
404
+ this._usesDefaultStateKey = this._keys.length === 1 && this._keys[0] === DEFAULT_STATE_KEY;
405
+ }
406
+ /**
407
+ * @param {?} state
408
+ * @param {?} event
409
+ * @param {?} next
410
+ * @return {?}
411
+ */
412
+ NgxsStoragePlugin.prototype.handle = /**
413
+ * @param {?} state
414
+ * @param {?} event
415
+ * @param {?} next
416
+ * @return {?}
417
+ */
418
+ function (state, event, next) {
419
+ var _this = this;
420
+ var e_1, _a;
421
+ if (common.isPlatformServer(this._platformId) && this._engine === null) {
422
+ return next(state, event);
423
+ }
424
+ /** @type {?} */
425
+ var matches = store.actionMatcher(event);
426
+ /** @type {?} */
427
+ var isInitAction = matches(store.InitState);
428
+ /** @type {?} */
429
+ var isUpdateAction = matches(store.UpdateState);
430
+ /** @type {?} */
431
+ var isInitOrUpdateAction = isInitAction || isUpdateAction;
432
+ /** @type {?} */
433
+ var hasMigration = false;
434
+ if (isInitOrUpdateAction) {
435
+ /** @type {?} */
436
+ var addedStates = isUpdateAction && event.addedStates;
437
+ var _loop_1 = function (key) {
438
+ // We're checking what states have been added by NGXS and if any of these states should be handled by
439
+ // the storage plugin. For instance, we only want to deserialize the `auth` state, NGXS has added
440
+ // the `user` state, the storage plugin will be rerun and will do redundant deserialization.
441
+ // `usesDefaultStateKey` is necessary to check since `event.addedStates` never contains `@@STATE`.
442
+ if (!this_1._usesDefaultStateKey && addedStates && !addedStates.hasOwnProperty(key)) {
443
+ return "continue";
444
+ }
445
+ /** @type {?} */
446
+ var storedValue = this_1._engine.getItem((/** @type {?} */ (key)));
447
+ if (storedValue !== 'undefined' && storedValue != null) {
448
+ try {
449
+ /** @type {?} */
450
+ var newVal = (/** @type {?} */ (this_1._options.deserialize))(storedValue);
451
+ storedValue = (/** @type {?} */ (this_1._options.afterDeserialize))(newVal, key);
452
+ }
453
+ catch (_a) {
454
+ // Caretaker note: we have still left the `typeof` condition in order to avoid
455
+ // creating a breaking change for projects that still use the View Engine.
456
+ if (typeof ngDevMode === 'undefined' || ngDevMode) {
457
+ console.error("Error ocurred while deserializing the " + key + " store value, falling back to empty object, the value obtained from the store: ", storedValue);
458
+ }
459
+ storedValue = {};
460
+ }
461
+ if (this_1._options.migrations) {
462
+ this_1._options.migrations.forEach((/**
463
+ * @param {?} strategy
464
+ * @return {?}
465
+ */
466
+ function (strategy) {
467
+ /** @type {?} */
468
+ var versionMatch = strategy.version === store.getValue(storedValue, strategy.versionKey || 'version');
469
+ /** @type {?} */
470
+ var keyMatch = (!strategy.key && _this._usesDefaultStateKey) || strategy.key === key;
471
+ if (versionMatch && keyMatch) {
472
+ storedValue = strategy.migrate(storedValue);
473
+ hasMigration = true;
474
+ }
475
+ }));
476
+ }
477
+ if (!this_1._usesDefaultStateKey) {
478
+ state = store.setValue(state, (/** @type {?} */ (key)), storedValue);
479
+ }
480
+ else {
481
+ // The `UpdateState` action is dispatched whenever the feature state is added.
482
+ // The below condition is met only when the `UpdateState` is dispatched.
483
+ // Let's assume that we have 2 states `counter` and `@ngxs/router-plugin` state.
484
+ // `CounterState` is provided on the root level when calling `NgxsModule.forRoot()`
485
+ // and `@ngxs/router-plugin` is provided as a feature state.
486
+ // The storage plugin may save the `counter` state value as `10` before.
487
+ // The `CounterState` may implement the `ngxsOnInit` hook and call `ctx.setState(999)`.
488
+ // The storage plugin will re-hydrate the whole state when the `RouterState` is registered,
489
+ // and the `counter` state will again equal `10` (not `999`).
490
+ if (storedValue && addedStates && Object.keys(addedStates).length > 0) {
491
+ storedValue = Object.keys(addedStates).reduce((/**
492
+ * @param {?} accumulator
493
+ * @param {?} addedState
494
+ * @return {?}
495
+ */
496
+ function (accumulator, addedState) {
497
+ // The `storedValue` may equal the whole state (when the default state key is used).
498
+ // If `addedStates` contains only `router` then we want to merge the state only
499
+ // with the `router` value.
500
+ // Let's assume that the `storedValue` is an object:
501
+ // `{ counter: 10, router: {...} }`
502
+ // This will pick only the `router` object from the `storedValue` and `counter`
503
+ // state will not be re-hydrated unnecessary.
504
+ if (storedValue.hasOwnProperty(addedState)) {
505
+ accumulator[addedState] = storedValue[addedState];
506
+ }
507
+ return accumulator;
508
+ }), (/** @type {?} */ ({})));
509
+ }
510
+ state = __assign({}, state, storedValue);
511
+ }
512
+ }
513
+ };
514
+ var this_1 = this;
515
+ try {
516
+ for (var _b = __values(this._keys), _c = _b.next(); !_c.done; _c = _b.next()) {
517
+ var key = _c.value;
518
+ _loop_1(key);
519
+ }
520
+ }
521
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
522
+ finally {
523
+ try {
524
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
525
+ }
526
+ finally { if (e_1) throw e_1.error; }
527
+ }
528
+ }
529
+ return next(state, event).pipe(operators.tap((/**
530
+ * @param {?} nextState
531
+ * @return {?}
532
+ */
533
+ function (nextState) {
534
+ var e_2, _a;
535
+ if (!isInitOrUpdateAction || (isInitOrUpdateAction && hasMigration)) {
536
+ try {
537
+ for (var _b = __values(_this._keys), _c = _b.next(); !_c.done; _c = _b.next()) {
538
+ var key = _c.value;
539
+ /** @type {?} */
540
+ var val = nextState;
541
+ if (key !== DEFAULT_STATE_KEY) {
542
+ val = store.getValue(nextState, (/** @type {?} */ (key)));
543
+ }
544
+ try {
545
+ /** @type {?} */
546
+ var newVal = (/** @type {?} */ (_this._options.beforeSerialize))(val, key);
547
+ _this._engine.setItem((/** @type {?} */ (key)), (/** @type {?} */ (_this._options.serialize))(newVal));
548
+ }
549
+ catch (error) {
550
+ // Caretaker note: we have still left the `typeof` condition in order to avoid
551
+ // creating a breaking change for projects that still use the View Engine.
552
+ if (typeof ngDevMode === 'undefined' || ngDevMode) {
553
+ if (error &&
554
+ (error.name === 'QuotaExceededError' ||
555
+ error.name === 'NS_ERROR_DOM_QUOTA_REACHED')) {
556
+ console.error("The " + key + " store value exceeds the browser storage quota: ", val);
557
+ }
558
+ else {
559
+ console.error("Error ocurred while serializing the " + key + " store value, value not updated, the value obtained from the store: ", val);
560
+ }
561
+ }
562
+ }
563
+ }
564
+ }
565
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
566
+ finally {
567
+ try {
568
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
569
+ }
570
+ finally { if (e_2) throw e_2.error; }
571
+ }
572
+ }
573
+ })));
574
+ };
575
+ NgxsStoragePlugin.decorators = [
576
+ { type: core.Injectable }
577
+ ];
578
+ /** @nocollapse */
579
+ NgxsStoragePlugin.ctorParameters = function () { return [
580
+ { type: undefined, decorators: [{ type: core.Inject, args: [NGXS_STORAGE_PLUGIN_OPTIONS,] }] },
581
+ { type: undefined, decorators: [{ type: core.Inject, args: [STORAGE_ENGINE,] }] },
582
+ { type: String, decorators: [{ type: core.Inject, args: [core.PLATFORM_ID,] }] }
583
+ ]; };
584
+ return NgxsStoragePlugin;
585
+ }());
586
+ if (false) {
587
+ /**
588
+ * @type {?}
589
+ * @private
590
+ */
591
+ NgxsStoragePlugin.prototype._keys;
592
+ /**
593
+ * @type {?}
594
+ * @private
595
+ */
596
+ NgxsStoragePlugin.prototype._usesDefaultStateKey;
597
+ /**
598
+ * @type {?}
599
+ * @private
600
+ */
601
+ NgxsStoragePlugin.prototype._options;
602
+ /**
603
+ * @type {?}
604
+ * @private
605
+ */
606
+ NgxsStoragePlugin.prototype._engine;
607
+ /**
608
+ * @type {?}
609
+ * @private
610
+ */
611
+ NgxsStoragePlugin.prototype._platformId;
564
612
  }
565
613
 
566
- /**
567
- * @fileoverview added by tsickle
568
- * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
569
- */
570
- /** @type {?} */
571
- var USER_OPTIONS = new core.InjectionToken('USER_OPTIONS');
572
- var NgxsStoragePluginModule = /** @class */ (function () {
573
- function NgxsStoragePluginModule() {
574
- }
575
- /**
576
- * @param {?=} options
577
- * @return {?}
578
- */
579
- NgxsStoragePluginModule.forRoot = /**
580
- * @param {?=} options
581
- * @return {?}
582
- */
583
- function (options) {
584
- return {
585
- ngModule: NgxsStoragePluginModule,
586
- providers: [
587
- {
588
- provide: store.NGXS_PLUGINS,
589
- useClass: NgxsStoragePlugin,
590
- multi: true
591
- },
592
- {
593
- provide: USER_OPTIONS,
594
- useValue: options
595
- },
596
- {
597
- provide: NGXS_STORAGE_PLUGIN_OPTIONS,
598
- useFactory: storageOptionsFactory,
599
- deps: [USER_OPTIONS]
600
- },
601
- {
602
- provide: STORAGE_ENGINE,
603
- useFactory: engineFactory,
604
- deps: [NGXS_STORAGE_PLUGIN_OPTIONS, core.PLATFORM_ID]
605
- }
606
- ]
607
- };
608
- };
609
- NgxsStoragePluginModule.decorators = [
610
- { type: core.NgModule }
611
- ];
612
- return NgxsStoragePluginModule;
614
+ /**
615
+ * @fileoverview added by tsickle
616
+ * @suppress {checkTypes,extraRequire,missingOverride,missingReturn,unusedPrivateMembers,uselessCode} checked by tsc
617
+ */
618
+ /** @type {?} */
619
+ var USER_OPTIONS = new core.InjectionToken('USER_OPTIONS');
620
+ var NgxsStoragePluginModule = /** @class */ (function () {
621
+ function NgxsStoragePluginModule() {
622
+ }
623
+ /**
624
+ * @param {?=} options
625
+ * @return {?}
626
+ */
627
+ NgxsStoragePluginModule.forRoot = /**
628
+ * @param {?=} options
629
+ * @return {?}
630
+ */
631
+ function (options) {
632
+ return {
633
+ ngModule: NgxsStoragePluginModule,
634
+ providers: [
635
+ {
636
+ provide: store.NGXS_PLUGINS,
637
+ useClass: NgxsStoragePlugin,
638
+ multi: true
639
+ },
640
+ {
641
+ provide: USER_OPTIONS,
642
+ useValue: options
643
+ },
644
+ {
645
+ provide: NGXS_STORAGE_PLUGIN_OPTIONS,
646
+ useFactory: storageOptionsFactory,
647
+ deps: [USER_OPTIONS]
648
+ },
649
+ {
650
+ provide: STORAGE_ENGINE,
651
+ useFactory: engineFactory,
652
+ deps: [NGXS_STORAGE_PLUGIN_OPTIONS, core.PLATFORM_ID]
653
+ }
654
+ ]
655
+ };
656
+ };
657
+ NgxsStoragePluginModule.decorators = [
658
+ { type: core.NgModule }
659
+ ];
660
+ return NgxsStoragePluginModule;
613
661
  }());
614
662
 
615
663
  exports.NGXS_STORAGE_PLUGIN_OPTIONS = NGXS_STORAGE_PLUGIN_OPTIONS;