vue3-core/packages/scheduler/src/experimental.ts

190 lines
4.2 KiB
TypeScript
Raw Normal View History

import { Op, setCurrentOps } from './patchNodeOps'
interface Job extends Function {
ops: Op[]
post: Function | null
expiration: number
}
2018-11-01 05:58:06 +08:00
const enum Priorities {
NORMAL = 500
}
type ErrorHandler = (err: Error) => any
2018-11-01 05:58:06 +08:00
let start: number = 0
const getNow = () => window.performance.now()
const frameBudget = __JSDOM__ ? Infinity : 1000 / 60
const patchQueue: Job[] = []
const commitQueue: Job[] = []
const postCommitQueue: Function[] = []
const nextTickQueue: Function[] = []
2018-11-01 05:58:06 +08:00
let globalHandler: ErrorHandler
const pendingRejectors: ErrorHandler[] = []
2018-11-01 05:58:06 +08:00
// Microtask for batching state mutations
const p = Promise.resolve()
function flushAfterMicroTask() {
return p.then(flush).catch(handleError)
2018-11-01 05:58:06 +08:00
}
// Macrotask for time slicing
const key = `__vueSchedulerTick`
window.addEventListener(
'message',
event => {
if (event.source !== window || event.data !== key) {
return
}
start = getNow()
try {
flush()
} catch (e) {
handleError(e)
}
2018-11-01 05:58:06 +08:00
},
false
)
function flushAfterMacroTask() {
2018-11-01 05:58:06 +08:00
window.postMessage(key, `*`)
}
export function nextTick<T>(fn?: () => T): Promise<T> {
return new Promise((resolve, reject) => {
p.then(() => {
if (hasPendingFlush) {
nextTickQueue.push(() => {
resolve(fn ? fn() : undefined)
})
2018-11-02 06:43:28 +08:00
pendingRejectors.push(err => {
if (fn) fn()
reject(err)
})
} else {
resolve(fn ? fn() : undefined)
}
}).catch(reject)
})
2018-11-01 05:58:06 +08:00
}
function handleError(err: Error) {
if (globalHandler) globalHandler(err)
pendingRejectors.forEach(handler => {
handler(err)
})
2018-11-01 05:58:06 +08:00
}
export function handleSchedulerError(handler: ErrorHandler) {
globalHandler = handler
2018-11-01 05:58:06 +08:00
}
let hasPendingFlush = false
export function queueJob(rawJob: Function, postJob?: Function | null) {
2018-11-01 05:58:06 +08:00
const job = rawJob as Job
job.post = postJob || null
2018-11-01 05:58:06 +08:00
job.ops = job.ops || []
// 1. let's see if this invalidates any work that
// has already been done.
const commitIndex = commitQueue.indexOf(job)
if (commitIndex > -1) {
// invalidated. remove from commit queue
// and move it back to the patch queue
commitQueue.splice(commitIndex, 1)
invalidateJob(job)
2018-11-01 05:58:06 +08:00
// With varying priorities we should insert job at correct position
// based on expiration time.
for (let i = 0; i < patchQueue.length; i++) {
if (job.expiration < patchQueue[i].expiration) {
patchQueue.splice(i, 0, job)
break
}
}
} else if (patchQueue.indexOf(job) === -1) {
// a new job
job.expiration = getNow() + Priorities.NORMAL
2018-11-01 05:58:06 +08:00
patchQueue.push(job)
}
if (!hasPendingFlush) {
hasPendingFlush = true
start = getNow()
flushAfterMicroTask()
2018-11-01 05:58:06 +08:00
}
}
function flush(): void {
2018-11-01 05:58:06 +08:00
let job
while (true) {
job = patchQueue.shift()
if (job) {
patchJob(job)
2018-11-01 05:58:06 +08:00
} else {
break
}
const now = getNow()
2018-11-01 05:58:06 +08:00
if (now - start > frameBudget && job.expiration > now) {
break
}
}
if (patchQueue.length === 0) {
// all done, time to commit!
while ((job = commitQueue.shift())) {
commitJob(job)
if (job.post && postCommitQueue.indexOf(job.post) < 0) {
postCommitQueue.push(job.post)
2018-11-01 05:58:06 +08:00
}
}
// post commit hooks (updated, mounted)
while ((job = postCommitQueue.shift())) {
2018-11-01 05:58:06 +08:00
job()
}
// some post commit hook triggered more updates...
2018-11-01 05:58:06 +08:00
if (patchQueue.length > 0) {
if (getNow() - start > frameBudget) {
return flushAfterMacroTask()
} else {
// not out of budget yet, flush sync
return flush()
}
2018-11-01 05:58:06 +08:00
}
// now we are really done
2018-11-01 05:58:06 +08:00
hasPendingFlush = false
pendingRejectors.length = 0
while ((job = nextTickQueue.shift())) {
job()
}
2018-11-01 05:58:06 +08:00
} else {
// got more job to do
flushAfterMacroTask()
}
}
function patchJob(job: Job) {
// job with existing ops means it's already been patched in a low priority queue
if (job.ops.length === 0) {
setCurrentOps(job.ops)
job()
2018-11-09 01:54:11 +08:00
setCurrentOps(null)
commitQueue.push(job)
}
}
function commitJob({ ops }: Job) {
for (let i = 0; i < ops.length; i++) {
const [fn, ...args] = ops[i]
fn(...args)
2018-11-01 05:58:06 +08:00
}
ops.length = 0
}
function invalidateJob(job: Job) {
job.ops.length = 0
2018-11-01 05:58:06 +08:00
}