-
Notifications
You must be signed in to change notification settings - Fork 916
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(core): rework local-storage initial state hydratation
- prevent loss of initial state of lazy loaded reducers - use meta reducer to merge local-storage state on both INIT and UPDATE (reducers) events - add debug meta reducer in DEV mode - run tests with PROD env to prevent debug reducer logging - standardize payloads (always use payload object with typed properties) Fixes #3, #34
- Loading branch information
1 parent
105408c
commit 42a37d9
Showing
4 changed files
with
43 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { ActionReducer } from '@ngrx/store'; | ||
|
||
export function debug(reducer: ActionReducer<any>): ActionReducer<any> { | ||
return function(state, action) { | ||
const newState = reducer(state, action); | ||
console.log(`[DEBUG] action: ${action.type}`, { | ||
payload: (<any>action).payload, | ||
oldState: state, | ||
newState | ||
}); | ||
return newState; | ||
}; | ||
} |
19 changes: 19 additions & 0 deletions
19
src/app/core/meta-reducers/init-state-from-local-storage.reducer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { ActionReducer, INIT, UPDATE } from '@ngrx/store'; | ||
|
||
import { LocalStorageService } from '../local-storage/local-storage.service'; | ||
|
||
export function initStateFromLocalStorage( | ||
reducer: ActionReducer<any> | ||
): ActionReducer<any> { | ||
return function(state, action) { | ||
const newState = reducer(state, action); | ||
if ([INIT.toString(), UPDATE.toString()].includes(action.type)) { | ||
return Object.assign( | ||
{}, | ||
newState, | ||
LocalStorageService.loadInitialState() | ||
); | ||
} | ||
return newState; | ||
}; | ||
} |