🌈 style: 修改代码格式
This commit is contained in:
parent
d005f06bd9
commit
c8de724698
@ -1,10 +1,8 @@
|
||||
import { DoubleQueue } from './double-queue';
|
||||
import type { Future } from './types';
|
||||
|
||||
export class AsyncQueue<T> {
|
||||
static CancelledError = class CancelledError extends Error {};
|
||||
protected _queue: DoubleQueue<T>;
|
||||
protected _getterFutures = new DoubleQueue<Future<T>>();
|
||||
protected _getters = new DoubleQueue<Future<T>>();
|
||||
|
||||
constructor(iterable: Iterable<T> = []) {
|
||||
this._queue = new DoubleQueue(iterable);
|
||||
@ -19,13 +17,13 @@ export class AsyncQueue<T> {
|
||||
return this._queue.shift() as T;
|
||||
}
|
||||
const future: Future<T> = Promise.withResolvers();
|
||||
this._getterFutures.push(future);
|
||||
this._getters.push(future);
|
||||
return future.promise;
|
||||
}
|
||||
|
||||
push(...values: T[]): this {
|
||||
for (const value of values) {
|
||||
const future = this._getterFutures.shift();
|
||||
const future = this._getters.shift();
|
||||
if (future !== undefined) {
|
||||
future.resolve(value);
|
||||
} else {
|
||||
@ -36,8 +34,12 @@ export class AsyncQueue<T> {
|
||||
}
|
||||
|
||||
cancelGetters() {
|
||||
for (const future of this._getterFutures) {
|
||||
for (const future of this._getters) {
|
||||
future.reject(new AsyncQueue.CancelledError());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export namespace AsyncQueue {
|
||||
export class CancelledError extends Error {}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user