From 78a6a5c36288cf846a220a8aa3a45293f327471e Mon Sep 17 00:00:00 2001 From: silvanshade Date: Sat, 18 Jun 2022 13:10:52 -0600 Subject: [PATCH] Refactoring --- packages/app/src/client.ts | 16 +--------------- packages/app/src/codec.ts | 27 ++++++++++++++++++++++----- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/packages/app/src/client.ts b/packages/app/src/client.ts index 86d93fd..4e31409 100644 --- a/packages/app/src/client.ts +++ b/packages/app/src/client.ts @@ -1,24 +1,10 @@ import * as jsrpc from "json-rpc-2.0"; import * as proto from "vscode-languageserver-protocol"; -import { Bytes, FromServer, Headers, IntoServer } from "./codec"; +import { Codec, FromServer, IntoServer } from "./codec"; const consoleChannel = document.getElementById("channel-console") as HTMLTextAreaElement; -class Codec { - static encode(json: jsrpc.JSONRPCRequest | jsrpc.JSONRPCResponse): Uint8Array { - const message = JSON.stringify(json); - const delimited = Headers.add(message); - return Bytes.encode(delimited); - } - - static decode(data: Uint8Array): T { - const delimited = Bytes.decode(data); - const message = Headers.remove(delimited); - return JSON.parse(message) as T; - } -} - export default class Client extends jsrpc.JSONRPCServerAndClient { afterInitializedHooks: (() => Promise)[] = []; #fromServer: FromServer; diff --git a/packages/app/src/codec.ts b/packages/app/src/codec.ts index 5c1753e..bb838d9 100644 --- a/packages/app/src/codec.ts +++ b/packages/app/src/codec.ts @@ -1,3 +1,4 @@ +import * as jsrpc from "json-rpc-2.0"; import * as vsrpc from "vscode-jsonrpc"; import Queue from "./queue"; @@ -6,6 +7,20 @@ import Tracer from "./tracer"; const encoder = new TextEncoder(); const decoder = new TextDecoder(); +export class Codec { + static encode(json: jsrpc.JSONRPCRequest | jsrpc.JSONRPCResponse): Uint8Array { + const message = JSON.stringify(json); + const delimited = Headers.add(message); + return Bytes.encode(delimited); + } + + static decode(data: Uint8Array): T { + const delimited = Bytes.decode(data); + const message = Headers.remove(delimited); + return JSON.parse(message) as T; + } +} + export class Bytes { static encode(input: string): Uint8Array { return encoder.encode(input); @@ -44,10 +59,10 @@ export class Headers { } export class PromiseMap { - #map: Map> = new Map(); + #map: Map> = new Map(); get(key: K & { toString(): string }): null | Promise { - let initialized: PromiseMap.type; + let initialized: PromiseMap.Entry; // if the entry doesn't exist, set it if (!this.#map.has(key)) { initialized = this.#set(key); @@ -65,7 +80,7 @@ export class PromiseMap { } } - #set(key: K, value?: V): PromiseMap.type { + #set(key: K, value?: V): PromiseMap.Entry { if (this.#map.has(key)) { // eslint-disable-next-line @typescript-eslint/no-non-null-assertion return this.#map.get(key)!; @@ -79,7 +94,7 @@ export class PromiseMap { resolve = resolver; }); // the initialized entry - const initialized: PromiseMap.type = { status: "pending", resolve, promise }; + const initialized: PromiseMap.Entry = { status: "pending", resolve, promise }; if (null != value) { initialized.resolve(value); } @@ -107,7 +122,9 @@ export class PromiseMap { // eslint-disable-next-line @typescript-eslint/no-namespace export namespace PromiseMap { - export type type = { status: "pending"; resolve: (item: V) => void; promise: Promise } | { status: "resolved" }; + export type Entry = + | { status: "pending"; resolve: (item: V) => void; promise: Promise } + | { status: "resolved" }; } // FIXME: tracing effiency