Skip to content

Commit 7fb44b7

Browse files
committed
Swap in postTask for MessageChannel
1 parent fb8c581 commit 7fb44b7

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

packages/scheduler/src/forks/SchedulerPostTaskOnly.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ function unstable_getCurrentPriorityLevel() {
429429
return currentPriorityLevel;
430430
}
431431

432-
let isMessageLoopRunning = false;
432+
let isTaskLoopRunning = false;
433433
let scheduledHostCallback = null;
434434
let taskTimeoutID = -1;
435435

@@ -522,38 +522,39 @@ const performWorkUntilDeadline = () => {
522522
try {
523523
const hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime);
524524
if (!hasMoreWork) {
525-
isMessageLoopRunning = false;
525+
isTaskLoopRunning = false;
526526
scheduledHostCallback = null;
527527
} else {
528528
// If there's more work, schedule the next message event at the end
529529
// of the preceding one.
530-
port.postMessage(null);
530+
postTask(performWorkUntilDeadline);
531531
}
532532
} catch (error) {
533533
// If a scheduler task throws, exit the current browser task so the
534534
// error can be observed.
535-
port.postMessage(null);
535+
postTask(performWorkUntilDeadline);
536536
throw error;
537537
}
538538
} else {
539-
isMessageLoopRunning = false;
539+
isTaskLoopRunning = false;
540540
}
541541
// Yielding to the browser will give it a chance to paint, so we can
542542
// reset this.
543543
needsPaint = false;
544544
};
545545

546-
const channel = new MessageChannel();
547-
const port = channel.port2;
548-
channel.port1.onmessage = performWorkUntilDeadline;
546+
function postTask(callback) {
547+
// Use experimental Chrome Scheduler postTask API.
548+
global.scheduler.postTask(callback);
549+
}
549550

550551
function requestHostCallback(callback) {
551-
scheduledHostCallback = callback;
552-
if (!isMessageLoopRunning) {
553-
isMessageLoopRunning = true;
554-
port.postMessage(null);
555-
}
556-
}
552+
scheduledHostCallback = callback;
553+
if (!isTaskLoopRunning) {
554+
isTaskLoopRunning = true;
555+
postTask(performWorkUntilDeadline);
556+
}
557+
}
557558

558559
function requestHostTimeout(callback, ms) {
559560
taskTimeoutID = setTimeout(() => {

0 commit comments

Comments
 (0)