Skip to content

Commit

Permalink
📦 Use Web Streams API version of vim-channel-command
Browse files Browse the repository at this point in the history
  • Loading branch information
lambdalisue committed May 14, 2023
1 parent 155aa08 commit ae4a9c0
Showing 1 changed file with 29 additions and 48 deletions.
77 changes: 29 additions & 48 deletions denops/@denops-private/host/vim.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,35 @@
import {
readerFromStreamReader,
writerFromStreamWriter,
} from "https://deno.land/[email protected]/streams/mod.ts";
import {
Session as VimSession,
} from "https://deno.land/x/[email protected]/mod.ts#^";
import type {
Message as VimMessage,
} from "https://deno.land/x/[email protected]/mod.ts#^";
import { responseTimeout } from "../defs.ts";
Client,
Message,
Session,
} from "https://deno.land/x/[email protected]/mod.ts";
import { Invoker, isInvokerMethod } from "./invoker.ts";
import { Host } from "./base.ts";

export class Vim implements Host {
#reader: ReadableStreamDefaultReader<Uint8Array>;
#writer: WritableStreamDefaultWriter<Uint8Array>;
#session: VimSession;
#session: Session;
#client: Client;

constructor(
reader: ReadableStream<Uint8Array>,
writer: WritableStream<Uint8Array>,
) {
this.#reader = reader.getReader();
this.#writer = writer.getWriter();
this.#session = new VimSession(
readerFromStreamReader(this.#reader),
writerFromStreamWriter(this.#writer),
undefined,
{
responseTimeout,
},
);
this.#session = new Session(reader, writer);
this.#session.start();
this.#client = new Client(this.#session);
}

redraw(force?: boolean): Promise<void> {
return this.#session.redraw(force);
this.#client.redraw(force);
return Promise.resolve();
}

async call(fn: string, ...args: unknown[]): Promise<unknown> {
const [ret, err] = await this.#session.call(
const [ret, err] = (await this.#client.call(
"denops#api#vim#call",
fn,
args,
) as [
unknown,
string,
];
)) as [unknown, string];
if (err !== "") {
throw new Error(`Failed to call '${fn}(${args.join(", ")})': ${err}`);
}
Expand All @@ -55,38 +39,35 @@ export class Vim implements Host {
async batch(
...calls: [string, ...unknown[]][]
): Promise<[unknown[], string]> {
return await this.#session.call("denops#api#vim#batch", calls) as [
return (await this.#client.call("denops#api#vim#batch", calls)) as [
unknown[],
string,
];
}

register(invoker: Invoker): void {
this.#session.replaceCallback(async (message: VimMessage) => {
this.#session.onMessage = (message: Message) => {
const [msgid, expr] = message;
let ok = null;
let err = null;
try {
ok = await dispatch(invoker, expr);
} catch (e) {
err = e;
}
if (msgid !== 0) {
await this.#session.reply(msgid, [ok, err]);
} else if (err !== null) {
console.error(err);
}
});
dispatch(invoker, expr)
.then((result) => {
this.#client.reply(msgid, [result, null]);
})
.catch((error) => {
if (msgid) {
this.#client.reply(msgid, [null, error]);
} else {
console.error(error);
}
});
};
}

waitClosed(): Promise<void> {
return this.#session.waitClosed();
return this.#session.wait();
}

dispose(): void {
this.#reader.releaseLock();
this.#writer.releaseLock();
this.#session.dispose();
this.#session.shutdown();
}
}

Expand Down

0 comments on commit ae4a9c0

Please sign in to comment.