Skip to content

Commit

Permalink
use @libreservice/lazy-cache
Browse files Browse the repository at this point in the history
  • Loading branch information
eagleoflqj committed May 28, 2023
1 parent 49a7b6d commit b00afcf
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 27 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ Chinese IME powered by [RIME](https://github.com/rime/librime).

https://my-rime.vercel.app/


This is a **STATIC** website so you **DON'T** need to own a server to host it.

All computation is performed in browser, thanks to Web Assembly.

It's also a [PWA](https://web.dev/progressive-web-apps/), so you can install it like a native App and use it **OFFLINE**.

If you want to distribute your own IME, see [customize](doc/customize.md).

If you want to deploy schemas dynamically (online, like how you deploy in Desktop/Mobile platforms), see [deploy](doc/deploy.md).
Expand Down
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<link rel="apple-touch-icon" href="./apple-touch-icon.png">
<link rel="mask-icon" href="./LibreService.svg" color="#FFFFFF">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1" />
<title>My RIME</title>
<title>My RIME - LibreService</title>
<meta name="description" content="Free and Open Source online Chinese IME powered by RIME. 自由开源在线中文输入法 拼音 双拼 粤语 注音 五笔 仓颉 速成">
</head>
<body>
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
},
"devDependencies": {
"@codemirror/legacy-modes": "^6.3.2",
"@libreservice/lazy-cache": "^0.1.0",
"@libreservice/my-widget": "^0.1.2",
"@libreservice/my-worker": "^0.4.0",
"@libreservice/wasm-code": "^0.1.1",
Expand Down
28 changes: 3 additions & 25 deletions src/worker.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
import { expose, control, loadWasm, fsOperate } from '@libreservice/my-worker'
import { openDB } from 'idb'
import { LazyCache } from '@libreservice/lazy-cache'
import schemaName from '../schema-name.json'
import schemaFiles from '../schema-files.json'
import schemaTarget from '../schema-target.json'
import dependencyMap from '../dependency-map.json'
import targetFiles from '../target-files.json'
import targetVersion from '../target-version.json'

const HASH = 'hash'
const CONTENT = 'content'

function getURL (target: string, name: string) {
if ('__RIME_CDN__') { // eslint-disable-line no-constant-condition
return '__RIME_CDN__' + `${target}@${(targetVersion as {[key: string]: string})[target]}/${name}`
}
return `ime/${target}/${name}`
}

const dbPromise = openDB('ime', 1, {
upgrade (db) {
db.createObjectStore(HASH)
db.createObjectStore(CONTENT)
}
})
const lazyCache = new LazyCache('ime')

async function setIME (schemaId: string) {
const fetched: string[] = []
Expand Down Expand Up @@ -58,26 +50,12 @@ async function setIME (schemaId: string) {
return files
}
const files = getFiles(schemaId)
const db = await dbPromise.catch(() => undefined) // not available in Firefox Private Browsing
await Promise.all(files.map(async ({ name, target, md5 }) => {
const path = `build/${name}`
try {
Module.FS.lookupPath(path)
} catch (e) { // not exists
const storedHash: string | undefined = await db?.get(HASH, name)
let ab: ArrayBuffer
if (storedHash === md5) {
ab = await db!.get(CONTENT, name)
} else {
const url = getURL(target, name)
const response = await fetch(url)
if (!response.ok) {
throw new Error(`Fail to download ${name}`)
}
ab = await response.arrayBuffer()
await db?.put(CONTENT, ab, name)
await db?.put(HASH, md5, name)
}
const ab = await lazyCache.get(name, md5, getURL(target, name))
Module.FS.writeFile(path, new Uint8Array(ab))
}
}))
Expand Down
2 changes: 1 addition & 1 deletion test/test-advanced.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ test('Deploy', async ({ page }) => {
await page.locator('.n-dialog .n-button').getByText('Upload').click()

const content = page.locator('.cm-content')
await page.getByText('default.yaml', { exact: true }).click()
await page.locator('.n-tree').getByText('default.yaml', { exact: true }).click()
await content.click()
await page.keyboard.press('Control+f')
await page.keyboard.insertText(' - schema: luna_pinyin_fluency[\\s\\S]+- schema: quick5\\n')
Expand Down

0 comments on commit b00afcf

Please sign in to comment.