Skip to content

Instantly share code, notes, and snippets.

@Tevinthuku
Last active August 14, 2019 08:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Tevinthuku/6544a30ca75a4f6175056e4d731764f4 to your computer and use it in GitHub Desktop.
Save Tevinthuku/6544a30ca75a4f6175056e4d731764f4 to your computer and use it in GitHub Desktop.
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