@@ -16,6 +16,7 @@ var supportsHistory = require('./supportsHistory');
16
16
var Transition = require ( './Transition' ) ;
17
17
var PropTypes = require ( './PropTypes' ) ;
18
18
var Redirect = require ( './Redirect' ) ;
19
+ var Cancellation = require ( './Cancellation' ) ;
19
20
var Path = require ( './Path' ) ;
20
21
21
22
/**
@@ -43,7 +44,9 @@ function defaultAbortHandler(abortReason, location) {
43
44
if ( typeof location === 'string' )
44
45
throw new Error ( 'Unhandled aborted transition! Reason: ' + abortReason ) ;
45
46
46
- if ( abortReason instanceof Redirect ) {
47
+ if ( abortReason instanceof Cancellation ) {
48
+ return ;
49
+ } else if ( abortReason instanceof Redirect ) {
47
50
location . replace ( this . makePath ( abortReason . to , abortReason . params , abortReason . query ) ) ;
48
51
} else {
49
52
location . pop ( ) ;
@@ -141,6 +144,7 @@ function createRouter(options) {
141
144
var onAbort = options . onAbort || defaultAbortHandler ;
142
145
var state = { } ;
143
146
var nextState = { } ;
147
+ var pendingTransition = null ;
144
148
145
149
function updateState ( ) {
146
150
state = nextState ;
@@ -212,7 +216,14 @@ function createRouter(options) {
212
216
'You cannot use transitionTo with a static location'
213
217
) ;
214
218
215
- location . push ( this . makePath ( to , params , query ) ) ;
219
+ var path = this . makePath ( to , params , query ) ;
220
+
221
+ if ( pendingTransition ) {
222
+ // Replace so pending location does not stay in history.
223
+ location . replace ( path ) ;
224
+ } else {
225
+ location . push ( path ) ;
226
+ }
216
227
} ,
217
228
218
229
/**
@@ -265,6 +276,11 @@ function createRouter(options) {
265
276
* hooks wait, the transition is fully synchronous.
266
277
*/
267
278
dispatch : function ( path , action , callback ) {
279
+ if ( pendingTransition ) {
280
+ pendingTransition . abort ( new Cancellation ( ) ) ;
281
+ pendingTransition = null ;
282
+ }
283
+
268
284
var prevPath = state . path ;
269
285
if ( prevPath === path )
270
286
return ; // Nothing to do!
@@ -306,6 +322,7 @@ function createRouter(options) {
306
322
}
307
323
308
324
var transition = new Transition ( path , this . replaceWith . bind ( this , path ) ) ;
325
+ pendingTransition = transition ;
309
326
310
327
transition . from ( fromRoutes , components , function ( error ) {
311
328
if ( error || transition . isAborted )
@@ -335,6 +352,8 @@ function createRouter(options) {
335
352
*/
336
353
run : function ( callback ) {
337
354
function dispatchHandler ( error , transition ) {
355
+ pendingTransition = null ;
356
+
338
357
if ( error ) {
339
358
onError . call ( router , error ) ;
340
359
} else if ( transition . isAborted ) {
0 commit comments