Skip to content

Commit 7354a1b

Browse files
authored
worker idle support idleForceTimeThreshold (#2189)
1 parent 2dfb108 commit 7354a1b

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/GlobalConfig.js

+2
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ const GlobalConfig = {
77
idleLog: false,
88
//idle 时间阈值
99
idleTimeRemaining: 8,
10+
//idle 申请不到idle时,强制执行时间阈值
11+
idleForceTimeThreshold: 100,
1012
//idle 超时阈值
1113
idleTimeout: 1000,
1214
//worker 数量

src/core/MicroTask.js

+13-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import PromisePolyfill from './Promise';
22
import { requestAnimFrame } from './util';
3-
import { isFunction, isNil, isNumber } from './util/common';
3+
import { isFunction, isNil, isNumber, now } from './util/common';
44
import { getGlobalWorkerPool } from './worker/WorkerPool';
55
import Browser from './Browser';
66
import GlobalConfig from '../GlobalConfig';
@@ -87,8 +87,9 @@ function loop() {
8787
broadcastIdleMessage = !broadcastIdleMessage;
8888
}
8989

90+
let loopFrameTime = now();
9091
function frameLoop(deadline) {
91-
const { idleTimeRemaining, idleLog, idleTimeout } = GlobalConfig;
92+
const { idleTimeRemaining, idleLog, idleTimeout, idleForceTimeThreshold } = GlobalConfig;
9293
if (Browser.requestIdleCallback) {
9394
if (deadline && deadline.timeRemaining) {
9495
const t = deadline.timeRemaining();
@@ -97,8 +98,16 @@ function frameLoop(deadline) {
9798
console.error('idle timeout in', idleTimeout);
9899
}
99100
loop();
100-
} else if (t <= idleTimeRemaining && idleLog) {
101-
console.warn('currrent page is busy,the timeRemaining is', t);
101+
loopFrameTime = now();
102+
} else {
103+
const time = now();
104+
if (time - loopFrameTime > idleForceTimeThreshold) {
105+
loop();
106+
loopFrameTime = now();
107+
}
108+
if (t <= idleTimeRemaining && idleLog) {
109+
console.warn('currrent page is busy,the timeRemaining is', t);
110+
}
102111
}
103112
}
104113
requestIdleCallback(frameLoop, { timeout: idleTimeout });

0 commit comments

Comments
 (0)