Skip to content

Commit

Permalink
Merge 0f1e97c into 7b7e8f0
Browse files Browse the repository at this point in the history
  • Loading branch information
rogerpadilla committed Sep 15, 2018
2 parents 7b7e8f0 + 0f1e97c commit 015b34c
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
15 changes: 8 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -245,17 +245,18 @@ router

const doInfiniteScroll = () => {
// do infinite scroll ...
};

const onNavigation = (navigationEvt) => {
// if navigating, then remove the listener for the window.scroll.
router.off('navigation', onNavigation);
window.removeEventListener('scroll', doInfiniteScroll);
};

window.addEventListener('scroll', doInfiniteScroll);

// subscribe to the navigation event (useful to know/react when going to a different route)
router.on('navigation', (navigationEvt) => {
// if changing the path, then remove the listener for the window.scroll.
if (navigationEvt.newUrl !== '/') {
window.removeEventListener('scroll', doInfiniteScroll);
}
});
// subscribe to the navigation event
router.on('navigation', onNavigation);

// and pass control to the next handler
next();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "prouter",
"description": "Fast, unopinionated, minimalist client side router library inspired in the simplicity and flexibility of express router",
"version": "8.1.0",
"version": "8.1.2",
"main": "prouter.min.js",
"homepage": "https://github.com/rogerpadilla/prouter",
"bugs": {
Expand Down
29 changes: 28 additions & 1 deletion src/browser-router.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ describe('browserRouter', () => {
expect(req.queryString).toBe('');
expect(req.query).toEqual({});
expect(router.getPath()).toBe('/');
resp.end({ preventnavigation: true });
resp.end({ preventNavigation: true });
})
.use('*', () => {
fail('Should not call this');
Expand Down Expand Up @@ -440,4 +440,31 @@ describe('browserRouter', () => {
router.push('/hello');
});

it('should unsubscribe from navigation event', (done) => {

expect(router.getPath()).toBe('/');

router
.use('/hello', (req, resp, next) => {
expect(req.originalUrl).toBe('/hello');
expect(req.path).toBe('/hello');
expect(req.queryString).toBe('');
expect(req.query).toEqual({});
expect(router.getPath()).toBe('/');
next();
done();
})
.listen();

const onNavigation = () => {
fail('Should not enter here since unsubscribed');
};

router.on('navigation', onNavigation);

router.off('navigation', onNavigation);

router.push('/hello');
});

});
12 changes: 9 additions & 3 deletions src/browser-router.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { baseRouter } from './router';
import { ProuterSubscriptors, ProuterSubscriptionType, ProuterSubscriptorCallback, ProuterNavigationEvent } from './entity';
import { ProuterSubscriptors, ProuterSubscriptionType, ProuterSubscriptorCallback, ProuterNavigationEvent, ProuterBrowserRouter } from './entity';


export function browserRouter() {
Expand All @@ -14,7 +14,7 @@ export function browserRouter() {
br.processCurrentPath();
};

const br = {
const br: ProuterBrowserRouter = {

...baseRouterObj,

Expand All @@ -39,7 +39,7 @@ export function browserRouter() {
push(newPath: string) {
baseRouterObj.processPath(newPath, opts => {

if (!opts || !opts.preventnavigation) {
if (!opts || !opts.preventNavigation) {

const oldPath = br.getPath();

Expand All @@ -64,6 +64,12 @@ export function browserRouter() {

on(type: ProuterSubscriptionType, callback: ProuterSubscriptorCallback) {
subscriptors[type].push(callback);
},

off(type: ProuterSubscriptionType, callback: ProuterSubscriptorCallback) {
subscriptors[type] = subscriptors[type].filter(cb => {
return cb !== callback;
});
}
};

Expand Down
4 changes: 3 additions & 1 deletion src/entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export interface ProuterRequestProcessor {
}

export interface ProuterOptsProcessPathCallback {
preventnavigation?: boolean;
preventNavigation?: boolean;
}

export interface ProuterProcessPathCallback {
Expand Down Expand Up @@ -89,6 +89,8 @@ export interface ProuterBrowserRouter extends ProuterRouter {
stop(): void;

on(type: ProuterSubscriptionType, callback: ProuterSubscriptorCallback): void;

off(type: ProuterSubscriptionType, callback: ProuterSubscriptorCallback): void;
}

export interface ProuterNavigationEvent {
Expand Down

0 comments on commit 015b34c

Please sign in to comment.