//#region src/client/stream/transport/queue.ts
var AsyncQueue = class {
	values = [];
	waiters = [];
	rejecters = [];
	closed = false;
	error = null;
	push(value) {
		if (this.closed) return;
		const waiter = this.waiters.shift();
		this.rejecters.shift();
		if (waiter) {
			waiter({
				done: false,
				value
			});
			return;
		}
		this.values.push(value);
	}
	close(error) {
		if (this.closed) return;
		this.closed = true;
		this.error = error == null ? null : error instanceof Error ? error : new Error(String(error));
		if (this.error) {
			for (const rejecter of this.rejecters.splice(0)) rejecter(this.error);
			this.waiters.length = 0;
			return;
		}
		for (const waiter of this.waiters.splice(0)) waiter({
			done: true,
			value: void 0
		});
		this.rejecters.length = 0;
	}
	async shift() {
		if (this.values.length > 0) return {
			done: false,
			value: this.values.shift()
		};
		if (this.error) throw this.error;
		if (this.closed) return {
			done: true,
			value: void 0
		};
		return await new Promise((resolve, reject) => {
			this.waiters.push(resolve);
			this.rejecters.push(reject);
		});
	}
};
//#endregion
exports.AsyncQueue = AsyncQueue;

//# sourceMappingURL=queue.cjs.map