@cloud-copilot/iam-lens
Version:
Visibility in IAM in and across AWS accounts
61 lines • 1.99 kB
JavaScript
import { RingQueue } from './RingQueue.js';
export class StreamingWorkQueue {
constructor(options = {}) {
this.ringCapacity = 1024;
this.enqueueIndex = 0;
this.dequeueIndex = 0;
this.notifyScheduled = false;
if (options.ringCapacity) {
this.ringCapacity = options.ringCapacity;
}
this.rings = [new 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(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?.();
});
}
}
//# sourceMappingURL=StreamingWorkQueue.js.map