12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- import { __awaiter } from "tslib";
- class Queue {
- constructor() {
- this.status = "waiting";
- }
- append(item, autoRun = false) {
- 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();
- this._promise = new Promise((resolve) => {
- this._myResolve = resolve;
- });
- }
- return this._promise;
- }
- 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";
- this._myResolve("stopped");
- this._promise = undefined;
- this._myResolve = undefined;
- }
- else {
- this.run();
- }
- }
- else {
- this.end = undefined;
- this.status = "waiting";
- this._myResolve("finished");
- this._promise = undefined;
- this._myResolve = undefined;
- }
- });
- }
- }
- 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 };
|