Skip to content

Commit

Permalink
chore: upgrade to ngrx v4
Browse files Browse the repository at this point in the history
This closes #5
  • Loading branch information
maxime1992 committed Sep 9, 2017
1 parent 6a331fd commit f9da8d2
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 35 deletions.
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 13 additions & 7 deletions src/app/core/core.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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: [
{
Expand Down
24 changes: 8 additions & 16 deletions src/app/shared/states/root.reducer.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -9,7 +8,7 @@ import { uiReducer } from './ui/ui.reducer';

// ------------------------------------------------------------------------------

const reducers = {
export const reducers: ActionReducerMap<IStore> = {
// pass your reducers here
ui: uiReducer,
};
Expand All @@ -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;
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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/[email protected]":
version "1.1.0"
Expand Down

0 comments on commit f9da8d2

Please sign in to comment.