mirror of
https://gitee.com/vuejs/vue.git
synced 2024-12-02 12:07:39 +08:00
fix nextTick Promise implementation for polyfills
This commit is contained in:
parent
6c7bc69328
commit
f242e119fa
@ -86,18 +86,20 @@ export const nextTick = (function () {
|
||||
}
|
||||
}
|
||||
|
||||
return function queueNextTick (cb: Function, ctx?: Object) {
|
||||
if (cb) {
|
||||
var func = ctx
|
||||
? function () { cb.call(ctx) }
|
||||
: cb
|
||||
callbacks.push(func)
|
||||
if (!pending) {
|
||||
pending = true
|
||||
timerFunc()
|
||||
}
|
||||
} else if (typeof Promise !== 'undefined') {
|
||||
return Promise.resolve(ctx)
|
||||
return function queueNextTick (cb?: Function, ctx?: Object) {
|
||||
let _resolve
|
||||
callbacks.push(() => {
|
||||
if (cb) cb.call(ctx)
|
||||
if (_resolve) _resolve(ctx)
|
||||
})
|
||||
if (!pending) {
|
||||
pending = true
|
||||
timerFunc()
|
||||
}
|
||||
if (!cb && typeof Promise !== 'undefined') {
|
||||
return new Promise(resolve => {
|
||||
_resolve = resolve
|
||||
})
|
||||
}
|
||||
}
|
||||
})()
|
||||
|
@ -119,7 +119,7 @@ describe('Instance methods lifecycle', () => {
|
||||
}
|
||||
}).$mount()
|
||||
vm.msg = 'bar'
|
||||
vm.$nextTick().then(function (ctx) {
|
||||
vm.$nextTick().then(ctx => {
|
||||
expect(ctx).toBe(vm)
|
||||
expect(vm.$el.textContent).toBe('bar')
|
||||
done()
|
||||
|
@ -1,3 +1,5 @@
|
||||
require('es6-promise/auto')
|
||||
|
||||
// import all helpers
|
||||
const helpersContext = require.context('../helpers', true)
|
||||
helpersContext.keys().forEach(helpersContext)
|
||||
|
@ -6,7 +6,7 @@ describe('nextTick', () => {
|
||||
})
|
||||
|
||||
it('returns undefined when passed a callback', () => {
|
||||
expect(typeof nextTick(() => {})).toBe('undefined')
|
||||
expect(nextTick(() => {})).toBeUndefined()
|
||||
})
|
||||
|
||||
if (typeof Promise !== 'undefined') {
|
||||
@ -21,5 +21,14 @@ describe('nextTick', () => {
|
||||
done()
|
||||
})
|
||||
})
|
||||
|
||||
it('returned Promise should resolve correctly vs callback', done => {
|
||||
const spy = jasmine.createSpy()
|
||||
nextTick(spy)
|
||||
nextTick().then(() => {
|
||||
expect(spy).toHaveBeenCalled()
|
||||
done()
|
||||
})
|
||||
})
|
||||
}
|
||||
})
|
||||
|
Loading…
Reference in New Issue
Block a user