feat(runtime-core): 添加预冲洗回调队列功能
- 在 runtime-core 中新增 queuePreFlushCb 函数用于管理预冲洗回调 - 在 vue 包中导出 queuePreFlushCb 函数供外部使用 - 实现调度器中的回调队列机制,包括队列管理和执行逻辑 - 添加去重逻辑确保回调函数只执行一次 - 新增示例文件展示调度器预冲洗回调的使用方法
This commit is contained in:
@@ -0,0 +1 @@
|
||||
export { queuePreFlushCb } from './scheduler'
|
||||
|
||||
@@ -1 +1,35 @@
|
||||
let isFlushPending = false
|
||||
|
||||
const resolvedPromise = Promise.resolve() as Promise<any>
|
||||
|
||||
let currentFlushPromise: Promise<void> | null = null
|
||||
|
||||
const pendingPreFlushCbs: Function[] = []
|
||||
export function queuePreFlushCb(cb: Function) {
|
||||
queueCb(cb, pendingPreFlushCbs)
|
||||
}
|
||||
function queueCb(cb: Function, pendingQueue: Function[]) {
|
||||
pendingQueue.push(cb)
|
||||
queueFlush()
|
||||
}
|
||||
|
||||
function queueFlush() {
|
||||
if (!isFlushPending) {
|
||||
isFlushPending = true
|
||||
currentFlushPromise = resolvedPromise.then(flushJobs)
|
||||
}
|
||||
}
|
||||
function flushJobs() {
|
||||
isFlushPending = false
|
||||
flushPreFlushCbs()
|
||||
}
|
||||
export function flushPreFlushCbs() {
|
||||
if (pendingPreFlushCbs.length) {
|
||||
//拷贝去重,类似深拷贝
|
||||
let activePreFlushCbs = [...new Set(pendingPreFlushCbs)]
|
||||
pendingPreFlushCbs.length = 0
|
||||
for (let i = 0; i < activePreFlushCbs.length; i++) {
|
||||
activePreFlushCbs[i]()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user