Skip to content

event-loop-turn

Yields one macrotask by posting a MessageChannel message to itself, and debounces on the same turn.

A zero-length timer is the usual way to give the event loop a turn, but Chromium throttles chained timers in a hidden window, down to about one wake-up a minute, and requestAnimationFrame does not fire there at all. A message posted to oneself is not throttled, so a long job paced by these helpers keeps its speed while the window is behind another one. MessageChannel exists in every browser renderer, in Obsidian on desktop and mobile, and in Node, unlike the Node-only setImmediate.

The channel is opened on the first turn and closed as soon as nothing waits: an open port with a handler keeps its bundle alive after a plugin unloads, and keeps a Node process from exiting.

Ported from src/turn.ts of the npm package @insurgently/obsidian-ui 0.4.0, under its MIT license:

MIT License

Copyright (c) 2026 Insurgently

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Function Description
coalesce Returns a function that calls callback once on the next event loop turn, however many times it is called before then. It is a debounce that, unlike one built on a timer, is not slowed while the window is hidden.
isEventLoopTurnPending Checks whether the channel behind yieldToEventLoop is open. It is closed once every pending turn has run, so a test can assert that nothing is left to keep a process or an unloaded plugin’s bundle alive.
yieldToEventLoop Gives the event loop one turn (one macrotask), by posting a MessageChannel message to itself. Unlike a zero-length timer or requestAnimationFrame, it is not throttled in a hidden window, and unlike setImmediate it works outside Node.