import { __awaiter } from "tslib"; class Queue { constructor() { this.status = "waiting"; } append(item, autoRun = true) { const node = new Node(item); if (!this.current || !this.end) { this.current = this.end = node; } else { this.end.setNext(node); this.end = node; } if (autoRun) { this.startRun(); } } startRun() { if (this.status === "waiting") { this.run(); } } run() { return __awaiter(this, void 0, void 0, function* () { if (this.current) { this.status = "running"; const current = this.current; const self = current.getSelf(); this.current = this.current.getNext(); const res = yield self.run(); current.destroy(); if (res === false) { this.status = "waiting"; } else { this.run(); } } else { this.end = undefined; this.status = "waiting"; } }); } } class Node { constructor(self) { this.self = self; } getSelf() { return this.self; } setPrev(prev) { this.prev = prev; if (prev) { prev.next = this; } } setNext(next) { this.next = next; if (next) { next.prev = this; } } getPrev() { return this.prev; } getNext() { return this.next; } destroy() { if (this.prev) { this.prev.setNext(undefined); } if (this.next) { this.next.setPrev(undefined); } this.prev = undefined; this.next = undefined; this.self = undefined; } } export { Queue };