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

Scheduler microtasks support #304

Merged
merged 3 commits into from
Sep 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion demo/src/benchmark.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,4 @@ const Main = () => {
)
}

render(<Main />, document.body, { sync: true })
render(<Main />, document.body)
5 changes: 2 additions & 3 deletions demo/src/concurrent.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// import { h, render, useState, useEffect } from 'fre'
// import { h, render } from 'preact'
// import {useState, useEffect } from 'preact/hooks'
import { render, useState, useEffect,h, createRoot } from '../../src/index'
import { render, useState, useEffect, h } from '../../src/index'

const UPDATE_EVERY = 500
const BLOCK_FOR = 5
Expand Down Expand Up @@ -297,5 +297,4 @@ ul.solarsystem li.jupiter {
</div>`

document.body.insertBefore(div, app)
// render(<App />, app)
createRoot(document.getElementById('app')).render(<App />)
render(<App />, app)
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fre",
"version": "2.3.4",
"version": "2.4.0",
"type": "module",
"main": "dist/fre.js",
"unpkg": "dist/fre.umd.js",
Expand Down
26 changes: 19 additions & 7 deletions src/schedule.ts
Original file line number Diff line number Diff line change
@@ -1,30 +1,42 @@
import { IFiber, ITask, ITaskCallback } from "./type"
import { options } from "./reconcile"

const queue: ITask[] = []
const threshold: number = 1000 / 60
const transitions = []
let deadline: number = 0

export const startTransition = (cb) => {
transitions.push(cb) && postMessage()
transitions.push(cb) && runTransition()
}

export const schedule = (callback: any): void => {
queue.push({ callback } as any)
startTransition(flush)
}

const postMessage = (() => {
const transitionRunnerFactory = (useMicrotasks: boolean) => {
const cb = () => transitions.splice(0, 1).forEach((c) => c())

if (useMicrotasks) {
if (typeof queueMicrotask !== "undefined") {
return () => queueMicrotask(cb)
}
const resolvedPromise = Promise.resolve()
return () => resolvedPromise.then(cb)
}

if (typeof MessageChannel !== "undefined") {
const { port1, port2 } = new MessageChannel()
port1.onmessage = cb
return () => port2.postMessage(null)
}
return () => setTimeout(cb)
})()
}

const runTransitionAsTask = transitionRunnerFactory(false)
const runTransitionAsMicroTask = transitionRunnerFactory(true)

let runTransition = runTransitionAsMicroTask

const flush = (): void => {
deadline = getTime() + threshold
Expand All @@ -44,10 +56,10 @@ const flush = (): void => {
}

export const shouldYield = (): boolean => {
if (options.sync) return false
return (
const isInputPending =
(navigator as any)?.scheduling?.isInputPending() || getTime() >= deadline
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've run some benchmarks and it seems that chrome is actually doing worse with isInputPending:

Firefox create 1k rows (20x): ~573ms
Chrome (without isInputPending) create 1k rows (20x) : ~642ms
Chrome (with isInputPending) create 1k rows (20x) : ~730ms

It would be nice if someone could confirm this as well;
Another case with it is that isInputPending function call creates ugly code that all other browsers needs to handle:

"use strict";
var _a, _b;
const isInputPending = ((_b = (_a = navigator) === null || _a === void 0 ? void 0 : _a.scheduling) === null || _b === void 0 ? void 0 : _b.isInputPending()) || getTime() >= deadline;

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, there is no need to isInputPending, and the calculation using time slice is enough. You can delete it.
https://github.com/WICG/scheduling-apis
This is a new set of scheduling APIs, but it is not yet mature.
We can remove them first and then add it when the time is ripe.

)
runTransition = isInputPending ? runTransitionAsTask : runTransitionAsMicroTask
return isInputPending
}

export const getTime = () => performance.now()
Expand Down