13694e662327eb71668f6f7a821ab31f923b729d0dc09605522a43cb1e36cd2e33efdef04a6d2800dac435bffb541dfdd5ae740b6e1aefaf56c4adb8652bd3 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /* @flow */
  2. /* globals MutationObserver */
  3. import { noop } from 'shared/util'
  4. import { handleError } from './error'
  5. import { isIE, isIOS, isNative } from './env'
  6. export let isUsingMicroTask = false
  7. const callbacks = []
  8. let pending = false
  9. function flushCallbacks () {
  10. pending = false
  11. const copies = callbacks.slice(0)
  12. callbacks.length = 0
  13. for (let i = 0; i < copies.length; i++) {
  14. copies[i]()
  15. }
  16. }
  17. // Here we have async deferring wrappers using microtasks.
  18. // In 2.5 we used (macro) tasks (in combination with microtasks).
  19. // However, it has subtle problems when state is changed right before repaint
  20. // (e.g. #6813, out-in transitions).
  21. // Also, using (macro) tasks in event handler would cause some weird behaviors
  22. // that cannot be circumvented (e.g. #7109, #7153, #7546, #7834, #8109).
  23. // So we now use microtasks everywhere, again.
  24. // A major drawback of this tradeoff is that there are some scenarios
  25. // where microtasks have too high a priority and fire in between supposedly
  26. // sequential events (e.g. #4521, #6690, which have workarounds)
  27. // or even between bubbling of the same event (#6566).
  28. let timerFunc
  29. // The nextTick behavior leverages the microtask queue, which can be accessed
  30. // via either native Promise.then or MutationObserver.
  31. // MutationObserver has wider support, however it is seriously bugged in
  32. // UIWebView in iOS >= 9.3.3 when triggered in touch event handlers. It
  33. // completely stops working after triggering a few times... so, if native
  34. // Promise is available, we will use it:
  35. /* istanbul ignore next, $flow-disable-line */
  36. if (typeof Promise !== 'undefined' && isNative(Promise)) {
  37. const p = Promise.resolve()
  38. timerFunc = () => {
  39. p.then(flushCallbacks)
  40. // In problematic UIWebViews, Promise.then doesn't completely break, but
  41. // it can get stuck in a weird state where callbacks are pushed into the
  42. // microtask queue but the queue isn't being flushed, until the browser
  43. // needs to do some other work, e.g. handle a timer. Therefore we can
  44. // "force" the microtask queue to be flushed by adding an empty timer.
  45. if (isIOS) setTimeout(noop)
  46. }
  47. isUsingMicroTask = true
  48. } else if (!isIE && typeof MutationObserver !== 'undefined' && (
  49. isNative(MutationObserver) ||
  50. // PhantomJS and iOS 7.x
  51. MutationObserver.toString() === '[object MutationObserverConstructor]'
  52. )) {
  53. // Use MutationObserver where native Promise is not available,
  54. // e.g. PhantomJS, iOS7, Android 4.4
  55. // (#6466 MutationObserver is unreliable in IE11)
  56. let counter = 1
  57. const observer = new MutationObserver(flushCallbacks)
  58. const textNode = document.createTextNode(String(counter))
  59. observer.observe(textNode, {
  60. characterData: true
  61. })
  62. timerFunc = () => {
  63. counter = (counter + 1) % 2
  64. textNode.data = String(counter)
  65. }
  66. isUsingMicroTask = true
  67. } else if (typeof setImmediate !== 'undefined' && isNative(setImmediate)) {
  68. // Fallback to setImmediate.
  69. // Technically it leverages the (macro) task queue,
  70. // but it is still a better choice than setTimeout.
  71. timerFunc = () => {
  72. setImmediate(flushCallbacks)
  73. }
  74. } else {
  75. // Fallback to setTimeout.
  76. timerFunc = () => {
  77. setTimeout(flushCallbacks, 0)
  78. }
  79. }
  80. export function nextTick (cb?: Function, ctx?: Object) {
  81. let _resolve
  82. callbacks.push(() => {
  83. if (cb) {
  84. try {
  85. cb.call(ctx)
  86. } catch (e) {
  87. handleError(e, ctx, 'nextTick')
  88. }
  89. } else if (_resolve) {
  90. _resolve(ctx)
  91. }
  92. })
  93. if (!pending) {
  94. pending = true
  95. timerFunc()
  96. }
  97. // $flow-disable-line
  98. if (!cb && typeof Promise !== 'undefined') {
  99. return new Promise(resolve => {
  100. _resolve = resolve
  101. })
  102. }
  103. }