Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 | 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 94x 1890x 1890x 72x 72x 72x 67x 67x 67x 67x 40x 27x 72x 1631x 1567x 1567x 1567x 1495x 72x 1567x 1567x 4895x 2068x 3328x 2193x 1x 2192x 2191x 2191x 1135x 2001243x 3328x 2111x 2049x 2111x 91x 91x 51x 14x 37x 37x 37x 51x 31x 37x 37x 6x 4068x 1849x 1849x 1849x 725x 725x 1225x 1225x 6x 1219x 2001854x 1849x 1849x 3x 1000003x 3x 1846x 1846x 1779x 1846x 2003427x 2003427x 2003427x 38x 2003427x 2003427x 1846x 1846x 94x 1784x 6x 6x 1778x 1784x 1784x 1784x 1784x 1784x 1776x 94x 4005461x 2086x 2028x 2086x 1478x 2086x 2086x 1529x 1529x 1523x 1523x 1414x 1523x 1523x 107x 2086x 3x 3x 3x 2086x 2086x 2086x 2086x 2086x 18x 2004805x 2004805x 2004805x 2004805x | import { ErrorCodes, callWithErrorHandling, handleError } from './errorHandling'
import { NOOP, isArray } from '@vue/shared'
import { type ComponentInternalInstance, getComponentName } from './component'
export enum SchedulerJobFlags {
QUEUED = 1 << 0,
PRE = 1 << 1,
/**
* Indicates whether the effect is allowed to recursively trigger itself
* when managed by the scheduler.
*
* 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 update functions and watch callbacks.
* Component update 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).
*/
ALLOW_RECURSE = 1 << 2,
DISPOSED = 1 << 3,
/**
* A post job buffered by a suspense boundary is queued, not dropped, when
* the pending branch is discarded.
*/
REQUEUE_ON_SUSPENSE_DISCARD = 1 << 4,
/**
* A post job buffered by a suspense boundary is queued as soon as that
* boundary resolves.
*/
FLUSH_ON_SUSPENSE_RESOLVE = 1 << 5,
}
export interface SchedulerJob extends Function {
id?: number
/**
* flags can technically be undefined, but it can still be used in bitwise
* operations just like 0.
*/
flags?: SchedulerJobFlags
/**
* Attached by renderer.ts when setting up a component's render effect
* Used to obtain component information when reporting max recursive updates.
* Also set on watcher jobs: null marks a watcher without an owner component.
*/
i?: ComponentInternalInstance | null
}
export type SchedulerJobs = SchedulerJob | SchedulerJob[]
const queue: SchedulerJob[] = []
let flushIndex = -1
const pendingPostFlushCbs: SchedulerJob[] = []
let activePostFlushCbs: SchedulerJob[] | null = null
let postFlushIndex = 0
const resolvedPromise = /*@__PURE__*/ Promise.resolve() as Promise<any>
let currentFlushPromise: Promise<void> | null = null
const RECURSION_LIMIT = 100
type CountMap = Map<SchedulerJob, number>
export function nextTick(): Promise<void>
export function nextTick<T, R>(
this: T,
fn: (this: T) => R | Promise<R>,
): Promise<R>
export function nextTick<T, R>(
this: T,
fn?: (this: T) => R | Promise<R>,
): Promise<void | R> {
const p = currentFlushPromise || resolvedPromise
return fn ? p.then(this ? fn.bind(this) : fn) : p
}
// Use binary-search to find a suitable position in the queue. The queue needs
// to be sorted in increasing order of the job ids. This ensures that:
// 1. Components are updated from parent to child. As the parent is always
// created before the child it will always have a smaller id.
// 2. If a component is unmounted during a parent component's update, its update
// can be skipped.
// A pre watcher will have the same id as its component's update job. The
// watcher should be inserted immediately before the update job. This allows
// watchers to be skipped if the component is unmounted by the parent update.
function findInsertionIndex(id: number) {
let start = flushIndex + 1
let end = queue.length
while (start < end) {
const middle = (start + end) >>> 1
const middleJob = queue[middle]
const middleJobId = getId(middleJob)
if (
middleJobId < id ||
(middleJobId === id && middleJob.flags! & SchedulerJobFlags.PRE)
) {
start = middle + 1
} else {
end = middle
}
}
return start
}
export function queueJob(job: SchedulerJob): void {
if (!(job.flags! & SchedulerJobFlags.QUEUED)) {
const jobId = getId(job)
const lastJob = queue[queue.length - 1]
if (
!lastJob ||
// fast path when the job id is larger than the tail
(!(job.flags! & SchedulerJobFlags.PRE) && jobId >= getId(lastJob))
) {
queue.push(job)
} else {
queue.splice(findInsertionIndex(jobId), 0, job)
}
job.flags! |= SchedulerJobFlags.QUEUED
queueFlush()
}
}
function queueFlush() {
if (!currentFlushPromise) {
currentFlushPromise = resolvedPromise.then(flushJobs)
}
}
export function queuePostFlushCb(cb: SchedulerJobs): void {
if (!isArray(cb)) {
if (activePostFlushCbs && cb.id === -1) {
activePostFlushCbs.splice(postFlushIndex + 1, 0, cb)
} else if (!(cb.flags! & SchedulerJobFlags.QUEUED)) {
pendingPostFlushCbs.push(cb)
cb.flags! |= SchedulerJobFlags.QUEUED
}
} else {
// 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 duplicate check here to improve perf.
// use a loop instead of `push(...cb)`: the array can be arbitrarily large
// (e.g. effects buffered by a suspense boundary) and spreading it would
// overflow the call stack (#15142)
for (let i = 0; i < cb.length; i++) {
pendingPostFlushCbs.push(cb[i])
}
}
queueFlush()
}
export function flushPreFlushCbs(
instance?: ComponentInternalInstance,
// root mount: only flush the jobs of the components it created
minUid?: number,
seen?: CountMap,
// skip the current job
i: number = flushIndex + 1,
): void {
if (__DEV__) {
seen = seen || new Map()
}
for (; i < queue.length; i++) {
const cb = queue[i]
if (cb && cb.flags! & SchedulerJobFlags.PRE) {
if (
(instance && cb.id !== instance.uid) ||
(minUid !== undefined && !(cb.id! >= minUid))
) {
continue
}
Iif (__DEV__ && checkRecursiveUpdates(seen!, cb)) {
continue
}
queue.splice(i, 1)
// the job may queue a job with a smaller id ahead of this index. A job
// of the same instance can't, so no rescan is needed for that filter.
i = instance ? i - 1 : flushIndex
if (cb.flags! & SchedulerJobFlags.ALLOW_RECURSE) {
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
cb()
if (!(cb.flags! & SchedulerJobFlags.ALLOW_RECURSE)) {
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
}
}
}
export function flushPostFlushCbs(
seen?: CountMap,
// root mount: cbs queued before it, left in the queue
skip?: Set<SchedulerJob>,
// root mount: watchers are kept or flushed by owner instead, so that those
// of other components (or without one) are left too
minUid = 0,
): void {
if (pendingPostFlushCbs.length) {
let cbs = pendingPostFlushCbs
let kept = 0
if (skip) {
cbs = []
for (let i = 0; i < pendingPostFlushCbs.length; i++) {
const cb = pendingPostFlushCbs[i]
if (cb.i !== undefined ? !cb.i || cb.i.uid < minUid : skip.has(cb)) {
pendingPostFlushCbs[kept++] = cb
} else {
cbs.push(cb)
}
}
}
const deduped = [...new Set(cbs)].sort((a, b) => getId(a) - getId(b))
pendingPostFlushCbs.length = kept
// #1947 already has active queue, nested flushPostFlushCbs call
if (activePostFlushCbs) {
for (let i = 0; i < deduped.length; i++) {
activePostFlushCbs.push(deduped[i])
}
return
}
activePostFlushCbs = deduped
if (__DEV__) {
seen = seen || new Map()
}
for (
postFlushIndex = 0;
postFlushIndex < activePostFlushCbs.length;
postFlushIndex++
) {
const cb = activePostFlushCbs[postFlushIndex]
Iif (__DEV__ && checkRecursiveUpdates(seen!, cb)) {
continue
}
if (cb.flags! & SchedulerJobFlags.ALLOW_RECURSE) {
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
if (!(cb.flags! & SchedulerJobFlags.DISPOSED)) cb()
cb.flags! &= ~SchedulerJobFlags.QUEUED
}
activePostFlushCbs = null
postFlushIndex = 0
}
}
let isFlushing = false
/**
* Run a root render, then flush the jobs it is responsible for. A mount or
* unmount (`minUid` given) only flushes the jobs of the components it created
* and the post cbs it queued, so pre watchers triggered in setup run before
* mounted (#5721) while the jobs of other components stay in the queue and
* keep their order (#6728). A root update keeps flushing all pending pre jobs
* and post cbs, as the jobs of the components it re-renders may predate it.
*/
export function flushOnAppMount(fn: () => void, minUid?: number): void {
if (isFlushing) {
fn()
return
}
const queued = minUid === undefined ? undefined : new Set(pendingPostFlushCbs)
fn()
isFlushing = true
try {
flushPreFlushCbs(undefined, minUid)
flushPostFlushCbs(undefined, queued, minUid)
} finally {
isFlushing = false
}
}
const getId = (job: SchedulerJob): number =>
job.id == null ? (job.flags! & SchedulerJobFlags.PRE ? -1 : Infinity) : job.id
function flushJobs(seen?: CountMap) {
if (__DEV__) {
seen = seen || new Map()
}
// conditional usage of checkRecursiveUpdates must be determined out of
// try ... catch block since Rollup by default de-optimizes treeshaking
// inside try-catch. This can leave all warning code unshaked. Although
// they would get eventually shaken by a minifier like terser, some minifiers
// would fail to do that (e.g. https://github.com/evanw/esbuild/issues/1610)
const check = __DEV__
? (job: SchedulerJob) => checkRecursiveUpdates(seen!, job)
: NOOP
try {
for (flushIndex = 0; flushIndex < queue.length; flushIndex++) {
const job = queue[flushIndex]
if (job && !(job.flags! & SchedulerJobFlags.DISPOSED)) {
Iif (__DEV__ && check(job)) {
continue
}
if (job.flags! & SchedulerJobFlags.ALLOW_RECURSE) {
job.flags! &= ~SchedulerJobFlags.QUEUED
}
callWithErrorHandling(
job,
job.i,
job.i ? ErrorCodes.COMPONENT_UPDATE : ErrorCodes.SCHEDULER,
)
if (!(job.flags! & SchedulerJobFlags.ALLOW_RECURSE)) {
job.flags! &= ~SchedulerJobFlags.QUEUED
}
}
}
} finally {
// If there was an error we still need to clear the QUEUED flags
for (; flushIndex < queue.length; flushIndex++) {
const job = queue[flushIndex]
Eif (job) {
job.flags! &= ~SchedulerJobFlags.QUEUED
}
}
flushIndex = -1
queue.length = 0
flushPostFlushCbs(seen)
currentFlushPromise = null
// If new jobs have been added to either queue, keep flushing
if (queue.length || pendingPostFlushCbs.length) {
flushJobs(seen)
}
}
}
function checkRecursiveUpdates(seen: CountMap, fn: SchedulerJob) {
const count = seen.get(fn) || 0
Iif (count > RECURSION_LIMIT) {
const instance = fn.i
const componentName = instance && getComponentName(instance.type)
handleError(
`Maximum recursive updates exceeded${
componentName ? ` in component <${componentName}>` : ``
}. ` +
`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.`,
null,
ErrorCodes.APP_ERROR_HANDLER,
)
return true
}
seen.set(fn, count + 1)
return false
}
|