UNPKG

@cloud-copilot/iam-lens

Version:

Visibility in IAM in and across AWS accounts

67 lines 2.18 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.StreamingWorkQueue = void 0; const RingQueue_js_1 = require("./RingQueue.js"); class StreamingWorkQueue { ringCapacity = 1024; rings; enqueueIndex = 0; dequeueIndex = 0; onWorkAvailable; notifyScheduled = false; constructor(options = {}) { if (options.ringCapacity) { this.ringCapacity = options.ringCapacity; } this.rings = [new RingQueue_js_1.RingQueue(this.ringCapacity)]; } setWorkAvailableCallback(callback) { this.onWorkAvailable = callback; } enqueue(item) { let attempts = 0; while (attempts < this.rings.length) { const ring = this.rings[this.enqueueIndex]; if (ring.enqueue(item)) { this.scheduleNotify(); return; } this.enqueueIndex = (this.enqueueIndex + 1) % this.rings.length; attempts++; } // All rings full, create a new one const newRing = new RingQueue_js_1.RingQueue(this.ringCapacity); newRing.enqueue(item); this.rings.push(newRing); this.enqueueIndex = this.rings.length - 1; this.scheduleNotify(); } dequeue() { let attempts = 0; while (attempts < this.rings.length) { const ring = this.rings[this.dequeueIndex]; const item = ring.dequeue(); if (item !== undefined) return item; this.dequeueIndex = (this.dequeueIndex + 1) % this.rings.length; attempts++; } return undefined; } length() { return this.rings.reduce((sum, ring) => sum + ring.length(), 0); } scheduleNotify() { if (this.notifyScheduled || !this.onWorkAvailable) { return; } this.notifyScheduled = true; // Use a microtask to debounce notifications queueMicrotask(() => { this.notifyScheduled = false; this.onWorkAvailable?.(); }); } } exports.StreamingWorkQueue = StreamingWorkQueue; //# sourceMappingURL=StreamingWorkQueue.js.map