ngrx-store-localstorage
Advanced tools
Comparing version 6.0.0 to 7.0.0
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
var _this = this; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var merge = require("lodash.merge"); | ||
var INIT_ACTION = '@ngrx/store/init'; | ||
@@ -188,29 +197,18 @@ var UPDATE_ACTION = '@ngrx/store/update-reducers'; | ||
return function (state, action) { | ||
if (state === void 0) { state = rehydratedState; } | ||
/* | ||
Handle case where state is rehydrated AND initial state is supplied. | ||
Any additional state supplied will override rehydrated state for the given key. | ||
*/ | ||
if ((action.type === INIT_ACTION || action.type === UPDATE_ACTION) && | ||
rehydratedState) { | ||
if (state) { | ||
Object.keys(state).forEach(function (key) { | ||
if (state[key] instanceof Array && rehydratedState[key] instanceof Array) { | ||
state[key] = rehydratedState[key]; | ||
} | ||
else if (typeof state[key] === 'object' | ||
&& typeof rehydratedState[key] === 'object') { | ||
state[key] = Object.assign({}, state[key], rehydratedState[key]); | ||
} | ||
else { | ||
state[key] = rehydratedState[key]; | ||
} | ||
}); | ||
} | ||
else { | ||
state = Object.assign({}, state, rehydratedState); | ||
} | ||
var nextState; | ||
// If state arrives undefined, we need to let it through the supplied reducer | ||
// in order to get a complete state as defined by user | ||
if ((action.type === INIT_ACTION) && !state) { | ||
nextState = reducer(state, action); | ||
} | ||
var nextState = reducer(state, action); | ||
exports.syncStateUpdate(nextState, stateKeys, config.storage, config.storageKeySerializer, config.removeOnUndefined, config.syncCondition); | ||
else { | ||
nextState = __assign({}, state); | ||
} | ||
if ((action.type === INIT_ACTION || action.type === UPDATE_ACTION) && rehydratedState) { | ||
nextState = merge({}, nextState, rehydratedState); | ||
} | ||
nextState = reducer(nextState, action); | ||
if (action.type !== INIT_ACTION) { | ||
exports.syncStateUpdate(nextState, stateKeys, config.storage, config.storageKeySerializer, config.removeOnUndefined, config.syncCondition); | ||
} | ||
return nextState; | ||
@@ -217,0 +215,0 @@ }; |
{ | ||
"name": "ngrx-store-localstorage", | ||
"version": "6.0.0", | ||
"version": "7.0.0", | ||
"description": "State and local storage syncing for @ngrx/store", | ||
@@ -57,3 +57,6 @@ "main": "./dist/index.js", | ||
}, | ||
"typings": "./dist/index.d.ts" | ||
"typings": "./dist/index.d.ts", | ||
"dependencies": { | ||
"lodash.merge": "^4.6.1" | ||
} | ||
} |
@@ -170,2 +170,26 @@ declare var it, describe, expect; | ||
it('filtered - multiple keys at root - should properly revive partial state', function () { | ||
const s = new MockStorage(); | ||
const skr = mockStorageKeySerializer; | ||
// state at any given moment, subject to sync selectively | ||
const nestedState = { | ||
app: { app1: true, app2: [1, 2], app3: { any: 'thing' } }, | ||
feature1: { slice11: true, slice12: [1, 2], slice13: { any: 'thing' } }, | ||
feature2: { slice21: true, slice22: [1, 2], slice23: { any: 'thing' } }, | ||
}; | ||
// test selective write to storage | ||
syncStateUpdate(nestedState, [ | ||
{ 'feature1': ['slice11', 'slice12'] }, | ||
{ 'feature2': ['slice21', 'slice22'] }, | ||
], s, skr, false); | ||
const raw1 = s.getItem('feature1'); | ||
expect(raw1).toEqual(jasmine.arrayContaining(['slice11', 'slice12'])); | ||
const raw2 = s.getItem('feature2'); | ||
expect(raw2).toEqual(jasmine.arrayContaining(['slice21', 'slice22'])); | ||
}); | ||
it('reviver', () => { | ||
@@ -437,2 +461,31 @@ // Use the reviver option to restore including classes | ||
}); | ||
}); | ||
it('should merge selectively saved state and rehydrated state', () => { | ||
const initialState = { | ||
app: { app1: false, app2: [], app3: {} }, | ||
feature1: { slice11: false, slice12: [], slice13: {} }, | ||
feature2: { slice21: false, slice22: [], slice23: {} }, | ||
}; | ||
// A legit case where state is saved in chunks rather than as a single object | ||
localStorage.setItem('feature1', JSON.stringify({ slice11: true, slice12: [1, 2] })); | ||
localStorage.setItem('feature2', JSON.stringify({ slice21: true, slice22: [1, 2] })); | ||
// Set up reducers | ||
const reducer = (state = initialState, action) => state; | ||
const metaReducer = localStorageSync({keys: [ | ||
{'feature1': ['slice11', 'slice12']}, | ||
{'feature2': ['slice21', 'slice22']}, | ||
], rehydrate: true}); | ||
const action = {type: INIT_ACTION}; | ||
// Resultant state should merge the rehydrated partial state and our initial state | ||
const finalState = metaReducer(reducer)(initialState, action); | ||
expect(finalState).toEqual({ | ||
app: { app1: false, app2: [], app3: {} }, | ||
feature1: { slice11: true, slice12: [1, 2], slice13: {} }, | ||
feature2: { slice21: true, slice22: [1, 2], slice23: {} }, | ||
}); | ||
}); | ||
}); |
@@ -0,1 +1,3 @@ | ||
import * as merge from 'lodash.merge'; | ||
const INIT_ACTION = '@ngrx/store/init'; | ||
@@ -240,35 +242,30 @@ const UPDATE_ACTION = '@ngrx/store/update-reducers'; | ||
return function(state = rehydratedState, action: any) { | ||
/* | ||
Handle case where state is rehydrated AND initial state is supplied. | ||
Any additional state supplied will override rehydrated state for the given key. | ||
*/ | ||
if ( | ||
(action.type === INIT_ACTION || action.type === UPDATE_ACTION) && | ||
rehydratedState | ||
) { | ||
if (state) { | ||
Object.keys(state).forEach(function (key) { | ||
if (state[key] instanceof Array && rehydratedState[key] instanceof Array) { | ||
state[key] = rehydratedState[key]; | ||
} else if (typeof state[key] === 'object' | ||
&& typeof rehydratedState[key] === 'object') { | ||
state[key] = Object.assign({}, state[key], rehydratedState[key]); | ||
} else { | ||
state[key] = rehydratedState[key]; | ||
} | ||
}); | ||
} else { | ||
state = Object.assign({}, state, rehydratedState); | ||
} | ||
return function (state, action: any) { | ||
let nextState; | ||
// If state arrives undefined, we need to let it through the supplied reducer | ||
// in order to get a complete state as defined by user | ||
if ((action.type === INIT_ACTION) && !state) { | ||
nextState = reducer(state, action); | ||
} else { | ||
nextState = { ...state }; | ||
} | ||
const nextState = reducer(state, action); | ||
syncStateUpdate( | ||
nextState, | ||
stateKeys, | ||
config.storage, | ||
config.storageKeySerializer, | ||
config.removeOnUndefined, | ||
config.syncCondition | ||
); | ||
if ((action.type === INIT_ACTION || action.type === UPDATE_ACTION) && rehydratedState) { | ||
nextState = merge({}, nextState, rehydratedState); | ||
} | ||
nextState = reducer(nextState, action); | ||
if (action.type !== INIT_ACTION) { | ||
syncStateUpdate( | ||
nextState, | ||
stateKeys, | ||
config.storage, | ||
config.storageKeySerializer, | ||
config.removeOnUndefined, | ||
config.syncCondition, | ||
); | ||
} | ||
return nextState; | ||
@@ -275,0 +272,0 @@ }; |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
60543
1004
2
+ Addedlodash.merge@^4.6.1
+ Addedlodash.merge@4.6.2(transitive)