Skip to content

Commit

Permalink
Adds hooks required for use with Immutable.js or other wrappers
Browse files Browse the repository at this point in the history
  • Loading branch information
dagstuan committed Jun 6, 2016
1 parent 11f7f21 commit 13e3744
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 11 deletions.
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,12 @@ Inspired by `@ngrx/router-store`
}
}
```

#### What if I use Immutable.js with my Redux store?

When using a wrapper for your store's state, such as Immutable.js, you will need to change two things from the standard setup:

1. Provide your own reducer function that will receive actions of type `UPDATE_LOCATION` and return the payload merged into state.
2. Pass a selector to access the payload state and convert it to a JS object via the `selectLocationFromState` option on `NgReduxRouter`'s `initialize()`.

These two hooks will allow you to store the state that this library uses in whatever format or wrapper you would like.
4 changes: 4 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,7 @@ export {
routerReducer,
RouterAction
} from './reducer';

export {
UPDATE_LOCATION
} from './actions';
4 changes: 0 additions & 4 deletions src/reducer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,3 @@ export function routerReducer(state: string = DefaultRouterState, action: Router
return state;
}
}

export function getLocationFromState(state) {
return state.router;
}
14 changes: 7 additions & 7 deletions src/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ import { NgRedux } from 'ng2-redux';
import { UPDATE_LOCATION } from './actions';
import {
RouterAction,
DefaultRouterState,
getLocationFromState
DefaultRouterState
} from './reducer';

@Injectable()
export class NgReduxRouter {
private isTimeTravelling: boolean;
private currentLocation: string;
private defaultSelectLocationState = (state) => state.router;

constructor(
private location: Location,
Expand All @@ -23,9 +23,9 @@ export class NgReduxRouter {
private applicationRef: ApplicationRef
) {}

initialize() {
initialize(selectLocationFromState = this.defaultSelectLocationState) {
this.listenToRouterChanges();
this.listenToReduxChanges();
this.listenToReduxChanges(selectLocationFromState);
}

listenToRouterChanges() {
Expand All @@ -49,7 +49,7 @@ export class NgReduxRouter {
.subscribe(handleLocationChange);
}

listenToReduxChanges() {
listenToReduxChanges(selectLocationFromState) {
const handleLocationChange = (location: string) => {
if (this.currentLocation === location) {
return;
Expand All @@ -60,14 +60,14 @@ export class NgReduxRouter {
this.router
.navigateByUrl(location)
.then(() => {
// Apparently navigating by url doesnt trigger angulars change detection,
// Apparently navigating by url doesn't trigger Angular's change detection,
// Need to do this manually then via ApplicationRef.
this.applicationRef.tick();
});
}

this.ngRedux
.select(state => getLocationFromState(state))
.select(state => selectLocationFromState(state))
.distinctUntilChanged()
.subscribe(handleLocationChange);
}
Expand Down

0 comments on commit 13e3744

Please sign in to comment.