Last active
August 14, 2019 08:23
-
-
Save Tevinthuku/6544a30ca75a4f6175056e4d731764f4 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { updateDomProperties } from "./dom-utils"; | |
import { TEXT_ELEMENT } from "./element"; | |
const ENOUGH_TIME = 1; // we set ours to 1 millisecond. | |
let workQueue = []; // there is no work initially | |
let nextUnitOfWork = null; // the nextUnitOfWork is null on initial render. | |
// the schedule function heere can stand | |
// for the scheduleUpdate or the | |
// call to render | |
// both those calls update the workQueue with a new task. | |
function schedule(task) { | |
// add the task to the workqueue. It will be worked on later. | |
workQueue.push(task); | |
// request to know when the browser will be pre-occupied. | |
// if the browser doesn't support requestIdleCallback | |
// react will pollyfill the function but for simplicities sake | |
// ill assume your running this on an ever-green browser. | |
requestIdleCallback(performWork); | |
} | |
function performWork(deadline) { | |
loopThroughWork(deadline) | |
if (nextUnitOfWork || workQueue.length > 0) { | |
// if theres more work to be done. get to know when the browser will be occupied | |
// and check if we can perform some work with the timing provided. | |
requestIdleCallback(performWork); | |
} | |
} | |
function loopThroughWork(deadline) { | |
while (nextUnitOfWork && deadline.timeRemaining() > ENOUGH_TIME) { | |
/** | |
* perform unitofwork on a fiber if there's enough time to spare | |
* from the browser's end. | |
*/ | |
nextUnitOfWork = performUnitOfWork(nextUnitOfWork); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment