@@ -5,7 +5,7 @@ import type Router from '../index'
5
5
import { inBrowser } from '../util/dom'
6
6
import { runQueue } from '../util/async'
7
7
import { warn } from '../util/warn'
8
- import { START , isSameRoute } from '../util/route'
8
+ import { START , isSameRoute , handleRouteEntered } from '../util/route'
9
9
import {
10
10
flatten ,
11
11
flatMapComponents ,
@@ -218,11 +218,9 @@ export class History {
218
218
}
219
219
220
220
runQueue ( queue , iterator , ( ) => {
221
- const postEnterCbs = [ ]
222
- const isValid = ( ) => this . current === route
223
221
// wait until async components are resolved before
224
222
// extracting in-component enter guards
225
- const enterGuards = extractEnterGuards ( activated , postEnterCbs , isValid )
223
+ const enterGuards = extractEnterGuards ( activated )
226
224
const queue = enterGuards . concat ( this . router . resolveHooks )
227
225
runQueue ( queue , iterator , ( ) => {
228
226
if ( this . pending !== route ) {
@@ -232,9 +230,7 @@ export class History {
232
230
onComplete ( route )
233
231
if ( this . router . app ) {
234
232
this . router . app . $nextTick ( ( ) => {
235
- postEnterCbs . forEach ( cb => {
236
- cb ( )
237
- } )
233
+ handleRouteEntered ( route )
238
234
} )
239
235
}
240
236
} )
@@ -352,57 +348,31 @@ function bindGuard (guard: NavigationGuard, instance: ?_Vue): ?NavigationGuard {
352
348
}
353
349
354
350
function extractEnterGuards (
355
- activated : Array < RouteRecord > ,
356
- cbs : Array < Function > ,
357
- isValid : ( ) = > boolean
351
+ activated : Array < RouteRecord >
358
352
) : Array < ?Function > {
359
353
return extractGuards (
360
354
activated ,
361
355
'beforeRouteEnter' ,
362
356
( guard , _ , match , key ) = > {
363
- return bindEnterGuard ( guard , match , key , cbs , isValid )
357
+ return bindEnterGuard ( guard , match , key )
364
358
}
365
359
)
366
360
}
367
361
368
362
function bindEnterGuard (
369
363
guard : NavigationGuard ,
370
364
match : RouteRecord ,
371
- key : string ,
372
- cbs : Array < Function > ,
373
- isValid : ( ) = > boolean
365
+ key : string
374
366
) : NavigationGuard {
375
367
return function routeEnterGuard ( to , from , next ) {
376
368
return guard ( to , from , cb => {
377
369
if ( typeof cb === 'function' ) {
378
- cbs . push ( ( ) => {
379
- // #750
380
- // if a router-view is wrapped with an out-in transition,
381
- // the instance may not have been registered at this time.
382
- // we will need to poll for registration until current route
383
- // is no longer valid.
384
- poll ( cb , match . instances , key , isValid )
385
- } )
370
+ if ( ! match . enteredCbs [ key ] ) {
371
+ match . enteredCbs [ key ] = [ ]
372
+ }
373
+ match . enteredCbs [ key ] . push ( cb )
386
374
}
387
375
next ( cb )
388
376
} )
389
377
}
390
378
}
391
-
392
- function poll (
393
- cb : any , // somehow flow cannot infer this is a function
394
- instances : Object ,
395
- key : string ,
396
- isValid : ( ) = > boolean
397
- ) {
398
- if (
399
- instances [ key ] &&
400
- ! instances [ key ] . _isBeingDestroyed // do not reuse being destroyed instance
401
- ) {
402
- cb ( instances [ key ] )
403
- } else if ( isValid ( ) ) {
404
- setTimeout ( ( ) => {
405
- poll ( cb , instances , key , isValid )
406
- } , 16 )
407
- }
408
- }
0 commit comments