Refactor to use static methods to create streams

This commit is contained in:
silvanshade 2022-05-29 17:41:30 -06:00
parent df051f7741
commit 95fd05ee6b
No known key found for this signature in database
2 changed files with 6 additions and 18 deletions

View file

@ -43,8 +43,8 @@
import init, { serve } from "./dist/server.js"
import { LspStdin, LspStdout } from "./dist/index.js"
const stdin = new LspStdin(document.getElementById("stdin"), document.getElementById("send-button"));
const stdout = new LspStdout(document.getElementById("stdout"));
const stdin = LspStdin.create(document.getElementById("stdin"), document.getElementById("send-button"));
const stdout = LspStdout.create(document.getElementById("stdout"));
await init();
await serve(stdin, stdout);

View file

@ -1,9 +1,7 @@
class LspStdin {
#stream: ReadableStream;
constructor(stdin: HTMLTextAreaElement, sendButton: HTMLButtonElement) {
static create(stdin: HTMLTextAreaElement, sendButton: HTMLButtonElement): ReadableStream {
const encoder = new TextEncoder();
this.#stream = new ReadableStream({
return new ReadableStream({
type: "bytes" as any,
async start(controller) {
while (true) {
@ -25,18 +23,12 @@ class LspStdin {
},
});
}
getReader(): ReadableStreamDefaultReader {
return this.#stream.getReader();
}
}
class LspStdout {
#stream: WritableStream;
constructor(stdout: HTMLTextAreaElement) {
static create(stdout: HTMLTextAreaElement): WritableStream {
const decoder = new TextDecoder();
this.#stream = new WritableStream({
return new WritableStream({
async write(bytes) {
const message = decoder.decode(bytes);
const payload = message.replace(/^Content-Length:\s*\d+\s*/, "");
@ -45,10 +37,6 @@ class LspStdout {
},
});
}
getWriter(): WritableStreamDefaultWriter {
return this.#stream.getWriter();
}
}
export { LspStdin, LspStdout };