Skip to content

Commit

Permalink
feat: optimize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
ourongxing committed Sep 5, 2024
1 parent e2afa41 commit 7798157
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 40 deletions.
13 changes: 5 additions & 8 deletions app/components/ExportAction.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,13 @@ export function ExportAction() {
const { sendJsonMessage, lastJsonMessage } = useWebSocket<ExportLog>(`ws://${location.host}/_ws`)
const [previewLib, setPreviewLib] = useAtom(previewLibAtom)
useEffect(() => {
if (lastJsonMessage)
if (lastJsonMessage) {
setExportState({
...exportState,
logs: [lastJsonMessage, ...exportState.logs],
status: lastJsonMessage.completed === lastJsonMessage.all ? "completed" : "running",
status: lastJsonMessage.completed === lastJsonMessage.all || lastJsonMessage.stop ? "completed" : "running",
})
}
}, [lastJsonMessage])

const exportAll = useCallback(() => {
Expand All @@ -29,8 +30,7 @@ export function ExportAction() {
type: "stop",
error: "用户主动停止",
})
}
else {
} else {
setExportState({ ...exportState, range: "all", status: "running", logs: [] })
// 异步的
sendJsonMessage<WSMessgae<ExportFnProps>>({
Expand All @@ -55,8 +55,7 @@ export function ExportAction() {
type: "stop",
error: "用户主动停止",
})
}
else {
} else {
setExportState({ ...exportState, range: "selected", status: "running", logs: [] })
sendJsonMessage<WSMessgae<ExportFnProps>>({
type: "export",
Expand Down Expand Up @@ -87,10 +86,8 @@ export function ExportAction() {
const allStatus = useMemo(() => {
if (!databaseStatus.maimemo_base || (exportState.status === "running" && exportState.range === "selected"))
return "disabled"

else if (exportState.status === "running" && exportState.range === "all")
return "stop"

else
return "idle"
}, [exportState, databaseStatus.maimemo_base])
Expand Down
1 change: 1 addition & 0 deletions shared/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ export interface ExportLog {
// 🟢 🟡 🔴 绿=成功 黄=跳过或者没导出 红=失败
name: string
time: string
stop?: boolean
}
export interface SelectedLib {
id: number
Expand Down
59 changes: 32 additions & 27 deletions src/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,19 @@ import fs from "fs-extra"
import { getLibWords, getLibs, translateAll } from "./get"
import { checkDatabases, databases, ensureTargetFolders } from "./db"
import { transform } from "./transform"
import type { ExportFnProps, ExportLog, Target, TrafficLights } from "@/types"
import type { ExportFnProps, ExportLog, Target, TrafficLights, Word } from "@/types"

export async function exportLib({ selected, range, type, options, fnEvery }: ExportFnProps & { fnEvery: (log: ExportLog) => Promise<boolean> }) {
checkDatabases()
const targetFolders = await ensureTargetFolders(options.folderName)

let libs = selected
if (range === "all")
if (range === "all") {
libs = getLibs(type).map(k => ({
id: k.id,
name: k.name,
}))
}

const initStatus = {
word: "🟡",
Expand All @@ -28,49 +29,53 @@ export async function exportLib({ selected, range, type, options, fnEvery }: Exp
}

for (const lib of libs) {
const words = getLibWords({
let words: Word[] = []
words = getLibWords({
id: lib.id,
type,
exculedMemorized: options.exculedMemorized,
})
for (const target of options.target) {
let path = join(targetFolders[target], `${lib.name}.txt`)
if (target === "translation")
path = join(targetFolders[target], `${lib.name}.csv`)

if (!options.override && fs.existsSync(path)) {
score.status[target] = "🟡"
continue
}

let content = ""
if (target === "translation")
if (databases.ecdict?.db)
content = transform(translateAll(words), target)
else
try {
let path = join(targetFolders[target], `${lib.name}.txt`)
if (target === "translation")
path = join(targetFolders[target], `${lib.name}.csv`)
if (!options.override && fs.existsSync(path)) {
score.status[target] = "🟡"
continue
else
content = transform(words, target)
}
if (!words.length) throw new Error("No words found")

if (!content) {
let content = ""
if (target === "translation") {
if (databases.ecdict?.db) {
content = transform(translateAll(words), target)
} else {
throw new Error("No ecdict database found")
}
} else {
content = transform(words, target)
}
await fs.writeFile(path, content)
score.status[target] = "🟢"
} catch (e) {
console.error(e)
score.status[target] = "🔴"
score.failed++
continue
}
await fs.writeFile(path, content)
score.status[target] = "🟢"
}
const status = [score.status.word, score.status.list, score.status.translation] as ExportLog["status"]
score.completed++
if (status.includes("🔴")) score.failed++

const log: ExportLog = {
completed: score.completed,
failed: score.failed,
status: [score.status.word, score.status.list, score.status.translation],
status,
name: lib.name,
all: libs.length,
time: new Date().toLocaleString(),
}
if (await fnEvery(log))
return
if (await fnEvery(log)) return
score.status = { ...initStatus }
}
}
2 changes: 1 addition & 1 deletion src/sql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const sql = {
WHERE origin_id in (
SELECT DISTINCT book_origin_id
FROM BK_VOC_TB)
ORDER BY id`,
ORDER BY name`,
queryBaseLibWordsByID: `SELECT spelling AS word, list
FROM VOC_TB
INNER JOIN (
Expand Down
11 changes: 7 additions & 4 deletions ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default eventHandler({
handler: () => { },
websocket: defineWebSocket({
async open() {
console.log("WebSocket opened")
console.log("webSocket opened")
},
async message(peer, message) {
const msg = JSON.parse(message.text()) as WSMessgae<ExportFnProps>
Expand All @@ -23,7 +23,10 @@ export default eventHandler({
// 留 100 ms 接收 stop,并且确保不要发送太快,避免 UI 刷新不及时
const now = Date.now()
await delay(now - last > 100 ? 1 : 100)
peer.send(log)
peer.send({
...log,
stop,
})
last = now
return stop
},
Expand All @@ -35,10 +38,10 @@ export default eventHandler({
}
},
async close() {
console.log("WebSocket closed")
console.log("webSocket closed")
},
async error() {
console.log("WebSocket error")
console.log("webSocket error")
},
}),
})

0 comments on commit 7798157

Please sign in to comment.