Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[schedule] Nested callbacks and continuations #13595

Closed
wants to merge 2 commits into from

Conversation

acdlite
Copy link
Collaborator

@acdlite acdlite commented Sep 7, 2018

Depends on #13582

Nested callbacks and continuations …
A "nested callback" is one that is scheduled during the execution of
a parent callback. It should default to the timeout of the parent.

A "continuation" is a callback that is scheduled when yielding
execution. It's scheduled by returning a callback from the parent. It
should have the same timeout as the parent, and because it's a
continuation, it should be inserted into the queue before callbacks of
equal priority (not after, as callbacks are usually scheduled).

Example of a continuation:

function performWork(deadline) {
  while (tasks.length > 0) {
    const task = tasks.shift();
    doTask(task);
    if (
      tasks.length > 0 &&
      !deadline.didTimeout &&
      deadline.timeRemaining() <= 0
    ) {
      // Ran out of time. Yield and
      // continue later.
      return performWork;
    }
  }
}

scheduleWork(performWork);

@pull-bot
Copy link

pull-bot commented Sep 7, 2018

React: size: 🔺+10.3%, gzip: 🔺+7.3%

Details of bundled changes.

Comparing: b87aabd...9da68b3

react

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
react.development.js +0.6% -0.2% 83.46 KB 83.97 KB 22.63 KB 22.59 KB UMD_DEV
react.production.min.js 🔺+10.3% 🔺+7.3% 9.54 KB 10.52 KB 3.96 KB 4.24 KB UMD_PROD

schedule

File Filesize Diff Gzip Diff Prev Size Current Size Prev Gzip Current Gzip ENV
schedule.development.js n/a n/a 0 B 19.17 KB 0 B 5.74 KB UMD_DEV
schedule.production.min.js n/a n/a 0 B 3.16 KB 0 B 1.53 KB UMD_PROD
schedule.development.js +2.6% -1.7% 15.34 KB 15.74 KB 4.61 KB 4.53 KB NODE_DEV
schedule.production.min.js 🔺+30.8% 🔺+24.0% 2.78 KB 3.63 KB 1.21 KB 1.51 KB NODE_PROD
Schedule-dev.js +2.5% -1.9% 15.58 KB 15.96 KB 4.66 KB 4.57 KB FB_WWW_DEV
Schedule-prod.js 🔺+22.3% 🔺+20.6% 7.72 KB 9.44 KB 1.91 KB 2.3 KB FB_WWW_PROD

Generated by 🚫 dangerJS

Once the API stabilizes, we will move Schedule this into a separate
repo. To promote adoption, especially by projects outside the React
ecosystem, we'll remove all React-isms from the source and keep it as
simple as possible:

- No build step.
- No static types.
- Everything is in a single file.

If we end up needing to support multiple targets, like CommonJS and ESM,
we can still avoid a build step by maintaining two copies of the same
file, but with different exports.

This commit also refactors the implementation to split out the DOM-
specific parts (essentially a requestIdleCallback polyfill). Aside from
the architectural benefits, this also makes it possible to write host-
agnostic tests. If/when we publish a version of Schedule that targets
other environments, like React Native, we can run these same tests
across all implementations.
A "nested callback" is one that is scheduled during the execution of
a parent callback. It should default to the timeout of the parent.

A "continuation" is a callback that is scheduled when yielding
execution. It's scheduled by returning a callback from the parent. It
should have the same timeout as the parent, and because it's a
continuation, it should be inserted into the queue before callbacks of
equal priority (not after, as callbacks are usually scheduled).

Example of a continuation:

  function performWork(deadline) {
    while (tasks.length > 0) {
      const task = tasks.shift();
      doTask(task);
      if (
        tasks.length > 0 &&
        !deadline.didTimeout &&
        deadline.timeRemaining() <= 0
      ) {
        // Ran out of time. Yield and
        // continue later.
        return performWork;
      }
    }
  }

  scheduleWork(performWork);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants