Skip to content

Commit 005f554

Browse files
committed
Some cleanup
1 parent 2ae49e7 commit 005f554

File tree

1 file changed

+38
-55
lines changed

1 file changed

+38
-55
lines changed

modules/createRouter.js

Lines changed: 38 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,6 @@ var DEFAULT_LOCATION = canUseDOM ? HashLocation : '/';
3131
*/
3232
var DEFAULT_SCROLL_BEHAVIOR = canUseDOM ? ImitateBrowserBehavior : null;
3333

34-
/**
35-
* The default error handler for new routers.
36-
*/
37-
function defaultErrorHandler(error) {
38-
// Throw so we don't silently swallow async errors.
39-
throw error; // This error probably originated in a transition hook.
40-
}
41-
42-
/**
43-
* The default aborted transition handler for new routers.
44-
*/
45-
function defaultAbortHandler(abortReason, location) {
46-
if (typeof location === 'string')
47-
throw new Error('Unhandled aborted transition! Reason: ' + abortReason);
48-
49-
if (abortReason instanceof Cancellation) {
50-
return;
51-
} else if (abortReason instanceof Redirect) {
52-
location.replace(this.makePath(abortReason.to, abortReason.params, abortReason.query));
53-
} else {
54-
location.pop();
55-
}
56-
}
57-
5834
function createMatch(route, params, pathname, query) {
5935
return {
6036
routes: [ route ],
@@ -152,13 +128,10 @@ function createRouter(options) {
152128
var mountedComponents = [];
153129
var location = options.location || DEFAULT_LOCATION;
154130
var scrollBehavior = options.scrollBehavior || DEFAULT_SCROLL_BEHAVIOR;
155-
var onError = options.onError || defaultErrorHandler;
156-
var onAbort = options.onAbort || defaultAbortHandler;
157131
var state = {};
158132
var nextState = {};
159133
var pendingTransition = null;
160134
var dispatchHandler = null;
161-
var changeListener = null;
162135

163136
if (typeof location === 'string') {
164137
warning(
@@ -322,6 +295,28 @@ function createRouter(options) {
322295
return false;
323296
},
324297

298+
handleAbort: options.onAbort || function (abortReason) {
299+
if (typeof location === 'string')
300+
throw new Error('Unhandled aborted transition! Reason: ' + abortReason);
301+
302+
if (abortReason instanceof Cancellation) {
303+
return;
304+
} else if (abortReason instanceof Redirect) {
305+
location.replace(this.makePath(abortReason.to, abortReason.params, abortReason.query));
306+
} else {
307+
location.pop();
308+
}
309+
},
310+
311+
handleError: options.onError || function (error) {
312+
// Throw so we don't silently swallow async errors.
313+
throw error; // This error probably originated in a transition hook.
314+
},
315+
316+
handleLocationChange: function (change) {
317+
this.dispatch(change.path, change.type);
318+
},
319+
325320
/**
326321
* Performs a transition to the given path and calls callback(error, abortReason)
327322
* when the transition is finished. If both arguments are null the router's state
@@ -392,22 +387,17 @@ function createRouter(options) {
392387

393388
transition.from(fromRoutes, fromComponents, function (error) {
394389
if (error || transition.abortReason)
395-
return dispatchHandler.call(Router, error, transition);
390+
return dispatchHandler.call(Router, error, transition); // No need to continue.
396391

397392
transition.to(toRoutes, nextParams, nextQuery, function (error) {
398-
if (error || transition.abortReason)
399-
return dispatchHandler.call(Router, error, transition);
400-
401-
nextState = {
393+
dispatchHandler.call(Router, error, transition, {
402394
path: path,
403395
action: action,
404396
pathname: match.pathname,
405397
routes: nextRoutes,
406398
params: nextParams,
407399
query: nextQuery
408-
};
409-
410-
dispatchHandler.call(Router, null, transition);
400+
});
411401
});
412402
});
413403
},
@@ -425,53 +415,46 @@ function createRouter(options) {
425415
'Router is already running'
426416
);
427417

428-
dispatchHandler = function (error, transition) {
418+
dispatchHandler = function (error, transition, newState) {
429419
if (error)
430-
onError.call(Router, error);
420+
Router.handleError(error);
431421

432422
if (pendingTransition !== transition)
433423
return;
434424

435425
pendingTransition = null;
436426

437427
if (transition.abortReason) {
438-
onAbort.call(Router, transition.abortReason, location);
428+
Router.handleAbort(transition.abortReason);
439429
} else {
440-
callback.call(Router, Router, nextState);
430+
callback.call(this, this, nextState = newState);
441431
}
442432
};
443433

444434
if (typeof location === 'string') {
445435
Router.dispatch(location, null);
446436
} else {
447-
// Listen for changes to the location.
448-
changeListener = function (change) {
449-
Router.dispatch(change.path, change.type);
450-
};
451-
452437
if (location.addChangeListener)
453-
location.addChangeListener(changeListener);
438+
location.addChangeListener(Router.handleLocationChange);
439+
440+
this.isRunning = true;
454441

455442
// Bootstrap using the current path.
456443
this.refresh();
457-
458-
this.isRunning = true;
459444
}
460445
},
461446

447+
refresh: function () {
448+
Router.dispatch(location.getCurrentPath(), null);
449+
},
450+
462451
stop: function () {
463452
this.cancelPendingTransition();
464453

465-
if (location.removeChangeListener && changeListener) {
466-
location.removeChangeListener(changeListener);
467-
changeListener = null;
468-
}
454+
if (location.removeChangeListener)
455+
location.removeChangeListener(Router.handleLocationChange);
469456

470457
this.isRunning = false;
471-
},
472-
473-
refresh: function () {
474-
Router.dispatch(location.getCurrentPath(), null);
475458
}
476459

477460
},

0 commit comments

Comments
 (0)