TangJieHaoT
Denoβ€’2y agoβ€’
8 replies
TangJieHao

command.spawn() stdin and stdout

I am not sure if I am handling the writeable stream and readable stream correctly.
I assigned getWrite and getReader to writer and reader in my class.

I am slightly confused on what ready, releaseLock is as it's not well documented (from what sourced I can find)
private async stdin(query: Query): Promise<void> {
    const data = this.encoder.encode(JSON.stringify(query) + "\n");
    await this.writer.ready;
    await this.writer.write(data);
    this.writer.releaseLock();
}

private async stdout(): Promise<AIObject> {
    const result = await this.reader.read();
    if (result.done) {
        throw new Error("Stream finished unexpectedly.");
    }
    const responseStr = this.decoder.decode(result.value);
    return JSON.parse(responseStr) as AIObject;
}
Was this page helpful?