UNPKG

@code-yeongyu/senpi

Version:

Coding agent CLI with read, bash, edit, write tools and session management

31 lines • 1.14 kB
/** * Cooperative time slicing for work that walks a whole transcript on a shared event loop. * Work checks `due` between items and awaits `yield()` once its slice is spent, so sockets, * timers and other sessions run between slices (senpi#3518). */ export const DEFAULT_SLICE_BUDGET_MS = 8; export class EventLoopSlicer { constructor(options = {}) { this.budgetMs = options.budgetMs ?? DEFAULT_SLICE_BUDGET_MS; this.clock = options.clock ?? (() => performance.now()); this.sliceStart = this.clock(); } get due() { return this.clock() - this.sliceStart >= this.budgetMs; } /** `setImmediate` rather than a microtask: only a macrotask lets pending I/O callbacks run. */ async yield() { await new Promise((resolve) => setImmediate(resolve)); this.sliceStart = this.clock(); } } export async function mapInSlices(items, transform, slicer) { const mapped = []; for (const item of items) { mapped.push(transform(item)); if (slicer.due) await slicer.yield(); } return mapped; } //# sourceMappingURL=event-loop-slicer.js.map