Skip to content

Commit 33f2b3e

Browse files
committed
support multiple global before/after hooks
1 parent 843acc4 commit 33f2b3e

File tree

4 files changed

+44
-19
lines changed

4 files changed

+44
-19
lines changed

src/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export default class Router {
5757
this._currentTransition = null
5858
this._previousTransition = null
5959
this._notFoundHandler = null
60-
this._beforeEachHook = null
61-
this._afterEachHook = null
60+
this._beforeEachHooks = []
61+
this._afterEachHooks = []
6262

6363
// feature detection
6464
this._hasPushState =

src/router/api.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ export default function (Vue, Router) {
6565
*/
6666

6767
Router.prototype.beforeEach = function (fn) {
68-
this._beforeEachHook = fn
68+
this._beforeEachHooks.push(fn)
6969
}
7070

7171
/**
@@ -75,7 +75,7 @@ export default function (Vue, Router) {
7575
*/
7676

7777
Router.prototype.afterEach = function (fn) {
78-
this._afterEachHook = fn
78+
this._afterEachHooks.push(fn)
7979
}
8080

8181
/**

src/router/internal.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,15 +150,19 @@ export default function (Vue, Router) {
150150
}
151151

152152
// check global before hook
153-
let before = this._beforeEachHook
153+
let beforeHooks = this._beforeEachHooks
154154
let startTransition = () => {
155155
transition.start(() => {
156156
this._postTransition(route, state, anchor)
157157
})
158158
}
159159

160-
if (before) {
161-
transition.callHook(before, null, startTransition, true)
160+
if (beforeHooks.length) {
161+
transition.runQueue(beforeHooks, (hook, _, next) => {
162+
if (transition === this._currentTransition) {
163+
transition.callHook(hook, null, next, true)
164+
}
165+
}, startTransition)
162166
} else {
163167
startTransition()
164168
}
@@ -195,11 +199,11 @@ export default function (Vue, Router) {
195199
})
196200
}
197201
// call global after hook
198-
if (this._afterEachHook) {
199-
this._afterEachHook.call(null, {
202+
if (this._afterEachHooks.length) {
203+
this._afterEachHooks.forEach(hook => hook.call(null, {
200204
to: transition.to,
201205
from: transition.from
202-
})
206+
}))
203207
}
204208
}
205209

test/unit/specs/core.js

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -416,34 +416,55 @@ describe('Core', function () {
416416
}
417417
}
418418
})
419-
var spy = jasmine.createSpy()
419+
420+
var spy1 = jasmine.createSpy('before hook 1')
421+
router.beforeEach(function (transition) {
422+
spy1()
423+
setTimeout(function () {
424+
transition.next()
425+
}, wait)
426+
})
427+
428+
var spy2 = jasmine.createSpy('before hook 2')
420429
router.beforeEach(function (transition) {
421-
spy()
430+
spy2()
422431
if (transition.to.path === '/no') {
423432
setTimeout(function () {
424433
transition.abort()
425434
next()
426-
}, 100)
435+
}, wait)
427436
} else if (transition.to.path.indexOf('/redirect') > -1) {
428437
setTimeout(function () {
429438
transition.redirect('/to/:id')
430439
next2()
431-
}, 100)
440+
}, wait)
432441
} else {
433442
transition.next()
434443
}
435444
})
445+
436446
router.start(App, el)
437-
expect(spy).toHaveBeenCalled()
438-
expect(router.app.$el.textContent).toBe('default')
439-
router.go('/no')
447+
expect(spy1).toHaveBeenCalled()
448+
expect(spy2).not.toHaveBeenCalled()
449+
expect(router.app.$el.textContent).toBe('')
450+
setTimeout(function () {
451+
expect(spy2).toHaveBeenCalled()
452+
expect(router.app.$el.textContent).toBe('default')
453+
router.go('/no')
454+
}, wait * 2)
440455
function next () {
456+
expect(spy1.calls.count()).toBe(2)
457+
expect(spy2.calls.count()).toBe(2)
441458
expect(router.app.$el.textContent).toBe('default')
442459
router.go('/redirect/12345')
443460
}
444461
function next2 () {
445-
expect(router.app.$el.textContent).toBe('to 12345')
446-
done()
462+
expect(spy1.calls.count()).toBe(4) // go + redirect
463+
expect(spy2.calls.count()).toBe(3) // only go at this moment
464+
setTimeout(function () {
465+
expect(router.app.$el.textContent).toBe('to 12345')
466+
done()
467+
}, wait * 2)
447468
}
448469
})
449470

0 commit comments

Comments
 (0)