//#region src/io.ts
/**
* Adapter between input stream and vm.
*
* It starts a detached promise that fills the vm with input.
* Each read (value or input-closed) emits a signal on the shared {@link ExternalProgressChannel}.
*/
var InputPump = class {
	stopped = false;
	runDone;
	constructor(coreVm, inputReader, channel, errorCallback) {
		this.coreVm = coreVm;
		this.inputReader = inputReader;
		this.channel = channel;
		this.errorCallback = errorCallback;
		this.runDone = this.run().catch(() => {}).then(() => this.inputReader);
	}
	/**
	* Stop the pump.
	* Once finished, returns back the ownership of the input reader for further usage.
	*/
	stop() {
		this.stopped = true;
		return this.runDone;
	}
	async run() {
		while (!this.stopped) {
			let nextValue;
			try {
				nextValue = await this.inputReader.next();
			} catch (e) {
				if (this.stopped) return;
				this.errorCallback(e);
				return;
			}
			if (this.stopped) return;
			if (nextValue.done) {
				this.coreVm.notify_input_closed();
				this.channel.signal();
				return;
			}
			if (nextValue.value !== void 0) {
				this.coreVm.notify_input(nextValue.value);
				this.channel.signal();
			}
		}
	}
};
/**
* Adapter between output stream and vm. It moves forward when [awaitNextProgress] is invoked.
*/
var OutputPump = class {
	constructor(coreVm, outputWriter) {
		this.coreVm = coreVm;
		this.outputWriter = outputWriter;
	}
	async awaitNextProgress() {
		const nextOutput = this.coreVm.take_output();
		if (nextOutput?.length > 0) await this.outputWriter.write(nextOutput);
	}
};

//#endregion
export { InputPump, OutputPump };
//# sourceMappingURL=io.js.map