-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.ts
82 lines (75 loc) · 3.22 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import * as Comlink from './comlink';
import pako from "pako";
import { untarBuffer } from "./untar";
import { TokenizeResult, ErrorValue } from '../crate/pkg';
export { TokenizeResultValue, TokenizeResult, ErrorValue } from '../crate/pkg';
const wasmImport = import("../crate/pkg");
interface This {
initialized: boolean;
loaded: boolean;
_readFromBin: (system_dict_bin: Uint8Array, char_def_bin: Uint8Array) => ErrorValue;
_tokenize: (text: string) => TokenizeResult;
}
export class WasmModule {
readonly initialized: boolean = false;
readonly loaded: boolean = false;
async initialize(this: This) {
if (this.initialized) { return }
console.debug((new Date()).toISOString(), "worker: start initialize");
const wasm = await wasmImport;
this._readFromBin = wasm.read_from_bin;
this._tokenize = wasm.tokenize;
this.initialized = true;
console.debug((new Date()).toISOString(), "worker: finish initialize");
}
async tokenize(this: This, text: string) {
if (!this.initialized) {
throw new Error("NotWasmInitialized");
}
if (!this.loaded) {
throw new Error("NotDictionaryLoaded");
}
console.debug((new Date()).toISOString(), "worker: tokenize text", text);
const result = await this._tokenize(text);
if (result.ok) {
console.debug((new Date()).toISOString(), "worker: tokenize results", result.ok);
return { type: "results" as "results", results: result.ok };
} else if (result.err) {
console.error((new Date()).toISOString(), "worker: tokenize error", result.err.error);
return { type: "error" as "error", error: result.err.error || "EmptyResponse" };
} else {
return { type: "error" as "error", error: "EmptyResponse" };
}
}
async readFromBin(this: This, systemDicTarGzURL: string, charDefURL: string) {
if (!this.initialized) {
throw new Error("NotWasmInitialized");
}
console.debug((new Date()).toISOString(), "worker: readFromBin start fetch");
const [systemDicBin, charDefBin] = await Promise.all([
(async () => {
const res = await fetch(systemDicTarGzURL, { mode: "cors" });
const buf = await res.arrayBuffer();
const unzipArray = pako.inflate(new Uint8Array(buf));
const files = Array.from(untarBuffer(unzipArray.buffer));
const file = files.find((file) => file.name.includes("system.dic"))
if (!file) { throw new Error("NotFoundSystemDic"); }
return new Uint8Array(file.buffer);
})(),
(async () => {
const res = await fetch(charDefURL, { mode: "cors" });
const buf = await res.arrayBuffer();
return new Uint8Array(buf);
})()
]);
console.debug((new Date()).toISOString(), "worker: readFromBin finish fetch");
console.debug((new Date()).toISOString(), "worker: systemDicBin.length", systemDicBin.length);
console.debug((new Date()).toISOString(), "worker: charDefBin.length", charDefBin.length);
console.debug((new Date()).toISOString(), "worker: readFromBin start load");
const result = await this._readFromBin(systemDicBin, charDefBin);
this.loaded = true;
console.debug((new Date()).toISOString(), "worker: readFromBin finish load");
return result;
}
};
Comlink.expose(new WasmModule());