Skip to content

Commit

Permalink
worker idle support idleForceTimeThreshold (#2189)
Browse files Browse the repository at this point in the history
  • Loading branch information
deyihu authored Jan 15, 2024
1 parent 2dfb108 commit 7354a1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 4 deletions.
2 changes: 2 additions & 0 deletions src/GlobalConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ const GlobalConfig = {
idleLog: false,
//idle 时间阈值
idleTimeRemaining: 8,
//idle 申请不到idle时,强制执行时间阈值
idleForceTimeThreshold: 100,
//idle 超时阈值
idleTimeout: 1000,
//worker 数量
Expand Down
17 changes: 13 additions & 4 deletions src/core/MicroTask.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import PromisePolyfill from './Promise';
import { requestAnimFrame } from './util';
import { isFunction, isNil, isNumber } from './util/common';
import { isFunction, isNil, isNumber, now } from './util/common';
import { getGlobalWorkerPool } from './worker/WorkerPool';
import Browser from './Browser';
import GlobalConfig from '../GlobalConfig';
Expand Down Expand Up @@ -87,8 +87,9 @@ function loop() {
broadcastIdleMessage = !broadcastIdleMessage;
}

let loopFrameTime = now();
function frameLoop(deadline) {
const { idleTimeRemaining, idleLog, idleTimeout } = GlobalConfig;
const { idleTimeRemaining, idleLog, idleTimeout, idleForceTimeThreshold } = GlobalConfig;
if (Browser.requestIdleCallback) {
if (deadline && deadline.timeRemaining) {
const t = deadline.timeRemaining();
Expand All @@ -97,8 +98,16 @@ function frameLoop(deadline) {
console.error('idle timeout in', idleTimeout);
}
loop();
} else if (t <= idleTimeRemaining && idleLog) {
console.warn('currrent page is busy,the timeRemaining is', t);
loopFrameTime = now();
} else {
const time = now();
if (time - loopFrameTime > idleForceTimeThreshold) {
loop();
loopFrameTime = now();
}
if (t <= idleTimeRemaining && idleLog) {
console.warn('currrent page is busy,the timeRemaining is', t);
}
}
}
requestIdleCallback(frameLoop, { timeout: idleTimeout });
Expand Down

0 comments on commit 7354a1b

Please sign in to comment.