2019-10-14 04:32:01 +00:00
|
|
|
import { ErrorCodes, callWithErrorHandling } from './errorHandling'
|
2019-10-14 02:41:23 +00:00
|
|
|
import { isArray } from '@vue/shared'
|
2019-08-30 19:15:23 +00:00
|
|
|
|
2020-04-14 21:31:35 +00:00
|
|
|
export interface Job {
|
|
|
|
(): void
|
|
|
|
id?: number
|
|
|
|
}
|
|
|
|
|
|
|
|
const queue: (Job | null)[] = []
|
2019-05-28 11:36:15 +00:00
|
|
|
const postFlushCbs: Function[] = []
|
2020-07-28 04:20:30 +00:00
|
|
|
const resolvedPromise: Promise<any> = Promise.resolve()
|
|
|
|
let currentFlushPromise: Promise<void> | null = null
|
2019-05-28 09:19:47 +00:00
|
|
|
|
|
|
|
let isFlushing = false
|
2019-10-31 01:41:28 +00:00
|
|
|
let isFlushPending = false
|
2020-07-16 02:36:41 +00:00
|
|
|
let flushIndex = 0
|
|
|
|
let pendingPostFlushCbs: Function[] | null = null
|
|
|
|
let pendingPostFlushIndex = 0
|
2019-05-28 09:19:47 +00:00
|
|
|
|
2019-11-14 17:06:23 +00:00
|
|
|
const RECURSION_LIMIT = 100
|
2020-04-14 21:31:35 +00:00
|
|
|
type CountMap = Map<Job | Function, number>
|
2019-11-14 17:06:23 +00:00
|
|
|
|
2019-05-28 09:19:47 +00:00
|
|
|
export function nextTick(fn?: () => void): Promise<void> {
|
2020-07-28 04:20:30 +00:00
|
|
|
const p = currentFlushPromise || resolvedPromise
|
2019-05-28 09:19:47 +00:00
|
|
|
return fn ? p.then(fn) : p
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:31:35 +00:00
|
|
|
export function queueJob(job: Job) {
|
2020-07-28 16:29:43 +00:00
|
|
|
if (!queue.includes(job, flushIndex + 1)) {
|
2019-05-28 09:19:47 +00:00
|
|
|
queue.push(job)
|
2019-10-31 01:41:28 +00:00
|
|
|
queueFlush()
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:31:35 +00:00
|
|
|
export function invalidateJob(job: Job) {
|
2020-02-10 18:09:15 +00:00
|
|
|
const i = queue.indexOf(job)
|
|
|
|
if (i > -1) {
|
|
|
|
queue[i] = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:15:23 +00:00
|
|
|
export function queuePostFlushCb(cb: Function | Function[]) {
|
2019-10-14 02:41:23 +00:00
|
|
|
if (!isArray(cb)) {
|
2020-07-16 02:36:41 +00:00
|
|
|
if (
|
|
|
|
!pendingPostFlushCbs ||
|
2020-07-28 16:29:43 +00:00
|
|
|
!pendingPostFlushCbs.includes(cb, pendingPostFlushIndex + 1)
|
2020-07-16 02:36:41 +00:00
|
|
|
) {
|
|
|
|
postFlushCbs.push(cb)
|
|
|
|
}
|
2019-10-14 02:41:23 +00:00
|
|
|
} else {
|
2020-07-16 02:36:41 +00:00
|
|
|
// if cb is an array, it is a component lifecycle hook which can only be
|
|
|
|
// triggered by a job, which is already deduped in the main queue, so
|
|
|
|
// we can skip dupicate check here to improve perf
|
2019-10-14 02:41:23 +00:00
|
|
|
postFlushCbs.push(...cb)
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
2019-10-31 01:41:28 +00:00
|
|
|
queueFlush()
|
|
|
|
}
|
2019-10-14 02:41:23 +00:00
|
|
|
|
2019-10-31 01:41:28 +00:00
|
|
|
function queueFlush() {
|
|
|
|
if (!isFlushing && !isFlushPending) {
|
|
|
|
isFlushPending = true
|
2020-07-28 04:20:30 +00:00
|
|
|
currentFlushPromise = resolvedPromise.then(flushJobs)
|
2019-08-19 18:44:52 +00:00
|
|
|
}
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
|
2019-11-14 17:06:23 +00:00
|
|
|
export function flushPostFlushCbs(seen?: CountMap) {
|
2019-05-28 11:36:15 +00:00
|
|
|
if (postFlushCbs.length) {
|
2020-07-16 02:36:41 +00:00
|
|
|
pendingPostFlushCbs = [...new Set(postFlushCbs)]
|
2019-05-28 11:36:15 +00:00
|
|
|
postFlushCbs.length = 0
|
2019-11-14 17:06:23 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
seen = seen || new Map()
|
|
|
|
}
|
2020-07-16 02:36:41 +00:00
|
|
|
for (
|
|
|
|
pendingPostFlushIndex = 0;
|
|
|
|
pendingPostFlushIndex < pendingPostFlushCbs.length;
|
|
|
|
pendingPostFlushIndex++
|
|
|
|
) {
|
2019-11-14 17:06:23 +00:00
|
|
|
if (__DEV__) {
|
2020-07-16 02:36:41 +00:00
|
|
|
checkRecursiveUpdates(seen!, pendingPostFlushCbs[pendingPostFlushIndex])
|
2019-11-14 17:06:23 +00:00
|
|
|
}
|
2020-07-16 02:36:41 +00:00
|
|
|
pendingPostFlushCbs[pendingPostFlushIndex]()
|
2019-05-28 11:36:15 +00:00
|
|
|
}
|
2020-07-16 02:36:41 +00:00
|
|
|
pendingPostFlushCbs = null
|
|
|
|
pendingPostFlushIndex = 0
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:31:35 +00:00
|
|
|
const getId = (job: Job) => (job.id == null ? Infinity : job.id)
|
|
|
|
|
2019-11-14 17:06:23 +00:00
|
|
|
function flushJobs(seen?: CountMap) {
|
2019-10-31 01:41:28 +00:00
|
|
|
isFlushPending = false
|
2019-05-28 09:19:47 +00:00
|
|
|
isFlushing = true
|
|
|
|
if (__DEV__) {
|
2019-11-14 17:06:23 +00:00
|
|
|
seen = seen || new Map()
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
2020-04-14 21:31:35 +00:00
|
|
|
|
|
|
|
// Sort queue before flush.
|
|
|
|
// This ensures that:
|
|
|
|
// 1. Components are updated from parent to child. (because parent is always
|
|
|
|
// created before the child so its render effect will have smaller
|
|
|
|
// priority number)
|
|
|
|
// 2. If a component is unmounted during a parent component's update,
|
|
|
|
// its update can be skipped.
|
|
|
|
// Jobs can never be null before flush starts, since they are only invalidated
|
|
|
|
// during execution of another flushed job.
|
|
|
|
queue.sort((a, b) => getId(a!) - getId(b!))
|
|
|
|
|
2020-07-16 02:36:41 +00:00
|
|
|
for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
|
|
|
|
const job = queue[flushIndex]
|
|
|
|
if (job) {
|
|
|
|
if (__DEV__) {
|
|
|
|
checkRecursiveUpdates(seen!, job)
|
|
|
|
}
|
|
|
|
callWithErrorHandling(job, null, ErrorCodes.SCHEDULER)
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-16 02:36:41 +00:00
|
|
|
flushIndex = 0
|
|
|
|
queue.length = 0
|
|
|
|
|
2019-11-14 17:06:23 +00:00
|
|
|
flushPostFlushCbs(seen)
|
2019-05-28 09:19:47 +00:00
|
|
|
isFlushing = false
|
2020-07-28 04:20:30 +00:00
|
|
|
currentFlushPromise = null
|
2019-05-28 09:19:47 +00:00
|
|
|
// some postFlushCb queued jobs!
|
|
|
|
// keep flushing until it drains.
|
2019-10-31 01:41:28 +00:00
|
|
|
if (queue.length || postFlushCbs.length) {
|
2019-11-14 17:06:23 +00:00
|
|
|
flushJobs(seen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 21:31:35 +00:00
|
|
|
function checkRecursiveUpdates(seen: CountMap, fn: Job | Function) {
|
2019-11-14 17:06:23 +00:00
|
|
|
if (!seen.has(fn)) {
|
|
|
|
seen.set(fn, 1)
|
|
|
|
} else {
|
|
|
|
const count = seen.get(fn)!
|
|
|
|
if (count > RECURSION_LIMIT) {
|
|
|
|
throw new Error(
|
|
|
|
'Maximum recursive updates exceeded. ' +
|
|
|
|
"You may have code that is mutating state in your component's " +
|
|
|
|
'render function or updated hook or watcher source function.'
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
seen.set(fn, count + 1)
|
|
|
|
}
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
}
|