Make intervals easier and faster, add window.setInterval and clearInt… #600
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
…erval
When the browser microtask was added, zig-specific timeout functions were added to the loop. This was necessary for two reasons: 1 - The existing functions were JS specific
2 - We wanted a different reset counter for JS and Zig
Like we did in #577, the loop is now JS-agnostic. It gets a Zig callback, and the Zig callback can execute JS (or do whatever). An intrusive node, like with events, is used to minimize allocations.
Also, because the microtask was recently moved to the page, there is no longer a need for separate event counters. All timeouts are scoped to the page.
The new timeout callback can now be used to efficiently reschedule a task. This reuses the IO.completion and Context, avoiding 2 allocations. More importantly it makes the internal timer_id static for the lifetime of an "interval". This is important for window.setInterval, where the callback can itself clear the interval, which we would need to detect in the callback handler to avoid re-scheduling. With the stable timer_id, the existing cancel mechanism works as expected.
The loop no longer has a cbk_error. Callback code is expected to try/catch callbacks (or use callback.tryCall) and handle errors accordingly.