Skip to content

Commit

Permalink
Merge pull request #16 from krzysztof-cislo/feature/allow-cancell-in-…
Browse files Browse the repository at this point in the history
…pre-navigate-event

- Removed navigation param NAVIGATION_CANCELLED_EVENT as it was not used
  • Loading branch information
majo44 authored Jun 22, 2020
2 parents f6b106d + 87d07a4 commit 9a0a887
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 10 deletions.
22 changes: 20 additions & 2 deletions index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
routingModule,
navigate,
cancelNavigation,
StateWithRouting, RoutingEvents
StateWithRouting, RoutingEvents, PRE_NAVIGATE_EVENT
} from './index';
import * as sinon from 'sinon';
import { expect, use } from 'chai';
Expand Down Expand Up @@ -141,6 +141,24 @@ describe(`simple scenarions`, () => {

});

it('Router should allow to cancel navigation in PRE_NAVIGATE_EVENT', async () => {
// eslint-disable-next-line @typescript-eslint/no-empty-function
onNavigate(store, '/a', () => {});
onNavigate(store, '/b', () => {});

store.on(PRE_NAVIGATE_EVENT,(_, {navigation}) => {
if(navigation.url === '/b') {
cancelNavigation(store);
}
})

await navigate(store, '/a');
await navigate(store, '/b');

expect(store.get().routing.current.url).eq('/a');
expect(store.get().routing.current.route).eq('/a');
});

it('Router should allows to cancel async navigation', async () => {
let continueA: () => void;
// eslint-disable-next-line @typescript-eslint/no-empty-function
Expand Down Expand Up @@ -193,6 +211,6 @@ describe(`simple scenarions`, () => {
onNavigate(store, '/a/(?<page>.*)', spy);
await navigate(store, '/a/test');
expect(store.get().routing.current.params).eql({page: 'test', 0: 'test'});
})
});

});
16 changes: 8 additions & 8 deletions index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export interface RoutingEvents {
[NAVIGATE_EVENT]: NavigationEvent;
[NAVIGATION_ENDED_EVENT]: {navigation: NavigationState};
[NAVIGATION_FAILED_EVENT]: {navigation: Navigation; error: any };
[NAVIGATION_CANCELLED_EVENT]: NavigationEvent;
[NAVIGATION_CANCELLED_EVENT]: undefined;
[NAVIGATION_IGNORED_EVENT]: NavigationEvent;
[POST_NAVIGATE_EVENT]: {navigation: Navigation; error?: any };
[CANCEL_EVENT]: undefined;
Expand All @@ -157,7 +157,7 @@ const ignoreNavigation = (navigation: Navigation, {current, next}: StateWithRout
* const store = createStore([asyncRoutingModule, your_module1 ...]);
*/
export const routingModule = (store: StoreonStore<StateWithRouting, RoutingEvents>) => {

const dispatch = store.dispatch.bind(store);
const on = store.on.bind(store);

Expand Down Expand Up @@ -199,7 +199,7 @@ export const routingModule = (store: StoreonStore<StateWithRouting, RoutingEvent
// we have to cancel them
on(NAVIGATE_EVENT, ({ routing }) => {
if (routing.next) {
dispatch(NAVIGATION_CANCELLED_EVENT, {navigation: routing.next})
dispatch(NAVIGATION_CANCELLED_EVENT)
}
});

Expand Down Expand Up @@ -271,10 +271,10 @@ export const routingModule = (store: StoreonStore<StateWithRouting, RoutingEvent
);

// state updates
on(NAVIGATION_CANCELLED_EVENT, ({ routing }) => ({routing : { ...routing, next: undefined }}));
on(NAVIGATION_FAILED_EVENT, ({ routing }) => ({routing : { ...routing, next: undefined }}));
on(NAVIGATION_CANCELLED_EVENT, ({ routing }) => ({routing : { ...routing, candidate: undefined, next: undefined }}));
on(NAVIGATION_FAILED_EVENT, ({ routing }) => ({routing : { ...routing, candidate: undefined, next: undefined }}));
on(NAVIGATION_ENDED_EVENT, ({ routing }, {navigation}) =>
({routing : { ...routing, next: undefined, current: navigation }}));
({routing : { ...routing, candidate: undefined, next: undefined, current: navigation }}));

// binding events to close promise
on(NAVIGATION_IGNORED_EVENT, (s, e) => dispatch(POST_NAVIGATE_EVENT, e));
Expand All @@ -289,8 +289,8 @@ export const routingModule = (store: StoreonStore<StateWithRouting, RoutingEvent
// public
on(CANCEL_EVENT, ({routing}) => {
/* istanbul ignore else */
if (routing.next) {
dispatch(NAVIGATION_CANCELLED_EVENT, {navigation: routing.next })
if (routing.next || routing.candidate) {
dispatch(NAVIGATION_CANCELLED_EVENT)
}
});
};
Expand Down

0 comments on commit 9a0a887

Please sign in to comment.