Sfoglia il codice sorgente

fix nextTick Promise implementation for polyfills

Evan You 9 anni fa
parent
commit
f242e119fa

+ 14 - 12
src/core/util/env.js

@@ -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
+      })
     }
   }
 })()

+ 1 - 1
test/unit/features/instance/methods-lifecycle.spec.js

@@ -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()

+ 2 - 0
test/unit/index.js

@@ -1,3 +1,5 @@
+require('es6-promise/auto')
+
 // import all helpers
 const helpersContext = require.context('../helpers', true)
 helpersContext.keys().forEach(helpersContext)

+ 10 - 1
test/unit/modules/util/next-tick.spec.js

@@ -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()
+      })
+    })
   }
 })