-
Notifications
You must be signed in to change notification settings - Fork 4.5k
iAPI: Refactor runtime initialization logic #71123
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
base: trunk
Are you sure you want to change the base?
Conversation
init(); | ||
|
||
// Defer hydration to the end of the task queue. | ||
postTask( hydrateRegions ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is mostly the same as before, with the setTimeout
function replaced by a new postTask
function, which adds the hydrateRegions
function to the end of the event loop.
Effectively, the result would be the same, as setTimeout
wouldn't be affected by any of the limitations that could potentially delay the callback execution.
const taskQueue: ( () => any )[] = []; | ||
const taskChannel = new MessageChannel(); | ||
// Drain one task per message | ||
taskChannel.port1.onmessage = () => { | ||
taskQueue.shift()?.(); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm reusing these for a scheduler
fallback. Not entirely confident this is the most suitable approach, though. 🤔
Using MessageChannel
could be considered more reliable than setTimeout()
to schedule tasks (the scheduler
polyfill from GoogleChromeLabs uses it when available), I feel that none of the delay issues setTimeout()
has would apply here, at least for the initialization case.
I've been testing different implementations here for As an example, these are the logs obtained for the
And these are for the implementation using
One interesting conclusion from these results is that, in Safari, the |
Another interesting finding is that we should avoid using top-level awaits in our code. When a module statically imports another module using a top-level Let's consider the init code that delays the hydration. Imagine we await // Defer hydration to the end of the task queue.
await postTask( hydrateRegions ); All |
What?
Closes #70870.
Revisits the initialization logic to clarify all the steps involved and at what moment they run.
For now, this PR serves more as an exploration to discuss possible implementations.
Why?
See #70870.
How?
Basically, the code changes are as follows:
index.ts
. There is no longer any initialization of isolated parts in internal modules, as was the case with the initialization ofstores
.init.ts
file has been renamedhydration.ts
, as it only involves the hydration part. Theinit()
function is now calledhydrateRegions()
.postTask
utility has been implemented to replace the use ofsetTimeout
when starting hydration. It usesscheduler.postTask
or a tiny fallback usingMessageChannel
.Testing Instructions
TBD