Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Fix for state mutation when rehydrating state #113

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,24 +35,25 @@
"@ngrx/store": "^4.0.0 || ^5.0.0 || ^6.0.0 || ^7.0.0"
},
"devDependencies": {
"@angular/core": "^2.4.7",
"@angular/core": "^7.2.0",
"@ngrx/core": "^1.2.0",
"@ngrx/store": "^7.0.0",
"@types/core-js": "^0.9.35",
"@types/core-js": "^2.5.0",
"@types/crypto-js": "^3.1.33",
"@types/jasmine": "^2.5.47",
"@types/node": "^7.0.18",
"@types/node": "^10.12.18",
"crypto-js": "^3.1.9-1",
"es6-promise": "^3.0.2",
"es6-promise": "^4.2.5",
"es6-shim": "^0.35.0",
"jasmine": "^2.4.1",
"jasmine-core": "^2.4.1",
"localstorage-polyfill": "^1.0.1",
"ngrx-store-freeze": "^0.2.4",
"rimraf": "^2.5.4",
"rxjs": "^5.1.1",
"tslint": "^4.0.2",
"typescript": "^2.1.4",
"zone.js": "^0.7.7"
"rxjs": "^6.3.3",
"tslint": "^5.12.1",
"typescript": "~3.1.0",
"zone.js": "^0.8.27"
},
"typings": "./dist/index.d.ts"
}
3 changes: 2 additions & 1 deletion spec/index_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ declare var it, describe, expect;
require('es6-shim');
import { syncStateUpdate, rehydrateApplicationState, dateReviver, localStorageSync } from '../src/index';
import * as CryptoJS from 'crypto-js';
import { storeFreeze } from 'ngrx-store-freeze';
import 'localstorage-polyfill';
const INIT_ACTION = '@ngrx/store/init';

Expand Down Expand Up @@ -430,7 +431,7 @@ describe('ngrxLocalStorage', () => {
const action = {type: INIT_ACTION};

// Resultant state should merge the oldstring state and our initual state
const finalState = metaReducer(reducer)(initialState, action);
const finalState = storeFreeze(metaReducer(reducer))(initialState, action);
expect(finalState.state.astring).toEqual(initialState.state.astring);
});
});
5 changes: 3 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -249,10 +249,11 @@ export const localStorageSync = (config: LocalStorageConfig) => (
if (state) {
Object.keys(state).forEach(function (key) {
if (state[key] instanceof Array && rehydratedState[key] instanceof Array) {
state[key] = rehydratedState[key];
state = {...state, [key]: rehydratedState[key]};
} else if (typeof state[key] === 'object'
&& typeof rehydratedState[key] === 'object') {
state[key] = Object.assign({}, state[key], rehydratedState[key]);
const slice = Object.assign({}, state[key], rehydratedState[key]);
state = {...state, key: slice};
} else {
state[key] = rehydratedState[key];
}
Expand Down