@code-yeongyu/senpi
Version:
Coding agent CLI with read, bash, edit, write tools and session management
45 lines • 1.41 kB
JavaScript
/**
* One extension module per event-loop turn (senpi#6199). Bun fetches the whole static graph of an `import()` inside
* one microtask drain, so a synchronous `onLoad` holds the loop for every module of the graph: a host compiling an
* extension (its boot load, a warm, an open) answered no socket until the last one. During an import each load waits
* for its own turn instead; a synchronous `require()` cannot take a promise, so its loads still run at once.
*/
const queued = [];
let draining = false;
let synchronousDepth = 0;
function drainOne() {
queued.shift()?.();
if (queued.length > 0)
setImmediate(drainOne);
else
draining = false;
}
export function loadInTurn(load) {
if (synchronousDepth > 0)
return load();
return new Promise((resolve, reject) => {
queued.push(() => {
try {
resolve(load());
}
catch (error) {
reject(error);
}
});
if (draining)
return;
draining = true;
setImmediate(drainOne);
});
}
/** Runs `require` so every module it loads is served synchronously, as a CommonJS require needs. */
export function loadSynchronously(require) {
synchronousDepth++;
try {
return require();
}
finally {
synchronousDepth--;
}
}
//# sourceMappingURL=bun-extension-load-turns.js.map