Skip to content

Commit d6dd1ef

Browse files
committed
hooks: treat hooks with no argument and non-promise return values as synchronous
1 parent 4f7da0d commit d6dd1ef

File tree

4 files changed

+58
-9
lines changed

4 files changed

+58
-9
lines changed

src/transition.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,9 @@ export default class RouteTransition {
184184
* Call a user provided route transition hook and handle
185185
* the response (e.g. if the user returns a promise).
186186
*
187+
* If the user neither expects an argument nor returns a
188+
* promise, the hook is assumed to be synchronous.
189+
*
187190
* @param {Function} hook
188191
* @param {*} [context]
189192
* @param {Function} [cb]
@@ -263,10 +266,12 @@ export default class RouteTransition {
263266
res.then(function (ok) {
264267
ok ? next() : abort()
265268
}, onError)
269+
} else if (!hook.length) {
270+
next(res)
266271
}
267272
} else if (resIsPromise) {
268273
res.then(next, onError)
269-
} else if (expectData && isPlainOjbect(res)) {
274+
} else if ((expectData && isPlainOjbect(res)) || !hook.length) {
270275
next(res)
271276
}
272277
}

test/unit/lib/pipeline-test-util.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,26 @@ exports.test = function (configs, cb) {
2424
var config = configs[route]
2525
Object.keys(config).forEach(function (hook) {
2626
var fn = config[hook]
27-
config[hook] = function (transition) {
28-
var event = route + '.' + hook
29-
calls.push(event)
30-
var res = typeof fn === 'function'
31-
? fn(transition)
32-
: fn
33-
emitter.emit(event)
34-
return res
27+
if (fn.length) {
28+
config[hook] = function (transition) {
29+
var event = route + '.' + hook
30+
calls.push(event)
31+
var res = typeof fn === 'function'
32+
? fn(transition)
33+
: fn
34+
emitter.emit(event)
35+
return res
36+
}
37+
} else {
38+
config[hook] = function () {
39+
var event = route + '.' + hook
40+
calls.push(event)
41+
var res = typeof fn === 'function'
42+
? fn()
43+
: fn
44+
emitter.emit(event)
45+
return res
46+
}
3547
}
3648
})
3749
})

test/unit/specs/pipeline/activate.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,21 @@ describe('activate', function () {
2424
})
2525
})
2626

27+
it('sync (no arg)', function (done) {
28+
test({
29+
a: {
30+
activate: function () {
31+
// noop
32+
}
33+
}
34+
}, function (router, calls, emitter) {
35+
router.go('/a')
36+
expect(router.app.$el.textContent).toBe('A ')
37+
assertCalls(calls, ['a.activate'])
38+
done()
39+
})
40+
})
41+
2742
it('async', function (done) {
2843
test({
2944
a: {

test/unit/specs/pipeline/deactivate.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,23 @@ describe('deactivate', function () {
2121
})
2222
})
2323

24+
it('sync (no arg)', function (done) {
25+
test({
26+
a: {
27+
deactivate: function () {
28+
// noop
29+
}
30+
}
31+
}, function (router, calls, emitter) {
32+
router.go('/a')
33+
expect(router.app.$el.textContent).toBe('A ')
34+
router.go('/b')
35+
expect(router.app.$el.textContent).toBe('')
36+
assertCalls(calls, ['a.deactivate'])
37+
done()
38+
})
39+
})
40+
2441
it('async', function (done) {
2542
test({
2643
a: {

0 commit comments

Comments
 (0)