Skip to content

Commit

Permalink
feat: Added isTransitioning property to RouterStore
Browse files Browse the repository at this point in the history
`isTransitioning` is set to true when the router is in the process of transitioning from one state
to another.

Implements #32
  • Loading branch information
nareshbhatia committed Jun 10, 2018
1 parent 63b95e1 commit 392a50d
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,9 @@ The `RouterStore` is the keeper of the `RouterState`. It allows transitioning be
```jsx
export class RouterStore {
@observable.ref routerState: RouterState;
@observable isTransitioning: boolean = false;

constructor(rootStore: any, routes: Route[], notFoundState: RouterState, initialRoute: Route = INITIAL_ROUTE);
goTo(toState: RouterState): Promise<RouterState>;
goTo(routeName: string, params?: StringMap, queryParams?: Object): Promise<RouterState>;
Expand All @@ -463,6 +466,10 @@ export class RouterStore {
}
```
The `RouterStore` exposes two observable properties:
- routerState: the state of the router
- isTransitioning: set to true when the router is in the process of transitioning from one state to another. This property can be used, for example, to display a progress indicator during transitions.
### HistoryAdapter
The `HistoryAdapter` is responsible for keeping the browser address bar and the `RouterState` in sync. It also provides a `goBack()` method to go back in history.
Expand Down
3 changes: 3 additions & 0 deletions src/router-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ export class RouterStore {
routes: Route[];
notFoundState: RouterState;
@observable.ref routerState: RouterState;
@observable isTransitioning: boolean = false;

constructor(
rootStore: any,
Expand Down Expand Up @@ -172,6 +173,7 @@ export class RouterStore {
const { beforeEnter, onEnter } = this.getRoute(toState.routeName);

// Call the transition hook chain
this.isTransitioning = true;
return (
[beforeExit, beforeEnter, onExit, onEnter]
.reduce(
Expand Down Expand Up @@ -202,5 +204,6 @@ export class RouterStore {
@action
private setRouterState(routerState: RouterState) {
this.routerState = routerState;
this.isTransitioning = false;
}
}
3 changes: 2 additions & 1 deletion test/router-store.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,12 @@ const errorState = new RouterState('errorRoute');

describe('RouterStore', () => {
test('transitions to the desired state', () => {
expect.assertions(1);
expect.assertions(2);

const routerStore = new RouterStore({}, routes, notFound);
return routerStore.goTo(home).then(toState => {
expect(toState.isEqual(home)).toBeTruthy();
expect(routerStore.isTransitioning).toBeFalsy();
});
});

Expand Down

0 comments on commit 392a50d

Please sign in to comment.