123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- class Queue<T extends { run: () => Promise<any> }> {
- private current?: Node<T>;
- private end?: Node<T>;
- private status: "waiting" | "running" = "waiting";
- private _myResolve?: (v: "finished" | "stopped") => void;
- private _promise?: Promise<"finished" | "stopped">;
- append(item: T, 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<"finished" | "stopped">((resolve) => {
- this._myResolve = resolve;
- });
- }
- return this._promise;
- }
- async run() {
- if (this.current) {
- this.status = "running";
- const current = this.current;
- const self = current.getSelf();
- this.current = this.current.getNext();
- const res = await 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<T = any> {
- private prev?: Node<T>;
- private next?: Node<T>;
- private self: T;
- constructor(self: T) {
- this.self = self;
- }
- getSelf() {
- return this.self;
- }
- setPrev(prev?: Node<T>) {
- this.prev = prev;
- if (prev) {
- prev.next = this;
- }
- }
- setNext(next?: Node<T>) {
- 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 };
|