From f9da8d2e6e21b4f32278100b908bf5165e7f7950 Mon Sep 17 00:00:00 2001 From: Maxime ROBERT Date: Sun, 30 Jul 2017 14:14:15 +0200 Subject: [PATCH] chore: upgrade to ngrx v4 This closes #5 --- package.json | 6 +++--- src/app/core/core.module.ts | 20 +++++++++++++------- src/app/shared/states/root.reducer.ts | 24 ++++++++---------------- yarn.lock | 18 +++++++++--------- 4 files changed, 33 insertions(+), 35 deletions(-) diff --git a/package.json b/package.json index 1743e54..48ecbfe 100644 --- a/package.json +++ b/package.json @@ -42,9 +42,9 @@ "@angular/router": "4.3.2", "@angular/service-worker": "1.0.0-beta.16", "@ngrx/core": "1.2.0", - "@ngrx/effects": "2.0.4", - "@ngrx/store": "2.2.3", - "@ngrx/store-devtools": "3.2.4", + "@ngrx/effects": "4.0.5", + "@ngrx/store": "4.0.3", + "@ngrx/store-devtools": "4.0.0", "angular2-prettyjson": "2.0.5", "core-js": "2.4.1", "hammerjs": "2.0.8", diff --git a/src/app/core/core.module.ts b/src/app/core/core.module.ts index ba1d404..b644a9c 100644 --- a/src/app/core/core.module.ts +++ b/src/app/core/core.module.ts @@ -12,8 +12,9 @@ import { TranslateLoader, TranslateModule } from 'ng2-translate'; // import RxJs needed operators only once import { RuntimeEnvironmentService } from 'app/core/runtime-environment.service'; +import { environment } from 'environments/environment'; import { createTranslateLoader } from '../shared/helpers/aot.helper'; -import { getRootReducer } from './../shared/states/root.reducer'; +import { metaReducers, reducers } from './../shared/states/root.reducer'; import { LANGUAGES } from './injection-tokens'; import './rxjs-operators'; @@ -23,22 +24,27 @@ import './rxjs-operators'; */ @NgModule({ imports: [ + // -------------------------------------------------------------------- // START : Do not add your libs here BrowserAnimationsModule, HttpModule, - // TODO : Keep an eye on ngrx V3 to have lazy loaded reducers - // https://github.com/ngrx/store/pull/269 - StoreModule.provideStore(getRootReducer), - StoreDevtoolsModule.instrumentOnlyWithExtension(), TranslateModule.forRoot({ provide: TranslateLoader, useFactory: createTranslateLoader, deps: [Http], }), + StoreModule.forRoot(reducers, { metaReducers }), + // it'd be nice to have the possibility to activate redux devtools + // even if we're in prod but only with the extension + // since ngrx v4, no idea how to do that + !environment.production + ? StoreDevtoolsModule.instrument({ maxAge: 50 }) + : [], // END : Do not add your libs here + // -------------------------------------------------------------------- - // pass every effect here, one per line - // EffectsModule.runAfterBootstrap(YOUR_EFFECT_GOES_HERE), + // pass every effects here + // EffectsModule.forRoot([YOUR_EFFECTS_GOES_HERE]); ], providers: [ { diff --git a/src/app/shared/states/root.reducer.ts b/src/app/shared/states/root.reducer.ts index c681c67..8dc7832 100644 --- a/src/app/shared/states/root.reducer.ts +++ b/src/app/shared/states/root.reducer.ts @@ -1,5 +1,4 @@ -import { compose } from '@ngrx/core/compose'; -import { combineReducers } from '@ngrx/store'; +import { ActionReducerMap } from '@ngrx/store'; import { storeFreeze } from 'ngrx-store-freeze'; import { enableBatching } from 'redux-batched-actions'; @@ -9,7 +8,7 @@ import { uiReducer } from './ui/ui.reducer'; // ------------------------------------------------------------------------------ -const reducers = { +export const reducers: ActionReducerMap = { // pass your reducers here ui: uiReducer, }; @@ -18,21 +17,14 @@ const reducers = { // if environment is != from production // use storeFreeze to avoid state mutation -const developmentReducer = compose( - storeFreeze, - enableBatching, - combineReducers -)(reducers); -const productionReducer = compose(enableBatching, combineReducers)(reducers); +const metaReducersDev = [storeFreeze, enableBatching]; // enableBatching allows us to dispatch multiple actions // without letting the subscribers being warned between the actions // only at the end : https://github.com/tshelburne/redux-batched-actions // can be very handy when normalizing HTTP response -export function getRootReducer(state: IStore, action: any) { - if (environment.production) { - return productionReducer(state, action); - } else { - return developmentReducer(state, action); - } -} +const metaReducersProd = [enableBatching]; + +export const metaReducers = environment.production + ? metaReducersProd + : metaReducersDev; diff --git a/yarn.lock b/yarn.lock index 99c9a65..e49cf21 100644 --- a/yarn.lock +++ b/yarn.lock @@ -168,17 +168,17 @@ version "1.2.0" resolved "https://registry.yarnpkg.com/@ngrx/core/-/core-1.2.0.tgz#882b46abafa2e0e6d887cb71a1b2c2fa3e6d0dc6" -"@ngrx/effects@2.0.4": - version "2.0.4" - resolved "https://registry.yarnpkg.com/@ngrx/effects/-/effects-2.0.4.tgz#418eee5e1032fa66de5bbf1855653bb1951f12a4" +"@ngrx/effects@4.0.5": + version "4.0.5" + resolved "https://registry.yarnpkg.com/@ngrx/effects/-/effects-4.0.5.tgz#1224763800621b7305f9b18bc17ee09b25c861d1" -"@ngrx/store-devtools@3.2.4": - version "3.2.4" - resolved "https://registry.yarnpkg.com/@ngrx/store-devtools/-/store-devtools-3.2.4.tgz#2ce4d13bf34848a9e51ec87e3b125ed67b51e550" +"@ngrx/store-devtools@4.0.0": + version "4.0.0" + resolved "https://registry.yarnpkg.com/@ngrx/store-devtools/-/store-devtools-4.0.0.tgz#b79c24773217df7fd9735ad21f9cbf2533c96e04" -"@ngrx/store@2.2.3": - version "2.2.3" - resolved "https://registry.yarnpkg.com/@ngrx/store/-/store-2.2.3.tgz#e7bd1149f1c44208f1cc4744353f0f98a0f1f57b" +"@ngrx/store@4.0.3": + version "4.0.3" + resolved "https://registry.yarnpkg.com/@ngrx/store/-/store-4.0.3.tgz#36abacdfa19bfb8506e40de80bae06050a1e15e9" "@ngtools/json-schema@1.1.0": version "1.1.0"