From f4de326e4054577e7353728c6fdd3b0bacd75d80 Mon Sep 17 00:00:00 2001 From: Songchen Tan Date: Sun, 30 Jun 2024 00:28:22 -0400 Subject: [PATCH] Add instant statistics via s2 --- package-lock.json | 6 ++++++ package.json | 3 ++- src/atoms/cache.ts | 4 +++- src/atoms/utils.ts | 25 +++++++------------------ src/lib/assembly.ts | 14 ++++++++------ src/worker.ts | 23 +++++++++++++++-------- vite.config.ts | 8 ++++++++ 7 files changed, 49 insertions(+), 34 deletions(-) diff --git a/package-lock.json b/package-lock.json index 75186a3..e126cee 100644 --- a/package-lock.json +++ b/package-lock.json @@ -29,6 +29,7 @@ "jotai-optics": "^0.4.0", "js-md5": "^0.8.3", "js-yaml": "4.1.0", + "libchai": "^0.1.12", "lodash-es": "^4.17.21", "lz-string": "^1.5.0", "mathjs": "^13.0.1", @@ -6708,6 +6709,11 @@ "node": ">= 0.8.0" } }, + "node_modules/libchai": { + "version": "0.1.12", + "resolved": "https://registry.npmjs.org/libchai/-/libchai-0.1.12.tgz", + "integrity": "sha512-Gg8dJ3kK1UVetSTAjsGk15wytFFoHqqPp7lieHx2eEmYURen4UyhyleNUlBKDLyJ1XcVfKNaprwr8S35Ovi5CA==" + }, "node_modules/lines-and-columns": { "version": "1.2.4", "resolved": "https://registry.npmjs.org/lines-and-columns/-/lines-and-columns-1.2.4.tgz", diff --git a/package.json b/package.json index 9aa7b0c..7aa415e 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "chai", "private": true, - "version": "0.1.11", + "version": "0.1.12", "type": "module", "scripts": { "start": "vite --mode CF", @@ -43,6 +43,7 @@ "jotai-optics": "^0.4.0", "js-md5": "^0.8.3", "js-yaml": "4.1.0", + "libchai": "^0.1.12", "lodash-es": "^4.17.21", "lz-string": "^1.5.0", "mathjs": "^13.0.1", diff --git a/src/atoms/cache.ts b/src/atoms/cache.ts index 802b044..d65a9a0 100644 --- a/src/atoms/cache.ts +++ b/src/atoms/cache.ts @@ -19,6 +19,7 @@ import { keyboardAtom, mappingAtom, meaningfulObjectiveAtom, + priorityShortCodesAtom, repertoireAtom, } from "."; import { assetsAtom, customElementsAtom } from "./assets"; @@ -78,7 +79,8 @@ export const assemblyResultAtom = atom(async (get) => { const dictionary = await get(dictionaryAtom); const analysisResult = await get(analysisResultAtom); const customElements = get(customElementsAtom); - const config = { algebra, encoder, keyboard }; + const priority = get(priorityShortCodesAtom); + const config = { algebra, encoder, keyboard, priority }; return await thread.spawn("assembly", [ repertoire, config, diff --git a/src/atoms/utils.ts b/src/atoms/utils.ts index bb42817..b321c3c 100644 --- a/src/atoms/utils.ts +++ b/src/atoms/utils.ts @@ -1,4 +1,4 @@ -import type { Config, EncodeResult } from "~/lib"; +import type { Config } from "~/lib"; import { isValidCJKChar, stringifySequence } from "~/lib"; import useTitle from "ahooks/es/useTitle"; import init, { validate } from "libchai"; @@ -12,36 +12,25 @@ import type { WorkerOutput } from "~/worker"; import { atomEffect } from "jotai-effect"; import { configAtom } from "./config"; import { assetsAtom } from "./assets"; -import { assemblyResultAtom, encodeResultAtom } from "./cache"; -import { atom } from "jotai"; -import { meaningfulObjectiveAtom } from "./encoder"; +import { assemblyResultAtom } from "./cache"; -export const syncConfig = atomEffect((get, set) => { +export const syncConfig = atomEffect((get) => { const value = get(configAtom); - thread - .spawn("sync", ["config", value]) - .then(() => set(dummyAtom, (x) => x + 1)); + thread.spawn("sync", ["config", value]); }); -export const syncAssets = atomEffect((get, set) => { +export const syncAssets = atomEffect((get) => { get(assetsAtom).then(async (value) => { await thread.spawn("sync", ["assets", value]); - set(dummyAtom, (x) => x + 1); }); }); -export const syncInfo = atomEffect((get, set) => { +export const syncInfo = atomEffect((get) => { get(assemblyResultAtom).then(async (value) => { - await thread.spawn("sync", [ - "info", - stringifySequence(value, get(configAtom)), - ]); - set(dummyAtom, (x) => x + 1); + await thread.spawn("sync", ["info", stringifySequence(value)]); }); }); -const dummyAtom = atom(0); - export const RemoteContext = createContext(true); export async function validateConfig(config: Config) { diff --git a/src/lib/assembly.ts b/src/lib/assembly.ts index 229d94d..228a591 100644 --- a/src/lib/assembly.ts +++ b/src/lib/assembly.ts @@ -1,7 +1,6 @@ import type { Algebra, Condition, - Config, EncoderConfig, Grouping, Keyboard, @@ -217,6 +216,7 @@ interface AssembleConfig { encoder: EncoderConfig; keyboard: Keyboard; algebra: Algebra; + priority: [string, string, number][]; } /** @@ -334,17 +334,19 @@ export const assemble = ( }); knownWords.add(hash); } - return result; + const priorityMap = getPriorityMap(config.priority); + return result.map((x) => { + const hash = `${x.name}-${x.pinyin_list.join(",")}`; + const level = priorityMap.get(hash) ?? -1; + return { ...x, level }; + }); }; -export const stringifySequence = (result: AssemblyResult, config: Config) => { - const priorityMap = getPriorityMap(config.encoder.priority_short_codes ?? []); +export const stringifySequence = (result: AssemblyResult) => { return result.map((x) => { - const hash = `${x.name}-${x.pinyin_list.join(",")}`; return { ...x, sequence: summarize(x.sequence), - level: priorityMap.get(hash) ?? -1, }; }); }; diff --git a/src/worker.ts b/src/worker.ts index e0545e8..01aac49 100644 --- a/src/worker.ts +++ b/src/worker.ts @@ -1,9 +1,6 @@ import init, { WebInterface } from "libchai"; import { analysis } from "./lib/repertoire"; import { assemble } from "./lib/assembly"; -import { defaultConfig } from "./lib"; -import type { Assets } from "./atoms"; - export interface WorkerInput { type: "sync" | "encode" | "evaluate" | "optimize" | "analysis" | "assembly"; data: any; @@ -38,11 +35,21 @@ export type WorkerOutput = }; await init(); -const webInterface = WebInterface.new(self.postMessage, defaultConfig, [], { - frequency: {}, - key_distribution: {}, - pair_equivalence: {}, -} satisfies Assets); +const webInterface = WebInterface.new( + self.postMessage, + { + info: { name: "" }, + source: null, + form: { alphabet: "", mapping: {} }, + encoder: { max_length: 0, sources: {}, conditions: {} }, + }, + [], + { + frequency: {}, + key_distribution: {}, + pair_equivalence: {}, + }, +); self.onmessage = async (event: MessageEvent) => { const channel = event.ports[0]!; diff --git a/vite.config.ts b/vite.config.ts index 070c5f4..d717460 100644 --- a/vite.config.ts +++ b/vite.config.ts @@ -37,6 +37,11 @@ export default defineConfig(({ mode }) => { emptyOutDir: true, reportCompressedSize: false, }, + esbuild: { + supported: { + "top-level-await": true, + }, + }, define: { APP_VERSION: JSON.stringify(process.env.npm_package_version), }, @@ -74,6 +79,9 @@ export default defineConfig(({ mode }) => { reporter: ["text", "html"], }, }, + worker: { + format: "es", + }, }; switch (mode) {