Skip to content

Commit

Permalink
feat: support pre flush job
Browse files Browse the repository at this point in the history
  • Loading branch information
cuixiaorui committed Jul 27, 2022
1 parent bc4f11c commit 4f9e6b5
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions packages/runtime-core/src/scheduler.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const queue: any[] = [];
const activePreFlushCbs:any = [];

const p = Promise.resolve();
let isFlushPending = false;
Expand Down Expand Up @@ -27,12 +28,38 @@ function queueFlush() {
nextTick(flushJobs);
}

export function queuePreFlushCb(cb) {
queueCb(cb, activePreFlushCbs);
}

function queueCb(cb, activeQueue) {
// 直接添加到对应的列表内就ok
// todo 这里没有考虑 activeQueue 是否已经存在 cb 的情况
// 然后在执行 flushJobs 的时候就可以调用 activeQueue 了
activeQueue.push(cb);
}

function flushJobs() {
isFlushPending = false;

// 先执行 pre 类型的 job
// 所以这里执行的job 是在渲染前的
// 也就意味着执行这里的 job 的时候 页面还没有渲染
flushPreFlushCbs();

// 这里是执行 queueJob 的
// 比如 render 渲染就是属于这个类型的 job
let job;
while ((job = queue.shift())) {
if (job) {
job();
}
}
}

function flushPreFlushCbs() {
// 执行所有的 pre 类型的 job
for (let i = 0; i < activePreFlushCbs.length; i++) {
activePreFlushCbs[i]()
}
}

0 comments on commit 4f9e6b5

Please sign in to comment.