Skip to content

Commit

Permalink
fix(core): rework local-storage initial state hydratation
Browse files Browse the repository at this point in the history
- 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
tomastrajan committed Jan 13, 2018
1 parent 105408c commit 42a37d9
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"clean": "rm -rf ./dist/",
"server": "cd dist && http-server",
"prod": "npm run clean && npm run build:prod && npm run server",
"ci": "npm run clean && npm run prettier:ci && ng lint && ng test --browser ChromeTravisCi --single-run --reporters spec && npm run build:travisci",
"ci": "npm run clean && npm run prettier:ci && ng lint && ng test --browser ChromeTravisCi --single-run --reporters spec --environment prod && npm run build:travisci",
"release": "standard-version && git push --follow-tags origin master",
"prettier": "prettier --single-quote --parser typescript --write \"./src/**/*.ts\" && prettier --single-quote --parser scss --write \"./src/**/*.scss\"",
"prettier:ci": "prettier --single-quote --parser typescript --list-different \"./src/**/*.ts\" && prettier --single-quote --parser scss --list-different \"./src/**/*.scss\"",
Expand Down
14 changes: 10 additions & 4 deletions src/app/core/core.module.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,21 @@
import { NgModule, Optional, SkipSelf } from '@angular/core';
import { CommonModule } from '@angular/common';
import { HttpClientModule } from '@angular/common/http';
import { StoreModule } from '@ngrx/store';
import { MetaReducer, StoreModule } from '@ngrx/store';
import { EffectsModule } from '@ngrx/effects';

import { environment } from '@env/environment';

import { debug } from './meta-reducers/debug.reducer';
import { initStateFromLocalStorage } from './meta-reducers/init-state-from-local-storage.reducer';
import { LocalStorageService } from './local-storage/local-storage.service';
import { authReducer } from './auth/auth.reducer';
import { AuthEffects } from './auth/auth.effects';

export function getInitialState() {
return LocalStorageService.loadInitialState();
export const metaReducers: MetaReducer<any>[] = [initStateFromLocalStorage];

if (!environment.production) {
metaReducers.unshift(debug);
}

@NgModule({
Expand All @@ -23,7 +29,7 @@ export function getInitialState() {
{
auth: authReducer
},
{ initialState: getInitialState }
{ metaReducers }
),
EffectsModule.forRoot([AuthEffects])
],
Expand Down
13 changes: 13 additions & 0 deletions src/app/core/meta-reducers/debug.reducer.ts
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;
};
}
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;
};
}

0 comments on commit 42a37d9

Please sign in to comment.