mirror of
https://github.com/silvanshade/tower-lsp-web-demo.git
synced 2025-01-22 11:47:51 +00:00
Refactor to use static methods to create streams
This commit is contained in:
parent
df051f7741
commit
95fd05ee6b
2 changed files with 6 additions and 18 deletions
|
@ -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);
|
||||
|
|
|
@ -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 };
|
||||
|
|
Loading…
Reference in a new issue