DEV Community

Cover image for Use isInputPending API for better scheduler
Yisar
Yisar

Posted on

Use isInputPending API for better scheduler

As you know, fre is an asynchronous rendering UI library that similar to react. The rendering process of components can be interrupted

Alt Text

The priority scheduling of react first enumerates and classifies events, which is too much code.

How can fre implement priority without enumerating event names?

isInputPending()

Inspired by react fiber, Facebook has written a proposal that when the browser is in the input pending state, you can interrupt your JS loop

while(navigator.scheduling.isInputPending()){
  setTimeout(reconcileWork) open a new tick
}
Enter fullscreen mode Exit fullscreen mode

We also can use this API to divide priorities, such as this:

dom.addEventListener('input', (e) => {
  let isHighPriority = navigator.scheduling.isInputPending()
  setState(count + 1, isHighPriority)
})
Enter fullscreen mode Exit fullscreen mode

It's done. We have a priority system with just one API.

https://github.com/yisar/fre/pull/226

At present, it has been shipped in fre, and we will do more based on it in the future.

It's worth mentioning that fre now has three priorities

  1. high priority => input pending => immediately
  2. common priority => time slicing => 16ms
  3. low priority => component update => It's been interrupted

If you are interested in asynchronous rendering or react internal implementation principles, welcome to fre's GitHub

https://github.com/yisar/fre

Redis image

Short-term memory for faster
AI agents 🤖💨

AI agents struggle with latency and context switching. Redis fixes it with a fast, in-memory layer for short-term context—plus native support for vectors and semi-structured data to keep real-time workflows on track.

Start building

Top comments (0)

Dev Diairies image

User Feedback & The Pivot That Saved The Project

🔥 Check out Episode 3 of Dev Diairies, following a successful Hackathon project turned startup.

Watch full video 🎥

👋 Kindness is contagious

Discover fresh viewpoints in this insightful post, supported by our vibrant DEV Community. Every developer’s experience matters—add your thoughts and help us grow together.

A simple “thank you” can uplift the author and spark new discussions—leave yours below!

On DEV, knowledge-sharing connects us and drives innovation. Found this useful? A quick note of appreciation makes a real impact.

Okay