systemquery/src/lib/async/ItemQueue.mjs

49 lines
770 B
JavaScript

"use strict";
import { EventEmitter, once } from 'events';
class ItemQueue extends EventEmitter {
constructor() {
super();
this.items = [];
}
__push_single(item) {
this.items.push(item);
this.emit("push", item);
}
push(...items) {
}
async pop(time_limit = 0) {
if(this.items.length === 0) {
}
this.pop()
}
wait_for_item(time_limit_ms = 0) {
const ac = new AbortController();
let timeout = null;
if(time_limit_ms > 0) {
timeout = setTimeout(() => {
ac.abort();
}, time_limit_ms);
}
let item = await once(this, "push", { signal: ac.signal });
if(timeout !== null)
clearTimeout(timeout);
this.items.splice(this.items.indexOf(item));
return item;
}
wait_empty() {
}
}
export default ItemQueue;