refactor(scheduler): improve infinite loop detection

This commit is contained in:
Evan You 2018-10-28 12:59:08 -04:00
parent 98e79943d2
commit 665ff43fad

View File

@ -1,10 +1,8 @@
const queue: Array<() => void> = [] const queue: Array<() => void> = []
const postFlushCbs: Array<() => void> = [] const postFlushCbs: Array<() => void> = []
const postFlushCbsForNextTick: Array<() => void> = []
const p = Promise.resolve() const p = Promise.resolve()
let isFlushing = false let isFlushing = false
let isFlushingPostCbs = false
export function nextTick(fn?: () => void): Promise<void> { export function nextTick(fn?: () => void): Promise<void> {
return p.then(fn) return p.then(fn)
@ -17,58 +15,54 @@ export function queueJob(
) { ) {
if (queue.indexOf(job) === -1) { if (queue.indexOf(job) === -1) {
queue.push(job) queue.push(job)
if (!isFlushing || isFlushingPostCbs) { if (!isFlushing) {
const p = nextTick(flushJobs) const p = nextTick(flushJobs)
if (onError) p.catch(onError) if (onError) p.catch(onError)
} }
} }
if (postFlushCb) { if (postFlushCb && postFlushCbs.indexOf(postFlushCb) === -1) {
if (isFlushingPostCbs) {
// it's possible for a postFlushCb to queue another job/cb combo,
// e.g. triggering a state update inside the updated hook.
if (postFlushCbsForNextTick.indexOf(postFlushCb) === -1) {
postFlushCbsForNextTick.push(postFlushCb)
}
} else if (postFlushCbs.indexOf(postFlushCb) === -1) {
postFlushCbs.push(postFlushCb) postFlushCbs.push(postFlushCb)
} }
} }
}
const seenJobs = new Map()
const RECURSION_LIMIT = 100 const RECURSION_LIMIT = 100
type JobCountMap = Map<Function, number>
function flushJobs() { function flushJobs(seenJobs?: JobCountMap) {
seenJobs.clear()
isFlushing = true isFlushing = true
let job let job
if (__DEV__) {
seenJobs = seenJobs || new Map()
}
while ((job = queue.shift())) { while ((job = queue.shift())) {
if (__DEV__) { if (__DEV__) {
if (!seenJobs.has(job)) { const seen = seenJobs as JobCountMap
seenJobs.set(job, 1) if (!seen.has(job)) {
seen.set(job, 1)
} else { } else {
const count = seenJobs.get(job) const count = seen.get(job) as number
if (count > RECURSION_LIMIT) { if (count > RECURSION_LIMIT) {
throw new Error('Maximum recursive updates exceeded') throw new Error(
'Maximum recursive updates exceeded. ' +
"You may have code that is mutating state in your component's " +
'render function or updated hook.'
)
} else { } else {
seenJobs.set(job, count + 1) seen.set(job, count + 1)
} }
} }
} }
job() job()
} }
isFlushingPostCbs = true const cbs = postFlushCbs.slice()
if (postFlushCbsForNextTick.length > 0) {
const postFlushCbsFromPrevTick = postFlushCbsForNextTick.slice()
postFlushCbsForNextTick.length = 0
for (let i = 0; i < postFlushCbsFromPrevTick.length; i++) {
postFlushCbsFromPrevTick[i]()
}
}
for (let i = 0; i < postFlushCbs.length; i++) {
postFlushCbs[i]()
}
postFlushCbs.length = 0 postFlushCbs.length = 0
isFlushingPostCbs = false for (let i = 0; i < cbs.length; i++) {
isFlushing = false cbs[i]()
}
isFlushing = false
// some postFlushCb queued jobs!
// keep flushing until it drains.
if (queue.length) {
flushJobs(seenJobs)
}
} }