From 06ae6d76cb12eafe854413a9a8ee81b01ec427b7 Mon Sep 17 00:00:00 2001 From: Brian Malinconico Date: Wed, 6 Jun 2018 09:21:31 -0400 Subject: [PATCH 1/2] Fixing partial state rehydration with lazy loaded modules [Closes #93] --- spec/index_spec.ts | 22 ++++++++++++++++++++-- src/index.ts | 33 ++++++++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/spec/index_spec.ts b/spec/index_spec.ts index 9ab5827..08ae30b 100644 --- a/spec/index_spec.ts +++ b/spec/index_spec.ts @@ -1,6 +1,6 @@ declare var beforeEachProviders, it, describe, expect, inject; require('es6-shim'); -import { syncStateUpdate, rehydrateApplicationState, dateReviver } from '../src/index'; +import { syncStateUpdate, rehydrateApplicationState, dateReviver, mergePartialStates } from '../src/index'; import * as CryptoJS from 'crypto-js'; // Very simple classes to test serialization options. They cover string, number, date, and nested classes @@ -374,4 +374,22 @@ describe('ngrxLocalStorage', () => { expect(t1 instanceof TypeA).toBeTruthy(); expect(finalState.simple instanceof TypeA).toBeFalsy(); }); -}); \ No newline at end of file + + it('merges partial states', () => { + const keyDef = ['foo', { bar: ['baz'] }]; + expect(true).toEqual(false); + expect( + mergePartialStates( + keyDef, + { bar: {baz: 123, nitch: 456} }, + { foo: 'hello', bar: {baz: 1} }, + ) + ).toEqual({ + foo: 'hello', + bar: { + baz: 1, + nitch: 456 + } + }); + }); +}); diff --git a/src/index.ts b/src/index.ts index 70a29e2..94638ca 100644 --- a/src/index.ts +++ b/src/index.ts @@ -232,7 +232,7 @@ export const localStorageSync = (config: LocalStorageConfig) => ( (action.type === INIT_ACTION || action.type === UPDATE_ACTION) && rehydratedState ) { - state = Object.assign({}, state, rehydratedState); + state = mergePartialStates(stateKeys, state, rehydratedState); } const nextState = reducer(state, action); syncStateUpdate( @@ -246,6 +246,37 @@ export const localStorageSync = (config: LocalStorageConfig) => ( }; }; +export const mergePartialStates = (stateKeys: (string | { [idx: string]: null | undefined | string[] })[], currentState: {}, rehydratedState: {}) => { + let finalState = { ...currentState }; + + stateKeys.map((key) => { + if ( typeof key === 'string' ) { + finalState = { + ...finalState, + [key]: rehydratedState[key], + }; + } else { + const name = Object.keys(key)[0]; + const newValues = {}; + + const keys = key[name] + if (keys) { + keys.map((key) => newValues[key] = rehydratedState[name][key]) + } + + finalState = { + ...finalState, + [name]: { + ...finalState[name], + ...newValues, + }, + }; + } + }); + + return finalState; +} + /* @deprecated: Use localStorageSync(LocalStorageConfig) From 576c861528e1b724ac903c8897ecce20277510bb Mon Sep 17 00:00:00 2001 From: Brian Malinconico Date: Wed, 6 Jun 2018 09:25:33 -0400 Subject: [PATCH 2/2] Removing test spec failure --- spec/index_spec.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/index_spec.ts b/spec/index_spec.ts index 08ae30b..3b05c16 100644 --- a/spec/index_spec.ts +++ b/spec/index_spec.ts @@ -377,7 +377,6 @@ describe('ngrxLocalStorage', () => { it('merges partial states', () => { const keyDef = ['foo', { bar: ['baz'] }]; - expect(true).toEqual(false); expect( mergePartialStates( keyDef,