Skip to content

Commit

Permalink
Merge pull request #2 from vsavkin/add_router_store
Browse files Browse the repository at this point in the history
add router-store module
  • Loading branch information
MikeRyanDev authored Apr 9, 2017
2 parents a52943e + c9d8ff7 commit 67027c8
Show file tree
Hide file tree
Showing 15 changed files with 3,409 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ __build__/**

# Build Artifacts #
release
dist
/node_modules/
lerna-debug.log
/lib/
Expand Down
21 changes: 21 additions & 0 deletions modules/router-store/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
The MIT License (MIT)

Copyright (c) 2017 Brandon Roberts, Mike Ryan, Rob Wormald, Victor Savkin

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
48 changes: 48 additions & 0 deletions modules/router-store/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# StoreRouterConnectingModule


StoreRouterConnectingModule connects RouterModule with StoreModule.

During the navigation, before any guards or resolvers run, the router will dispatch a ROUTER_NAVIGATION action, which has the following signature:

```typescript
export type RouterNavigationPayload = {
routerState: RouterStateSnapshot,
event: RoutesRecognized
}
```
* Either a reducer or an effect can be invoked in response to this action. If the invoked reducer throws, the navigation will be canceled.
* If navigation gets canceled because of a guard, a ROUTER_CANCEL action will be dispatched.
* If navigation results in an error, a ROUTER_ERROR action will be dispatched.
* Both ROUTER_CANCEL and ROUTER_ERROR contain the store state before the navigation which can be used to restore the consistency of the store.
## Usage
```typescript

function routerReducer(state = "", action: any) {
if (action.type === "ROUTER_NAVIGATION") {
const s: RouterStateSnapshot = action.payload.routerState;
return s.url.toString();
} else {
return state;
}
}

@NgModule({
declarations: [AppCmp, SimpleCmp],
imports: [
BrowserModule,
StoreModule.provideStore({router: routerReducer}),
RouterModule.forRoot([
{ path: '', component: SimpleCmp },
{ path: 'next', component: SimpleCmp }
]),
StoreRouterConnectingModule
],
bootstrap: [AppCmp]
})
export class AppModule {
}
```
42 changes: 42 additions & 0 deletions modules/router-store/e2e/app.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { Component, NgModule } from "@angular/core";
import { BrowserModule } from "@angular/platform-browser";
import { platformBrowserDynamic } from "@angular/platform-browser-dynamic";
import { Router, RouterModule } from "@angular/router";
import { StoreModule } from '@ngrx/store';
import { ROUTER_NAVIGATION, ROUTER_CANCEL, ROUTER_ERROR, StoreRouterConnectingModule } from "../src/index";

@Component({
selector: 'test-app',
template: '<router-outlet></router-outlet>'
})
export class AppCmp { }

@Component({
selector: 'simple-cmp',
template: 'simple'
})
export class SimpleCmp { }

export function reducer (state: string = "", action: any) {
if (action.type === ROUTER_NAVIGATION) {
return action.payload.routerState.url.toString();
} else {
return state;
}
}

@NgModule({
declarations: [AppCmp, SimpleCmp],
imports: [
BrowserModule,
RouterModule.forRoot([
{ path: '', component: SimpleCmp },
{ path: 'next', component: SimpleCmp }
]),
StoreModule.provideStore({ reducer }),
StoreRouterConnectingModule
],
bootstrap: [AppCmp]
})
export class AppModule {
}
4 changes: 4 additions & 0 deletions modules/router-store/e2e/main.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import {platformBrowserDynamic} from '@angular/platform-browser-dynamic';
import {AppModule} from './app';

platformBrowserDynamic().bootstrapModule(AppModule);
29 changes: 29 additions & 0 deletions modules/router-store/karma.conf.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
var path = require('path');
module.exports = function(karma) {
'use strict';

karma.set({
basePath: __dirname,
frameworks: ['jasmine'],
files: [
'node_modules/core-js/client/core.js',
'node_modules/reflect-metadata/Reflect.js',

// Zone.js dependencies
'node_modules/zone.js/dist/zone.js',
'node_modules/zone.js/dist/long-stack-trace-zone.js',
'node_modules/zone.js/dist/proxy.js',
'node_modules/zone.js/dist/sync-test.js',
'node_modules/zone.js/dist/jasmine-patch.js',
'node_modules/zone.js/dist/async-test.js',
'node_modules/zone.js/dist/fake-async-test.js',

{ pattern: 'release/bundle.js', watched: false }
],

browsers: ['Chrome'],
colors: true,
logLevel: karma.LOG_INFO,
singleRun: true
});
};
68 changes: 68 additions & 0 deletions modules/router-store/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
{
"name": "@ngrx/router-store",
"version": "0.0.1",
"description": "Bindings to connect @angular/router to @ngrx/store",
"main": "./bundles/router-store.umd.js",
"module": "index.js",
"scripts": {
"test": "npm run clean:pre && webpack --config webpack.test.config.js && karma start && npm run clean:pre",
"e2e": "npm run clean:pre && webpack --config webpack.e2e.config.js",
"clean:pre": "rimraf release && rimraf dist",
"clean:post": "rimraf \"src/**/*.ngfactory.ts\"",
"copy": "cpy LICENSE package.json README.md release",
"build:js": "ngc -p tsconfig.json",
"build:umd": "rollup -c rollup.config.js",
"build:uglify": "uglifyjs -c --screw-ie8 --comments -o ./release/bundles/router-store.min.umd.js ./release/bundles/router-store.umd.js",
"prebuild": "npm run test && npm run clean:pre",
"postbuild": "npm run clean:post && npm run copy",
"build": "npm run build:js && npm run build:umd && npm run build:uglify"
},
"repository": {
"type": "git",
"url": "git+https://github.com/ngrx/platform.git"
},
"keywords": [
"RxJS",
"Angular",
"Redux"
],
"authors": [
"Victor Savkin <[email protected]>"
],
"license": "MIT",
"peerDependencies": {
"rxjs": "^5.0.0-beta.12",
"@angular/common": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/router": "^4.0.0",
"@ngrx/core": "^1.2.0",
"@ngrx/store": "^2.0.0"
},
"devDependencies": {
"@angular/common": "^4.0.0",
"@angular/compiler": "^4.0.0",
"@angular/compiler-cli": "^4.0.0",
"@angular/core": "^4.0.0",
"@angular/platform-browser": "^4.0.0",
"@angular/platform-browser-dynamic": "^4.0.0",
"@angular/router": "^4.0.0",
"@ngrx/core": "^1.2.0",
"@ngrx/store": "^2.0.0",
"@ngtools/webpack": "^1.2.9",
"@types/jasmine": "^2.2.33",
"@types/node": "^6.0.38",
"jasmine-core": "^2.5.2",
"karma": "^1.4.0",
"karma-chrome-launcher": "^2.0.0",
"karma-jasmine": "^1.1.0",
"rxjs": "^5.0.0-beta.12",
"ts-loader": "^2.0.0",
"typescript": "^2.2.0",
"uglifyjs": "^2.4.10",
"webpack": "^2.2.0",
"zone.js": "^0.7.6",
"rimraf": "^2.5.4",
"rollup": "^0.34.13",
"cpy-cli": "^1.0.1"
}
}
10 changes: 10 additions & 0 deletions modules/router-store/rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export default {
entry: './release/src/index.js',
dest: './release/bundles/router-store.umd.js',
format: 'umd',
moduleName: 'ngrx.routerStore',
globals: {
'rxjs/Observable': 'Rx',
'rxjs/observable/of': 'Rx.Observable'
}
}
Loading

0 comments on commit 67027c8

Please sign in to comment.