fix(scheduler): warn recursive updates in postFlushCbs as well (#456)
This commit is contained in:
parent
4b2610c468
commit
aa6c67ee2e
@ -8,6 +8,9 @@ const p = Promise.resolve()
|
|||||||
let isFlushing = false
|
let isFlushing = false
|
||||||
let isFlushPending = false
|
let isFlushPending = false
|
||||||
|
|
||||||
|
const RECURSION_LIMIT = 100
|
||||||
|
type CountMap = Map<Function, number>
|
||||||
|
|
||||||
export function nextTick(fn?: () => void): Promise<void> {
|
export function nextTick(fn?: () => void): Promise<void> {
|
||||||
return fn ? p.then(fn) : p
|
return fn ? p.then(fn) : p
|
||||||
}
|
}
|
||||||
@ -37,51 +40,57 @@ function queueFlush() {
|
|||||||
|
|
||||||
const dedupe = (cbs: Function[]): Function[] => [...new Set(cbs)]
|
const dedupe = (cbs: Function[]): Function[] => [...new Set(cbs)]
|
||||||
|
|
||||||
export function flushPostFlushCbs() {
|
export function flushPostFlushCbs(seen?: CountMap) {
|
||||||
if (postFlushCbs.length) {
|
if (postFlushCbs.length) {
|
||||||
const cbs = dedupe(postFlushCbs)
|
const cbs = dedupe(postFlushCbs)
|
||||||
postFlushCbs.length = 0
|
postFlushCbs.length = 0
|
||||||
|
if (__DEV__) {
|
||||||
|
seen = seen || new Map()
|
||||||
|
}
|
||||||
for (let i = 0; i < cbs.length; i++) {
|
for (let i = 0; i < cbs.length; i++) {
|
||||||
|
if (__DEV__) {
|
||||||
|
checkRecursiveUpdates(seen!, cbs[i])
|
||||||
|
}
|
||||||
cbs[i]()
|
cbs[i]()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const RECURSION_LIMIT = 100
|
function flushJobs(seen?: CountMap) {
|
||||||
type JobCountMap = Map<Function, number>
|
|
||||||
|
|
||||||
function flushJobs(seenJobs?: JobCountMap) {
|
|
||||||
isFlushPending = false
|
isFlushPending = false
|
||||||
isFlushing = true
|
isFlushing = true
|
||||||
let job
|
let job
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
seenJobs = seenJobs || new Map()
|
seen = seen || new Map()
|
||||||
}
|
}
|
||||||
while ((job = queue.shift())) {
|
while ((job = queue.shift())) {
|
||||||
if (__DEV__) {
|
if (__DEV__) {
|
||||||
const seen = seenJobs!
|
checkRecursiveUpdates(seen!, job)
|
||||||
if (!seen.has(job)) {
|
|
||||||
seen.set(job, 1)
|
|
||||||
} else {
|
|
||||||
const count = seen.get(job)!
|
|
||||||
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.'
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
seen.set(job, count + 1)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
callWithErrorHandling(job, null, ErrorCodes.SCHEDULER)
|
callWithErrorHandling(job, null, ErrorCodes.SCHEDULER)
|
||||||
}
|
}
|
||||||
flushPostFlushCbs()
|
flushPostFlushCbs(seen)
|
||||||
isFlushing = false
|
isFlushing = false
|
||||||
// some postFlushCb queued jobs!
|
// some postFlushCb queued jobs!
|
||||||
// keep flushing until it drains.
|
// keep flushing until it drains.
|
||||||
if (queue.length || postFlushCbs.length) {
|
if (queue.length || postFlushCbs.length) {
|
||||||
flushJobs(seenJobs)
|
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)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user