Match-id-1f58ff819dfbd01d0b80fd9bfaa198bcb73f0b50

This commit is contained in:
* 2023-01-12 17:48:28 +08:00
parent e26d0e0fd7
commit cadaabfd3b
2 changed files with 25 additions and 15 deletions

View File

@ -19,11 +19,9 @@
let isMessageLoopRunning = false; let isMessageLoopRunning = false;
let browserCallback = null; let browserCallback = null;
const { port1, port2 } = new MessageChannel(); let port1 = null;
let port2 = null;
export function isOverTime() { let isTestRuntime = false;
return false;
}
// 1、设置deadline2、回调TaskExecutor传过来的browserCallback // 1、设置deadline2、回调TaskExecutor传过来的browserCallback
const callRenderTasks = () => { const callRenderTasks = () => {
@ -41,21 +39,38 @@ const callRenderTasks = () => {
browserCallback = null; browserCallback = null;
} else { } else {
// 还有task继续调用 // 还有task继续调用
port2.postMessage(null); asyncCall();
} }
} catch (error) { } catch (error) {
port2.postMessage(null); asyncCall();
throw error; throw error;
} }
}; };
port1.onmessage = callRenderTasks; if (typeof MessageChannel === 'function') {
const mc = new MessageChannel();
port1 = mc.port1;
port1.onmessage = callRenderTasks;
port2 = mc.port2;
} else {
// 测试环境没有 MessageChannel
isTestRuntime = true;
}
function asyncCall() {
if (isTestRuntime) {
setTimeout(callRenderTasks, 0);
} else {
port2.postMessage(null);
}
}
export function requestBrowserCallback(callback) { export function requestBrowserCallback(callback) {
browserCallback = callback; browserCallback = callback;
if (!isMessageLoopRunning) { if (!isMessageLoopRunning) {
isMessageLoopRunning = true; isMessageLoopRunning = true;
port2.postMessage(null); asyncCall();
} }
} }

View File

@ -18,7 +18,7 @@
*/ */
import { Node } from '../taskExecutor/TaskQueue'; import { Node } from '../taskExecutor/TaskQueue';
import { requestBrowserCallback, isOverTime } from './BrowserAsync'; import { requestBrowserCallback } from './BrowserAsync';
import { add, shift, first, remove } from './TaskQueue'; import { add, shift, first, remove } from './TaskQueue';
@ -44,11 +44,6 @@ function callTasks() {
// 循环执行task // 循环执行task
while (task !== null) { while (task !== null) {
if (isOverTime()) {
// 超过了deadline
break;
}
const callback = task.callback; const callback = task.callback;
if (callback !== null) { if (callback !== null) {
task.callback = null; task.callback = null;