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
|
|
|
|
2019-05-28 11:36:15 +00:00
|
|
|
const queue: Function[] = []
|
|
|
|
const postFlushCbs: Function[] = []
|
2019-05-28 09:19:47 +00:00
|
|
|
const p = Promise.resolve()
|
|
|
|
|
|
|
|
let isFlushing = false
|
2019-10-31 01:41:28 +00:00
|
|
|
let isFlushPending = false
|
2019-05-28 09:19:47 +00:00
|
|
|
|
2019-11-14 17:06:23 +00:00
|
|
|
const RECURSION_LIMIT = 100
|
|
|
|
type CountMap = Map<Function, number>
|
|
|
|
|
2019-05-28 09:19:47 +00:00
|
|
|
export function nextTick(fn?: () => void): Promise<void> {
|
|
|
|
return fn ? p.then(fn) : p
|
|
|
|
}
|
|
|
|
|
2019-08-30 19:15:23 +00:00
|
|
|
export function queueJob(job: () => void) {
|
2019-10-14 02:41:23 +00:00
|
|
|
if (!queue.includes(job)) {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)) {
|
2019-05-28 11:59:54 +00:00
|
|
|
postFlushCbs.push(cb)
|
2019-10-14 02:41:23 +00:00
|
|
|
} else {
|
|
|
|
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
|
2019-08-30 19:15:23 +00:00
|
|
|
nextTick(flushJobs)
|
2019-08-19 18:44:52 +00:00
|
|
|
}
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 02:41:23 +00:00
|
|
|
const dedupe = (cbs: Function[]): Function[] => [...new Set(cbs)]
|
2019-05-28 11:36:15 +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) {
|
|
|
|
const cbs = dedupe(postFlushCbs)
|
|
|
|
postFlushCbs.length = 0
|
2019-11-14 17:06:23 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
seen = seen || new Map()
|
|
|
|
}
|
2019-05-28 11:36:15 +00:00
|
|
|
for (let i = 0; i < cbs.length; i++) {
|
2019-11-14 17:06:23 +00:00
|
|
|
if (__DEV__) {
|
|
|
|
checkRecursiveUpdates(seen!, cbs[i])
|
|
|
|
}
|
2019-05-28 11:36:15 +00:00
|
|
|
cbs[i]()
|
|
|
|
}
|
2019-05-28 09:19:47 +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
|
|
|
|
let job
|
|
|
|
if (__DEV__) {
|
2019-11-14 17:06:23 +00:00
|
|
|
seen = seen || new Map()
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
|
|
|
while ((job = queue.shift())) {
|
|
|
|
if (__DEV__) {
|
2019-11-14 17:06:23 +00:00
|
|
|
checkRecursiveUpdates(seen!, job)
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
2019-10-14 04:32:01 +00:00
|
|
|
callWithErrorHandling(job, null, ErrorCodes.SCHEDULER)
|
2019-05-28 09:19:47 +00:00
|
|
|
}
|
2019-11-14 17:06:23 +00:00
|
|
|
flushPostFlushCbs(seen)
|
2019-05-28 09:19:47 +00:00
|
|
|
isFlushing = false
|
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function checkRecursiveUpdates(seen: CountMap, fn: Function) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|