S
SolidJS7mo ago
alloyed

implementation detail: enableScheduling uses MessageChannel API?

I can put in the work to polyfill it, but why is it needed to begin with? does the scheduler do any wacky iframe/webworker stuff under the hood?
No description
7 Replies
alloyed
alloyed7mo ago
another web-specific API:
alloyed
alloyed7mo ago
No description
alloyed
alloyed7mo ago
I'll just drop in an undefined variable here
lxsmnsyc
lxsmnsyc7mo ago
MessageChannel has a very specific timing (on its event) that the scheduler needs. where are you running this, may I ask?
alloyed
alloyed7mo ago
Proprietary embedded context! It's not react native, but the broad strokes are super similar so any web-specific API i need to polyfill, but we don't have things like iframes to worry about
lxsmnsyc
lxsmnsyc7mo ago
I see, yes it's a bit tricky on the scheduler's side
alloyed
alloyed7mo ago
ok notes for future spelunkers. MessageChannel is used in the body of this function and nowhere else: https://github.com/solidjs/solid/blob/460068202bbd7a56a14664ac14fa7efb900d8194/packages/solid/src/reactive/scheduler.ts#L29 The solidjs tests replace it with a do-nearly-nothing polyfill that using setTimeout instead: https://github.com/solidjs/solid/blob/460068202bbd7a56a14664ac14fa7efb900d8194/packages/solid/test/MessageChannel.ts#L1C1-L9C3 The purpose of this piece of code is to schedule work to be done at a future time. Using setTimeout means it'll happen in the next frame's update loop, I don't know exactly what MessageChannel does but it's probably something similar. queueMicrotask() would be a bad idea, it'd just do everything in a single frame, and requestIdleCallback() would probably result in a very laggy experience. The scheduler uses 'navigator.scheduling.isInputPending' to detect if it should yield to existing user input: https://developer.mozilla.org/en-US/docs/Web/API/Scheduling/isInputPending https://github.com/solidjs/solid/blob/460068202bbd7a56a14664ac14fa7efb900d8194/packages/solid/src/reactive/scheduler.ts#L68 if it doesn't detect any user input, it will attempt to run for as long as possible, up to the maxYieldInterval (300ms). If you don't implement isInputPending, it will assume that there's always input pending and yield after the the yieldInterval, 5ms. All of this is user-customizable by providing a custom implementation of requestCallback to enableScheduling.