scheduler.ts 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. import { ErrorCodes, callWithErrorHandling } from './errorHandling'
  2. import { isArray } from '@vue/shared'
  3. import { ComponentPublicInstance } from './componentPublicInstance'
  4. export interface SchedulerJob {
  5. (): void
  6. /**
  7. * unique job id, only present on raw effects, e.g. component render effect
  8. */
  9. id?: number
  10. /**
  11. * Indicates whether the job is allowed to recursively trigger itself.
  12. * By default, a job cannot trigger itself because some built-in method calls,
  13. * e.g. Array.prototype.push actually performs reads as well (#1740) which
  14. * can lead to confusing infinite loops.
  15. * The allowed cases are component update functions and watch callbacks.
  16. * Component update functions may update child component props, which in turn
  17. * trigger flush: "pre" watch callbacks that mutates state that the parent
  18. * relies on (#1801). Watch callbacks doesn't track its dependencies so if it
  19. * triggers itself again, it's likely intentional and it is the user's
  20. * responsibility to perform recursive state mutation that eventually
  21. * stabilizes (#1727).
  22. */
  23. allowRecurse?: boolean
  24. }
  25. export type SchedulerCb = Function & { id?: number }
  26. export type SchedulerCbs = SchedulerCb | SchedulerCb[]
  27. let isFlushing = false
  28. let isFlushPending = false
  29. const queue: (SchedulerJob | null)[] = []
  30. let flushIndex = 0
  31. const pendingPreFlushCbs: SchedulerCb[] = []
  32. let activePreFlushCbs: SchedulerCb[] | null = null
  33. let preFlushIndex = 0
  34. const pendingPostFlushCbs: SchedulerCb[] = []
  35. let activePostFlushCbs: SchedulerCb[] | null = null
  36. let postFlushIndex = 0
  37. const resolvedPromise: Promise<any> = Promise.resolve()
  38. let currentFlushPromise: Promise<void> | null = null
  39. let currentPreFlushParentJob: SchedulerJob | null = null
  40. const RECURSION_LIMIT = 100
  41. type CountMap = Map<SchedulerJob | SchedulerCb, number>
  42. export function nextTick(
  43. this: ComponentPublicInstance | void,
  44. fn?: () => void
  45. ): Promise<void> {
  46. const p = currentFlushPromise || resolvedPromise
  47. return fn ? p.then(this ? fn.bind(this) : fn) : p
  48. }
  49. export function queueJob(job: SchedulerJob) {
  50. // the dedupe search uses the startIndex argument of Array.includes()
  51. // by default the search index includes the current job that is being run
  52. // so it cannot recursively trigger itself again.
  53. // if the job is a watch() callback, the search will start with a +1 index to
  54. // allow it recursively trigger itself - it is the user's responsibility to
  55. // ensure it doesn't end up in an infinite loop.
  56. if (
  57. (!queue.length ||
  58. !queue.includes(
  59. job,
  60. isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex
  61. )) &&
  62. job !== currentPreFlushParentJob
  63. ) {
  64. queue.push(job)
  65. queueFlush()
  66. }
  67. }
  68. function queueFlush() {
  69. if (!isFlushing && !isFlushPending) {
  70. isFlushPending = true
  71. currentFlushPromise = resolvedPromise.then(flushJobs)
  72. }
  73. }
  74. export function invalidateJob(job: SchedulerJob) {
  75. const i = queue.indexOf(job)
  76. if (i > -1) {
  77. queue[i] = null
  78. }
  79. }
  80. function queueCb(
  81. cb: SchedulerCbs,
  82. activeQueue: SchedulerCb[] | null,
  83. pendingQueue: SchedulerCb[],
  84. index: number
  85. ) {
  86. if (!isArray(cb)) {
  87. if (
  88. !activeQueue ||
  89. !activeQueue.includes(
  90. cb,
  91. (cb as SchedulerJob).allowRecurse ? index + 1 : index
  92. )
  93. ) {
  94. pendingQueue.push(cb)
  95. }
  96. } else {
  97. // if cb is an array, it is a component lifecycle hook which can only be
  98. // triggered by a job, which is already deduped in the main queue, so
  99. // we can skip duplicate check here to improve perf
  100. pendingQueue.push(...cb)
  101. }
  102. queueFlush()
  103. }
  104. export function queuePreFlushCb(cb: SchedulerCb) {
  105. queueCb(cb, activePreFlushCbs, pendingPreFlushCbs, preFlushIndex)
  106. }
  107. export function queuePostFlushCb(cb: SchedulerCbs) {
  108. queueCb(cb, activePostFlushCbs, pendingPostFlushCbs, postFlushIndex)
  109. }
  110. export function flushPreFlushCbs(
  111. seen?: CountMap,
  112. parentJob: SchedulerJob | null = null
  113. ) {
  114. if (pendingPreFlushCbs.length) {
  115. currentPreFlushParentJob = parentJob
  116. activePreFlushCbs = [...new Set(pendingPreFlushCbs)]
  117. pendingPreFlushCbs.length = 0
  118. if (__DEV__) {
  119. seen = seen || new Map()
  120. }
  121. for (
  122. preFlushIndex = 0;
  123. preFlushIndex < activePreFlushCbs.length;
  124. preFlushIndex++
  125. ) {
  126. if (__DEV__) {
  127. checkRecursiveUpdates(seen!, activePreFlushCbs[preFlushIndex])
  128. }
  129. activePreFlushCbs[preFlushIndex]()
  130. }
  131. activePreFlushCbs = null
  132. preFlushIndex = 0
  133. currentPreFlushParentJob = null
  134. // recursively flush until it drains
  135. flushPreFlushCbs(seen, parentJob)
  136. }
  137. }
  138. export function flushPostFlushCbs(seen?: CountMap) {
  139. if (pendingPostFlushCbs.length) {
  140. const deduped = [...new Set(pendingPostFlushCbs)]
  141. pendingPostFlushCbs.length = 0
  142. // #1947 already has active queue, nested flushPostFlushCbs call
  143. if (activePostFlushCbs) {
  144. activePostFlushCbs.push(...deduped)
  145. return
  146. }
  147. activePostFlushCbs = deduped
  148. if (__DEV__) {
  149. seen = seen || new Map()
  150. }
  151. activePostFlushCbs.sort((a, b) => getId(a) - getId(b))
  152. for (
  153. postFlushIndex = 0;
  154. postFlushIndex < activePostFlushCbs.length;
  155. postFlushIndex++
  156. ) {
  157. if (__DEV__) {
  158. checkRecursiveUpdates(seen!, activePostFlushCbs[postFlushIndex])
  159. }
  160. activePostFlushCbs[postFlushIndex]()
  161. }
  162. activePostFlushCbs = null
  163. postFlushIndex = 0
  164. }
  165. }
  166. const getId = (job: SchedulerJob | SchedulerCb) =>
  167. job.id == null ? Infinity : job.id
  168. function flushJobs(seen?: CountMap) {
  169. isFlushPending = false
  170. isFlushing = true
  171. if (__DEV__) {
  172. seen = seen || new Map()
  173. }
  174. flushPreFlushCbs(seen)
  175. // Sort queue before flush.
  176. // This ensures that:
  177. // 1. Components are updated from parent to child. (because parent is always
  178. // created before the child so its render effect will have smaller
  179. // priority number)
  180. // 2. If a component is unmounted during a parent component's update,
  181. // its update can be skipped.
  182. // Jobs can never be null before flush starts, since they are only invalidated
  183. // during execution of another flushed job.
  184. queue.sort((a, b) => getId(a!) - getId(b!))
  185. try {
  186. for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
  187. const job = queue[flushIndex]
  188. if (job) {
  189. if (__DEV__) {
  190. checkRecursiveUpdates(seen!, job)
  191. }
  192. callWithErrorHandling(job, null, ErrorCodes.SCHEDULER)
  193. }
  194. }
  195. } finally {
  196. flushIndex = 0
  197. queue.length = 0
  198. flushPostFlushCbs(seen)
  199. isFlushing = false
  200. currentFlushPromise = null
  201. // some postFlushCb queued jobs!
  202. // keep flushing until it drains.
  203. if (queue.length || pendingPostFlushCbs.length) {
  204. flushJobs(seen)
  205. }
  206. }
  207. }
  208. function checkRecursiveUpdates(seen: CountMap, fn: SchedulerJob | SchedulerCb) {
  209. if (!seen.has(fn)) {
  210. seen.set(fn, 1)
  211. } else {
  212. const count = seen.get(fn)!
  213. if (count > RECURSION_LIMIT) {
  214. throw new Error(
  215. `Maximum recursive updates exceeded. ` +
  216. `This means you have a reactive effect that is mutating its own ` +
  217. `dependencies and thus recursively triggering itself. Possible sources ` +
  218. `include component template, render function, updated hook or ` +
  219. `watcher source function.`
  220. )
  221. } else {
  222. seen.set(fn, count + 1)
  223. }
  224. }
  225. }