|
1 | | -import { Converter, ConverterBuild, init } from "./opencc-rust-lib.mjs" |
| 1 | +import { Converter, ConverterBuild, init } from "./opencc-rust-lib.mjs"; |
2 | 2 |
|
3 | 3 | const RESOURCES = { |
4 | 4 | opencc_wasm: "https://cdn.jsdelivr.net/gh/polyproline/opencc-wasm@main/opencc_gc.wasm", |
@@ -47,21 +47,29 @@ const CONVERTION_MAP = { |
47 | 47 |
|
48 | 48 | const SELECTED_DICT = CONVERTION_MAP.ToTaiwan; |
49 | 49 |
|
50 | | -const wasm = await fetch(RESOURCES.opencc_wasm); |
51 | | -await init(wasm); |
| 50 | +let _converter = null; |
52 | 51 |
|
53 | | -const build = ConverterBuild.new(); |
54 | | -for (let dict of SELECTED_DICT) { |
55 | | - if (Array.isArray(dict)) { |
56 | | - for(let d of dict) { |
57 | | - build.adddict(await fetch(RESOURCES[d]).then(response => response.text())); |
| 52 | +async function initOpenccRust() { |
| 53 | + const wasm = await fetch(RESOURCES.opencc_wasm); |
| 54 | + await init(wasm); |
| 55 | + |
| 56 | + const build = ConverterBuild.new(); |
| 57 | + for (let dict of SELECTED_DICT) { |
| 58 | + if (Array.isArray(dict)) { |
| 59 | + for(let d of dict) { |
| 60 | + build.adddict(await fetch(RESOURCES[d]).then(response => response.text())); |
| 61 | + } |
| 62 | + } else { |
| 63 | + build.adddict(await fetch(RESOURCES[dict]).then(response => response.text())); |
58 | 64 | } |
59 | | - } else { |
60 | | - build.adddict(await fetch(RESOURCES[dict]).then(response => response.text())); |
| 65 | + build.group(); |
61 | 66 | } |
62 | | - build.group(); |
| 67 | + _converter = build.build(); |
63 | 68 | } |
64 | 69 |
|
65 | | -const converter = build.build(); |
| 70 | +function getConverter() { |
| 71 | + if (!_converter) throw new Error("You must call and await initOpenccRust() first."); |
| 72 | + return _converter; |
| 73 | +} |
66 | 74 |
|
67 | | -export { converter }; |
| 75 | +export { initOpenccRust, getConverter }; |
0 commit comments