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-07-30 21:54:05 +00:00
|
|
|
export interface SchedulerJob {
|
2020-04-14 21:31:35 +00:00
|
|
|
(): void
|
2020-07-30 21:54:05 +00:00
|
|
|
/**
|
|
|
|
* unique job id, only present on raw effects, e.g. component render effect
|
|
|
|
*/
|
2020-04-14 21:31:35 +00:00
|
|
|
id?: number
|
2020-07-30 21:54:05 +00:00
|
|
|
/**
|
2020-08-13 21:27:14 +00:00
|
|
|
* Indicates whether the job is allowed to recursively trigger itself.
|
|
|
|
* By default, a job cannot trigger itself because some built-in method calls,
|
|
|
|
* e.g. Array.prototype.push actually performs reads as well (#1740) which
|
|
|
|
* can lead to confusing infinite loops.
|
|
|
|
* The allowed cases are component render functions and watch callbacks.
|
|
|
|
* Render functions may update child component props, which in turn trigger
|
|
|
|
* flush: "pre" watch callbacks that mutates state that the parent relies on
|
|
|
|
* (#1801). Watch callbacks doesn't track its dependencies so if it triggers
|
|
|
|
* itself again, it's likely intentional and it is the user's responsibility
|
|
|
|
* to perform recursive state mutation that eventually stabilizes (#1727).
|
2020-07-30 21:54:05 +00:00
|
|
|
*/
|
2020-08-13 21:27:14 +00:00
|
|
|
allowRecurse?: boolean
|
2020-04-14 21:31:35 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 13:50:23 +00:00
|
|
|
export type SchedulerCb = Function & { id?: number }
|
|
|
|
export type SchedulerCbs = SchedulerCb | SchedulerCb[]
|
|
|
|
|
2020-08-05 14:55:23 +00:00
|
|
|
let isFlushing = false
|
|
|
|
let isFlushPending = false
|
|
|
|
|
2020-07-30 21:54:05 +00:00
|
|
|
const queue: (SchedulerJob | null)[] = []
|
2020-08-05 14:55:23 +00:00
|
|
|
let flushIndex = 0
|
|
|
|
|
2020-08-14 13:50:23 +00:00
|
|
|
const pendingPreFlushCbs: SchedulerCb[] = []
|
|
|
|
let activePreFlushCbs: SchedulerCb[] | null = null
|
2020-08-05 14:55:23 +00:00
|
|
|
let preFlushIndex = 0
|
|
|
|
|
2020-08-14 13:50:23 +00:00
|
|
|
const pendingPostFlushCbs: SchedulerCb[] = []
|
|
|
|
let activePostFlushCbs: SchedulerCb[] | null = null
|
2020-08-05 14:55:23 +00:00
|
|
|
let postFlushIndex = 0
|
|
|
|
|
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
|
|
|
|
2020-08-04 17:20:09 +00:00
|
|
|
let currentPreFlushParentJob: SchedulerJob | null = null
|
2019-05-28 09:19:47 +00:00
|
|
|
|
2019-11-14 17:06:23 +00:00
|
|
|
const RECURSION_LIMIT = 100
|
2020-08-14 13:50:23 +00:00
|
|
|
type CountMap = Map<SchedulerJob | SchedulerCb, 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-07-30 21:54:05 +00:00
|
|
|
export function queueJob(job: SchedulerJob) {
|
|
|
|
// the dedupe search uses the startIndex argument of Array.includes()
|
|
|
|
// by default the search index includes the current job that is being run
|
|
|
|
// so it cannot recursively trigger itself again.
|
|
|
|
// if the job is a watch() callback, the search will start with a +1 index to
|
|
|
|
// allow it recursively trigger itself - it is the user's responsibility to
|
|
|
|
// ensure it doesn't end up in an infinite loop.
|
|
|
|
if (
|
2020-08-04 17:20:09 +00:00
|
|
|
(!queue.length ||
|
|
|
|
!queue.includes(
|
|
|
|
job,
|
2020-08-13 21:27:14 +00:00
|
|
|
isFlushing && job.allowRecurse ? flushIndex + 1 : flushIndex
|
2020-08-04 17:20:09 +00:00
|
|
|
)) &&
|
|
|
|
job !== currentPreFlushParentJob
|
2020-07-30 21:54:05 +00:00
|
|
|
) {
|
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-08-05 14:55:23 +00:00
|
|
|
function queueFlush() {
|
|
|
|
if (!isFlushing && !isFlushPending) {
|
|
|
|
isFlushPending = true
|
|
|
|
currentFlushPromise = resolvedPromise.then(flushJobs)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-30 21:54:05 +00:00
|
|
|
export function invalidateJob(job: SchedulerJob) {
|
2020-02-10 18:09:15 +00:00
|
|
|
const i = queue.indexOf(job)
|
|
|
|
if (i > -1) {
|
|
|
|
queue[i] = null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:55:23 +00:00
|
|
|
function queueCb(
|
2020-08-14 13:50:23 +00:00
|
|
|
cb: SchedulerCbs,
|
|
|
|
activeQueue: SchedulerCb[] | null,
|
|
|
|
pendingQueue: SchedulerCb[],
|
2020-08-05 14:55:23 +00:00
|
|
|
index: number
|
|
|
|
) {
|
2019-10-14 02:41:23 +00:00
|
|
|
if (!isArray(cb)) {
|
2020-07-16 02:36:41 +00:00
|
|
|
if (
|
2020-08-05 14:55:23 +00:00
|
|
|
!activeQueue ||
|
2020-08-13 21:27:14 +00:00
|
|
|
!activeQueue.includes(
|
|
|
|
cb,
|
|
|
|
(cb as SchedulerJob).allowRecurse ? index + 1 : index
|
|
|
|
)
|
2020-07-16 02:36:41 +00:00
|
|
|
) {
|
2020-08-05 14:55:23 +00:00
|
|
|
pendingQueue.push(cb)
|
2020-07-16 02:36:41 +00:00
|
|
|
}
|
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
|
2020-08-14 21:05:12 +00:00
|
|
|
// we can skip duplicate check here to improve perf
|
2020-08-05 14:55:23 +00:00
|
|
|
pendingQueue.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
|
|
|
|
2020-08-14 13:50:23 +00:00
|
|
|
export function queuePreFlushCb(cb: SchedulerCb) {
|
2020-08-05 14:55:23 +00:00
|
|
|
queueCb(cb, activePreFlushCbs, pendingPreFlushCbs, preFlushIndex)
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:50:23 +00:00
|
|
|
export function queuePostFlushCb(cb: SchedulerCbs) {
|
2020-08-05 14:55:23 +00:00
|
|
|
queueCb(cb, activePostFlushCbs, pendingPostFlushCbs, postFlushIndex)
|
|
|
|
}
|
|
|
|
|
|
|
|
export function flushPreFlushCbs(
|
|
|
|
seen?: CountMap,
|
|
|
|
parentJob: SchedulerJob | null = null
|
|
|
|
) {
|
|
|
|
if (pendingPreFlushCbs.length) {
|
|
|
|
currentPreFlushParentJob = parentJob
|
|
|
|
activePreFlushCbs = [...new Set(pendingPreFlushCbs)]
|
|
|
|
pendingPreFlushCbs.length = 0
|
|
|
|
if (__DEV__) {
|
|
|
|
seen = seen || new Map()
|
|
|
|
}
|
|
|
|
for (
|
|
|
|
preFlushIndex = 0;
|
|
|
|
preFlushIndex < activePreFlushCbs.length;
|
|
|
|
preFlushIndex++
|
|
|
|
) {
|
|
|
|
if (__DEV__) {
|
|
|
|
checkRecursiveUpdates(seen!, activePreFlushCbs[preFlushIndex])
|
|
|
|
}
|
|
|
|
activePreFlushCbs[preFlushIndex]()
|
|
|
|
}
|
|
|
|
activePreFlushCbs = null
|
|
|
|
preFlushIndex = 0
|
|
|
|
currentPreFlushParentJob = null
|
|
|
|
// recursively flush until it drains
|
|
|
|
flushPreFlushCbs(seen, parentJob)
|
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) {
|
2020-08-05 14:55:23 +00:00
|
|
|
if (pendingPostFlushCbs.length) {
|
|
|
|
activePostFlushCbs = [...new Set(pendingPostFlushCbs)]
|
|
|
|
pendingPostFlushCbs.length = 0
|
2019-11-14 17:06:23 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
seen = seen || new Map()
|
|
|
|
}
|
2020-08-14 13:50:23 +00:00
|
|
|
|
|
|
|
activePostFlushCbs.sort((a, b) => getId(a) - getId(b))
|
|
|
|
|
2020-07-16 02:36:41 +00:00
|
|
|
for (
|
2020-08-05 14:55:23 +00:00
|
|
|
postFlushIndex = 0;
|
|
|
|
postFlushIndex < activePostFlushCbs.length;
|
|
|
|
postFlushIndex++
|
2020-07-16 02:36:41 +00:00
|
|
|
) {
|
2019-11-14 17:06:23 +00:00
|
|
|
if (__DEV__) {
|
2020-08-05 14:55:23 +00:00
|
|
|
checkRecursiveUpdates(seen!, activePostFlushCbs[postFlushIndex])
|
2019-11-14 17:06:23 +00:00
|
|
|
}
|
2020-08-05 14:55:23 +00:00
|
|
|
activePostFlushCbs[postFlushIndex]()
|
2019-05-28 11:36:15 +00:00
|
|
|
}
|
2020-08-05 14:55:23 +00:00
|
|
|
activePostFlushCbs = null
|
|
|
|
postFlushIndex = 0
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:50:23 +00:00
|
|
|
const getId = (job: SchedulerJob | SchedulerCb) =>
|
|
|
|
job.id == null ? Infinity : job.id
|
2020-04-14 21:31:35 +00:00
|
|
|
|
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
|
|
|
|
2020-08-05 14:55:23 +00:00
|
|
|
flushPreFlushCbs(seen)
|
|
|
|
|
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-30 21:54:05 +00:00
|
|
|
try {
|
|
|
|
for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
|
|
|
|
const job = queue[flushIndex]
|
|
|
|
if (job) {
|
|
|
|
if (__DEV__) {
|
|
|
|
checkRecursiveUpdates(seen!, job)
|
|
|
|
}
|
|
|
|
callWithErrorHandling(job, null, ErrorCodes.SCHEDULER)
|
2020-07-16 02:36:41 +00:00
|
|
|
}
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
2020-07-30 21:54:05 +00:00
|
|
|
} finally {
|
|
|
|
flushIndex = 0
|
|
|
|
queue.length = 0
|
|
|
|
|
|
|
|
flushPostFlushCbs(seen)
|
2020-08-05 14:55:23 +00:00
|
|
|
|
2020-07-30 21:54:05 +00:00
|
|
|
isFlushing = false
|
|
|
|
currentFlushPromise = null
|
|
|
|
// some postFlushCb queued jobs!
|
|
|
|
// keep flushing until it drains.
|
2020-08-05 14:55:23 +00:00
|
|
|
if (queue.length || pendingPostFlushCbs.length) {
|
2020-07-30 21:54:05 +00:00
|
|
|
flushJobs(seen)
|
|
|
|
}
|
2019-11-14 17:06:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-14 13:50:23 +00:00
|
|
|
function checkRecursiveUpdates(seen: CountMap, fn: SchedulerJob | SchedulerCb) {
|
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(
|
2020-07-30 21:54:05 +00:00
|
|
|
`Maximum recursive updates exceeded. ` +
|
|
|
|
`This means you have a reactive effect that is mutating its own ` +
|
|
|
|
`dependencies and thus recursively triggering itself. Possible sources ` +
|
|
|
|
`include component template, render function, updated hook or ` +
|
|
|
|
`watcher source function.`
|
2019-11-14 17:06:23 +00:00
|
|
|
)
|
|
|
|
} else {
|
|
|
|
seen.set(fn, count + 1)
|
|
|
|
}
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
}
|