├── .gitignore ├── LICENSE ├── README.md ├── package.json ├── src ├── index.ts ├── reducer.ts └── router-connector.ts ├── tsconfig.json ├── tslint.json └── typings.json /.gitignore: -------------------------------------------------------------------------------- 1 | logs/* 2 | !.gitkeep 3 | node_modules/ 4 | tmp 5 | .DS_Store 6 | .idea 7 | .build 8 | !dist/index.html 9 | dist/ 10 | typings 11 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 Brandon 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ngrx-store-router 2 | 3 | This project has been transitioned and is being maintained at https://github.com/ngrx/router-store 4 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "ngrx-store-router", 3 | "version": "0.4.0", 4 | "description": "Sync between the current router URL and @ngrx/store", 5 | "main": "index.js", 6 | "scripts": { 7 | "prebuild": "rm -rf ./dist", 8 | "build": "tsc", 9 | "postbuild": "cp ./{readme.md,package.json} ./dist", 10 | "typings": "typings", 11 | "typings:install": "npm run typings -- install" 12 | }, 13 | "repository": { 14 | "type": "git", 15 | "url": "git+https://github.com/CodeSequence/ngrx-store-router.git" 16 | }, 17 | "keywords": [ 18 | "angular2", 19 | "ngrx", 20 | "store", 21 | "router" 22 | ], 23 | "author": "Brandon Roberts", 24 | "license": "ISC", 25 | "bugs": { 26 | "url": "https://github.com/CodeSequence/ngrx-store-router/issues" 27 | }, 28 | "homepage": "https://github.com/CodeSequence/ngrx-store-router#readme", 29 | "peerDependencies": { 30 | "@angular/core": "^2.0.0-rc", 31 | "@angular/common": "^2.0.0-rc", 32 | "@angular/router": "^3.0.0-beta", 33 | "@ngrx/core": "^1.0.1", 34 | "@ngrx/store": "^2.0.0", 35 | "rxjs": "5.0.0-beta.6" 36 | }, 37 | "devDependencies": { 38 | "@angular/common": "^2.0.0-rc", 39 | "@angular/compiler": "^2.0.0-rc", 40 | "@angular/core": "^2.0.0-rc", 41 | "@angular/platform-browser": "^2.0.0-rc", 42 | "@angular/platform-browser-dynamic": "^2.0.0-rc", 43 | "@angular/router": "^3.0.0-beta", 44 | "@ngrx/core": "^1.0.1", 45 | "@ngrx/store": "^2.0.0", 46 | "es6-shim": "^0.35.0", 47 | "rxjs": "5.0.0-beta.6", 48 | "typings": "^0.8.1", 49 | "zone.js": "^0.6.12" 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /src/index.ts: -------------------------------------------------------------------------------- 1 | import {RouterConnector} from './router-connector'; 2 | 3 | export const provideRouterConnector = () => { 4 | return [ 5 | RouterConnector 6 | ]; 7 | }; 8 | 9 | export {RouterConnector}; 10 | export {routerReducer, RouterState, RouterActions} from './reducer'; 11 | -------------------------------------------------------------------------------- /src/reducer.ts: -------------------------------------------------------------------------------- 1 | import { Action } from '@ngrx/store'; 2 | 3 | const initialState = { 4 | init: false, 5 | url: '', 6 | navigating: false 7 | }; 8 | 9 | export interface RouterState { 10 | init?: boolean; 11 | url: string; 12 | navigating?: boolean; 13 | } 14 | 15 | export const RouterActions = { 16 | init: '[ROUTER] INITIALIZED', 17 | navigated: '[ROUTER] NAVIGATED', 18 | navigating: '[ROUTER] NAVIGATING' 19 | }; 20 | 21 | export const routerReducer = (state: RouterState = initialState, action: Action): RouterState => { 22 | switch (action.type) { 23 | case RouterActions.init: 24 | return Object.assign({}, state, { url: action.payload.url, init: true }); 25 | case RouterActions.navigating: 26 | return Object.assign({}, state, { url: action.payload.url, navigating: true }); 27 | case RouterActions.navigated: 28 | return Object.assign({}, state, { url: action.payload.url, navigating: false }); 29 | default: 30 | return state; 31 | } 32 | }; 33 | -------------------------------------------------------------------------------- /src/router-connector.ts: -------------------------------------------------------------------------------- 1 | import 'rxjs/add/operator/distinctUntilChanged'; 2 | import 'rxjs/add/operator/do'; 3 | import 'rxjs/add/operator/filter'; 4 | import 'rxjs/add/operator/map'; 5 | import 'rxjs/add/operator/withLatestFrom'; 6 | import { Injectable } from '@angular/core'; 7 | import { Store } from '@ngrx/store'; 8 | import { Router, Event, NavigationEnd } from '@angular/router'; 9 | import { Observable } from 'rxjs/Observable'; 10 | import { Subscription } from 'rxjs/Subscription'; 11 | import { RouterActions } from './reducer'; 12 | 13 | @Injectable() 14 | export class RouterConnector { 15 | routerSubscription: Subscription; 16 | storeSubscription: Subscription; 17 | 18 | latestUrl: Observable = this.router 19 | .events 20 | .filter((event: Event) => event instanceof NavigationEnd) 21 | .map(() => this.router.url) 22 | .distinctUntilChanged(); 23 | 24 | routerSelect = this.store.select(state => state.router); 25 | 26 | constructor( 27 | private store: Store, 28 | private router: Router 29 | ) {} 30 | 31 | connect() { 32 | this.initRouterSubscriber(); 33 | this.initStoreSubscriber(); 34 | } 35 | 36 | disconnect() { 37 | if (this.routerSubscription) { 38 | this.routerSubscription.unsubscribe(); 39 | } 40 | 41 | if (this.storeSubscription) { 42 | this.storeSubscription.unsubscribe(); 43 | } 44 | } 45 | 46 | initRouterSubscriber() { 47 | this.routerSubscription = this.latestUrl 48 | .withLatestFrom(this.routerSelect) 49 | .filter(([url, rs]) => this.initialized(rs, url)) 50 | .filter(([url, rs]) => this.urlChanged(rs, url)) 51 | .map(([url]) => { 52 | return { 53 | type: RouterActions.navigated, 54 | payload: {url} 55 | }; 56 | }) 57 | .subscribe(this.store); 58 | } 59 | 60 | initStoreSubscriber() { 61 | this.storeSubscription = this.routerSelect 62 | .withLatestFrom(this.latestUrl) 63 | .filter(([rs, url]) => this.isInit(rs) && this.urlChanged(rs, url)) 64 | .map(([rs]) => rs.url) 65 | .do(url => this.router.navigateByUrl(url)) 66 | .subscribe(); 67 | } 68 | 69 | isInit(rs: any) { 70 | return rs.init; 71 | } 72 | 73 | initialized(rs: any, url: string) { 74 | let init = this.isInit(rs); 75 | 76 | if (!init) { 77 | this.store.dispatch({ 78 | type: RouterActions.init, 79 | payload: {url} 80 | }); 81 | } 82 | 83 | return init; 84 | } 85 | 86 | urlChanged(rs: any, url: string) { 87 | return rs.navigating || rs.url !== url; 88 | } 89 | } 90 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "emitDecoratorMetadata": true, 5 | "experimentalDecorators": true, 6 | "module": "commonjs", 7 | "outDir": "dist", 8 | "declaration": true 9 | }, 10 | "exclude": [ 11 | "node_modules", 12 | "typings/browser.d.ts", 13 | "typings/browser" 14 | ], 15 | "compileOnSave": false 16 | } 17 | -------------------------------------------------------------------------------- /tslint.json: -------------------------------------------------------------------------------- 1 | { 2 | "rules": { 3 | "class-name": true, 4 | "comment-format": [ 5 | true, 6 | "check-space" 7 | ], 8 | "curly": true, 9 | "eofline": true, 10 | "forin": true, 11 | "indent": [ 12 | true, 13 | "spaces" 14 | ], 15 | "label-position": true, 16 | "label-undefined": true, 17 | "max-line-length": [ 18 | true, 19 | 140 20 | ], 21 | "member-access": false, 22 | "member-ordering": [ 23 | true, 24 | "static-before-instance", 25 | "variables-before-functions" 26 | ], 27 | "no-arg": true, 28 | "no-bitwise": true, 29 | "no-console": [ 30 | true, 31 | "debug", 32 | "info", 33 | "time", 34 | "timeEnd", 35 | "trace" 36 | ], 37 | "no-construct": true, 38 | "no-debugger": true, 39 | "no-duplicate-key": true, 40 | "no-duplicate-variable": true, 41 | "no-empty": false, 42 | "no-eval": true, 43 | "no-inferrable-types": true, 44 | "no-shadowed-variable": true, 45 | "no-string-literal": false, 46 | "no-switch-case-fall-through": true, 47 | "no-trailing-whitespace": true, 48 | "no-unused-expression": true, 49 | "no-unused-variable": true, 50 | "no-unreachable": true, 51 | "no-use-before-declare": true, 52 | "no-var-keyword": true, 53 | "object-literal-sort-keys": false, 54 | "one-line": [ 55 | true, 56 | "check-open-brace", 57 | "check-catch", 58 | "check-else", 59 | "check-whitespace" 60 | ], 61 | "quotemark": [ 62 | true, 63 | "single" 64 | ], 65 | "radix": true, 66 | "semicolon": [ 67 | "always" 68 | ], 69 | "triple-equals": [ 70 | true, 71 | "allow-null-check" 72 | ], 73 | "typedef-whitespace": [ 74 | true, 75 | { 76 | "call-signature": "nospace", 77 | "index-signature": "nospace", 78 | "parameter": "nospace", 79 | "property-declaration": "nospace", 80 | "variable-declaration": "nospace" 81 | } 82 | ], 83 | "variable-name": false, 84 | "whitespace": [ 85 | true, 86 | "check-branch", 87 | "check-decl", 88 | "check-operator", 89 | "check-separator", 90 | "check-type" 91 | ] 92 | } 93 | } 94 | -------------------------------------------------------------------------------- /typings.json: -------------------------------------------------------------------------------- 1 | { 2 | "ambientDevDependencies": { 3 | "es6-shim": "github:DefinitelyTyped/DefinitelyTyped/es6-shim/es6-shim.d.ts#6697d6f7dadbf5773cb40ecda35a76027e0783b2", 4 | "jasmine": "github:DefinitelyTyped/DefinitelyTyped/jasmine/jasmine.d.ts#26c98c8a9530c44f8c801ccc3b2057e2101187ee", 5 | "ng2": "github:gdi2290/typings-ng2/ng2.d.ts#32998ff5584c0eab0cd9dc7704abb1c5c450701c", 6 | "require": "github:DefinitelyTyped/DefinitelyTyped/requirejs/require.d.ts#853544cb7068af64bdb10ef1ae0c54d3aa3a80f6", 7 | "zone.js": "github:DefinitelyTyped/DefinitelyTyped/zone.js/zone.js.d.ts#b923a5aaf013ac84c566f27ba6b5843211981c7a" 8 | } 9 | } 10 | --------------------------------------------------------------------------------