swc/bundler/tests/.cache/untrusted/0b7e941fba0142ef7b838db3cfd99370b9047b22.ts
강동윤 bbaf619f63
fix(bundler): Fix bugs (#1437)
swc_bundler:
 - [x] Fix wrapped esms. (denoland/deno#9307)
 - [x] Make test secure.
2021-03-02 17:33:03 +09:00

52 lines
1.1 KiB
TypeScript

// Loaded from https://raw.githubusercontent.com/deno-postgres/deno-postgres/master/connection/deferred.ts
import { Deferred, deferred } from "../deps.ts";
export class DeferredStack<T> {
private _array: Array<T>;
private _queue: Array<Deferred<T>>;
private _maxSize: number;
private _size: number;
constructor(
max?: number,
ls?: Iterable<T>,
private _creator?: () => Promise<T>,
) {
this._maxSize = max || 10;
this._array = ls ? [...ls] : [];
this._size = this._array.length;
this._queue = [];
}
async pop(): Promise<T> {
if (this._array.length > 0) {
return this._array.pop()!;
} else if (this._size < this._maxSize && this._creator) {
this._size++;
return await this._creator();
}
const d = deferred<T>();
this._queue.push(d);
await d;
return this._array.pop()!;
}
push(value: T): void {
this._array.push(value);
if (this._queue.length > 0) {
const d = this._queue.shift()!;
d.resolve();
}
}
get size(): number {
return this._size;
}
get available(): number {
return this._array.length;
}
}