Add ESM mainloop module
mainloop
module
Extension of !732 (merged). This replaces imports.mainloop
in modules as imports.mainloop.run()
will block Promises.
The second commit enables an ESM mainloop
module which exposes run()
and quit()
. It additionally exposes idle()
as the Timers API does not cover the idle_add
function (setTimeout
and setInterval
cover other use cases)
This example below demonstrates how run
and quit
can be used to start and stop the mainloop repeatedly.
import {run, quit} from 'mainloop';
setTimeout(() => {
console.log('test 2');
quit();
}, 1000);
console.log('test 1');
await run();
console.log('test 3');
setTimeout(() => {
console.log('test 5');
quit();
}, 1000);
console.log('test 4');
await run();
console.log('test 6');
Another example with idle
import { run, quit, idle } from 'mainloop';
let i = 0;
setInterval(() => {
Promise.resolve().then(() => {
i++;
console.log(`Count: ${i}`);
if (i > 12) quit();
});
}, 100);
idle(() => console.log('idle!'));
await run();
Fixes #468 (closed)
Edited by Evan Welsh