Skip to content

Commit

Permalink
Swap in postTask for MessageChannel
Browse files Browse the repository at this point in the history
  • Loading branch information
rickhanlonii committed Nov 9, 2020
1 parent fb8c581 commit 7fb44b7
Showing 1 changed file with 15 additions and 14 deletions.
29 changes: 15 additions & 14 deletions packages/scheduler/src/forks/SchedulerPostTaskOnly.js
Original file line number Diff line number Diff line change
Expand Up @@ -429,7 +429,7 @@ function unstable_getCurrentPriorityLevel() {
return currentPriorityLevel;
}

let isMessageLoopRunning = false;
let isTaskLoopRunning = false;
let scheduledHostCallback = null;
let taskTimeoutID = -1;

Expand Down Expand Up @@ -522,38 +522,39 @@ const performWorkUntilDeadline = () => {
try {
const hasMoreWork = scheduledHostCallback(hasTimeRemaining, currentTime);
if (!hasMoreWork) {
isMessageLoopRunning = false;
isTaskLoopRunning = false;
scheduledHostCallback = null;
} else {
// If there's more work, schedule the next message event at the end
// of the preceding one.
port.postMessage(null);
postTask(performWorkUntilDeadline);
}
} catch (error) {
// If a scheduler task throws, exit the current browser task so the
// error can be observed.
port.postMessage(null);
postTask(performWorkUntilDeadline);
throw error;
}
} else {
isMessageLoopRunning = false;
isTaskLoopRunning = false;
}
// Yielding to the browser will give it a chance to paint, so we can
// reset this.
needsPaint = false;
};

const channel = new MessageChannel();
const port = channel.port2;
channel.port1.onmessage = performWorkUntilDeadline;
function postTask(callback) {
// Use experimental Chrome Scheduler postTask API.
global.scheduler.postTask(callback);
}

function requestHostCallback(callback) {
scheduledHostCallback = callback;
if (!isMessageLoopRunning) {
isMessageLoopRunning = true;
port.postMessage(null);
}
}
scheduledHostCallback = callback;
if (!isTaskLoopRunning) {
isTaskLoopRunning = true;
postTask(performWorkUntilDeadline);
}
}

function requestHostTimeout(callback, ms) {
taskTimeoutID = setTimeout(() => {
Expand Down

0 comments on commit 7fb44b7

Please sign in to comment.