Skip to content

Commit

Permalink
Merge pull request #171 from vim-skk/write
Browse files Browse the repository at this point in the history
Fix skkServer hang up
  • Loading branch information
kuuote authored Dec 30, 2023
2 parents 6d9b187 + 56f98f3 commit 47a777e
Showing 1 changed file with 38 additions and 14 deletions.
52 changes: 38 additions & 14 deletions denops/skkeleton/jisyo/skk_server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,37 +21,45 @@ export class SkkServer implements Dictionary {
}

async connect() {
this.close();
this.#conn = await Deno.connect(this.connectOptions);
}

async getHenkanResult(_type: HenkanType, word: string): Promise<string[]> {
if (!this.#conn) return [];
await this.connect();

await this.#conn.write(encode(`1${word} `, this.requestEncoding));
if (!this.#conn) return [];

const result: string[] = [];
try {
await this.write(`1${word} `);

for await (
const str of iterLine(this.#conn.readable, this.responseEncoding)
) {
if (str.length === 0) {
continue;
}

result.push(...(str.at(0) === "4") ? [] : str.split("/").slice(1, -1));

if (str.endsWith("\n")) {
break;
}
break;
}
} catch (_e) {
// NOTE: ReadableStream may be locked
}

// NOTE: Close the current connection.
// Because the stream is locked.
this.close();

return result;
}

async getCompletionResult(
prefix: string,
feed: string,
): Promise<CompletionData> {
if (!this.#conn) return [];

let midashis: string[] = [];
if (feed != "") {
const table = getKanaTable();
Expand All @@ -78,30 +86,48 @@ export class SkkServer implements Dictionary {

private async getMidashis(prefix: string): Promise<string[]> {
// Get midashis from prefix
await this.connect();

if (!this.#conn) return [];

await this.#conn.write(encode(`4${prefix} `, this.requestEncoding));
const result: string[] = [];
try {
await this.write(`4${prefix} `);

for await (
const str of iterLine(this.#conn.readable, this.responseEncoding)
) {
if (str.length === 0) {
continue;
}

result.push(...(str.at(0) === "4") ? [] : str.split("/").slice(1, -1));

if (str.endsWith("\n")) {
break;
}
break;
}
} catch (_e) {
// NOTE: ReadableStream may be locked
}

// NOTE: Close the current connection.
// Because the stream is locked.
this.close();

return result;
}

close() {
this.#conn?.write(encode("0", this.requestEncoding));
this.#conn?.close();
this.#conn = undefined;
}

private async write(str: string) {
if (!this.#conn) return;

await this.#conn.write(encode(str, this.requestEncoding));
const reader = this.#conn.readable.getReader();
reader.releaseLock();
}
}

Expand All @@ -118,9 +144,7 @@ async function* iterLine(
.pipeThrough(new TextLineStream());

for await (const line of lines) {
if ((line as string).length) {
yield line as string;
}
yield line as string;
}
}

Expand Down

0 comments on commit 47a777e

Please sign in to comment.