# scheduler-polyfill

> Polyfill of self.scheduler API

Latest version **1.3.0** (published 2024-10-22) · Apache-2.0 license · 0 weekly downloads

## Install

```sh
npm install scheduler-polyfill
pnpm add scheduler-polyfill
yarn add scheduler-polyfill
bun add scheduler-polyfill
```

## Health

**Score 50/100 (C)** — status: stable.

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads.

Negative: stale.

## Facts

| | |
|---|---|
| Version | 1.3.0 |
| Published | 2024-10-22 |
| First published | 2021-11-29 |
| Weekly downloads | 0 |
| License | Apache-2.0 |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 70.1 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 148 |
| Author | The Chromium Authors |
| Maintainers | google-wombot |
| Keywords | scheduler, scheduling, postTask, TaskController, TaskSignal |

## Links

- npm: https://www.npmjs.com/package/scheduler-polyfill
- Repository: https://github.com/GoogleChromeLabs/scheduler-polyfill
- Issues: https://github.com/GoogleChromeLabs/scheduler-polyfill/issues
- npm.io page: https://npm.io/package/scheduler-polyfill

## Recent versions

- 1.3.0 (latest) — 2024-10-22
- 1.3.0-beta.0 (next) — 2023-08-24
- 1.2.1 — 2023-08-24
- 1.0.3 — 2023-08-24
- 1.2.0 — 2023-08-24
- 1.0.2 — 2023-08-22
- 1.0.1 — 2023-06-23
- 1.0.0 — 2021-11-29

## README

# Scheduler Polyfill

This is a polyfill for the [Prioritized Task Scheduling
API](https://wicg.github.io/scheduling-apis/). Documentation can be found on
[MDN](https://developer.mozilla.org/en-US/docs/Web/API/Scheduler).

The polyfill includes implementations of `Scheduler`, exposed through
`self.scheduler`, as well as `TaskController` and `TaskPriorityChangeEvent`
classes.

## `scheduler.postTask()`

The implementation uses a combination of `setTimeout`, `MessageChannel`, and
`requestIdleCallback` to implement task scheduling, falling back to `setTimeout`
when other APIs are not available.

The polyfill, like the native implementation, runs `scheduler` tasks in
descending priority order (`'user-blocking'` > `'user-visible'` >
`'background'`). But there are some differences in the relative order of
non-`scheduler` tasks:

 - `"background"` tasks are scheduled using `requestIdleCallback` on browsers
   that support it, which provides similar scheduling as `scheduler`. For
   browsers that don't support it, these tasks do not have low/idle event loop
   priority.

 - `"user-blocking"` tasks have the same event loop scheduling prioritization as
   `"user-visible"` (similar to `setTimeout()`), meaning these tasks do not have
   a higher event loop priority.

## `scheduler.yield()`

The polyfill does not support [priority or signal
inheritance](https://developer.mozilla.org/en-US/docs/Web/API/Scheduler/yield#inheriting_task_priorities),
so all continuations are scheduled with `"user-visible"` continuation priority.
The scheduling behavior of this depends on whether the browser supports
`scheduler.postTask()` (i.e. older Chrome versions):
  * For browsers that support `scheduler.postTask()`, `scheduler.yield()` is
    polyfilled with `"user-blocking"` `scheduler.postTask()` tasks. This means
    they typically have a higher event loop priority than other tasks
    (consistent with `yield()`), but they can be interleaved with other
    `"user-blocking"` tasks.

 * On browsers that don't support `scheduler.postTask()`, the same event loop
   prioritization as the `postTask()` polyfill applies (see above), but
   continuations run between `"user-blocking"` and `"user-visible"` tasks.

## Requirements

A browser that supports ES6 is required for this polyfill.

## Usage

### Include via npm and a bundler

```console
npm install scheduler-polyfill
```

Import to populate the task-scheduling global variables, if not already
available in the executing browser:

```js
import 'scheduler-polyfill';
```

### Include via unpkg

```html
<script src="https://unpkg.com/scheduler-polyfill"></script>
```

### Building from source

```console
git clone https://github.com/GoogleChromeLabs/scheduler-polyfill
cd scheduler-polyfill
npm i
npm test        # Tests should pass
npm run build   # Outputs minified polyfill to dist/
```

```html
<script src="/path_to_polyfill/scheduler-polyfill.js"></script>
```

## License

[Apache 2.0](LICENSE)

---
_Source: https://npm.io/package/scheduler-polyfill · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
