-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
📦 Use Web Streams API version of vim-channel-command
- Loading branch information
1 parent
155aa08
commit ae4a9c0
Showing
1 changed file
with
29 additions
and
48 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); | ||
} | ||
|
@@ -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(); | ||
} | ||
} | ||
|
||
|