From cc6b1b679e551f53f9108365dbcd9f6a866c74f3 Mon Sep 17 00:00:00 2001 From: Meng-Yuan Huang Date: Fri, 17 Jun 2022 14:25:33 +0800 Subject: [PATCH] * Fix a bug that read Bookcase from filesystem before loading cbetaBookcaseDir. --- VERSIONS.md | 3 ++ .../io.github.mrmyhuang.cbetar2.metainfo.xml | 7 +++++ package.json | 4 +-- src/App.tsx | 2 ++ src/CbetaOfflineDb.ts | 29 +++++++++++++++++-- srcElectron/main.ts | 15 ++++++++-- 6 files changed, 53 insertions(+), 7 deletions(-) diff --git a/VERSIONS.md b/VERSIONS.md index 5c15ded..36ae2c4 100644 --- a/VERSIONS.md +++ b/VERSIONS.md @@ -1,4 +1,7 @@ # 版本歷史 +* PWA 9.0.1, Electron app 20.0.1: + * [修正] Electron app 啟動時偶爾讀取離線 DB 失敗。 + * PWA 9.0.0, Electron app 20.0.0: * [變更] 用 JS 改寫讀取檔案系統離線 DB 程式碼。 * [新增] 樹狀目錄新增知名經典目錄。 diff --git a/buildElectron/io.github.mrmyhuang.cbetar2.metainfo.xml b/buildElectron/io.github.mrmyhuang.cbetar2.metainfo.xml index 14eb245..ec7063c 100644 --- a/buildElectron/io.github.mrmyhuang.cbetar2.metainfo.xml +++ b/buildElectron/io.github.mrmyhuang.cbetar2.metainfo.xml @@ -26,6 +26,13 @@ + + +
    +
  • [修正] Electron app 啟動時偶爾讀取離線 DB 失敗。
  • +
+
+
    diff --git a/package.json b/package.json index 7aca396..63eba07 100644 --- a/package.json +++ b/package.json @@ -2,8 +2,8 @@ "name": "cbetar2", "productName": "電子佛典", "homepage": "/", - "pwaVersion": "9.0.0", - "version": "20.0.0", + "pwaVersion": "9.0.1", + "version": "20.0.1", "license": "MIT", "keywords": [ "CBETA", diff --git a/src/App.tsx b/src/App.tsx index 506aa25..ef73a80 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -177,11 +177,13 @@ class _AppOrig extends React.Component { electronBackendApi?.receive("fromMain", (data: any) => { switch (data.event) { case 'version': + // Backend is ready. this.props.dispatch({ type: "TMP_SET_KEY_VAL", key: 'mainVersion', val: data.version, }); + CbetaOfflineDb.setOfflineFileSystemV2Ready(); break; case 'cbetaOfflineDbMode': let dbMode = CbetaDbMode.Online; diff --git a/src/CbetaOfflineDb.ts b/src/CbetaOfflineDb.ts index 11328ff..c34f853 100644 --- a/src/CbetaOfflineDb.ts +++ b/src/CbetaOfflineDb.ts @@ -75,6 +75,11 @@ async function getFileAsStringFromIndexedDB(file: string) { return textDecoder.decode((await IndexedDbFuncs.getZippedFile(file)) as Uint8Array); } +let isOfflineFileSystemV2Ready = false; +export async function setOfflineFileSystemV2Ready() { + isOfflineFileSystemV2Ready = true; +} + export async function init(mode: CbetaDbMode) { // Avoid multiple inits. if (isInitializing) { @@ -111,6 +116,15 @@ export async function init(mode: CbetaDbMode) { const teiStylesheetString = await getFileAsStringFromIndexedDB(`/${Globals.cbetar2AssetDir}/tei.xsl`); await initFromFiles(catalogsString, spinesString, gaijisString, stylesheetString, documentString, teiStylesheetString); } else if (mode === CbetaDbMode.OfflineFileSystemV2) { + await new Promise(ok => { + const timer = setInterval(() => { + if (isOfflineFileSystemV2Ready) { + clearInterval(timer); + ok(); + } + }, 100); + }); + const catalogsString = (await readBookcaseFromFileSystem(`CBETA/catalog.txt`)); const spinesString = (await readBookcaseFromFileSystem(`CBETA/spine.txt`)); const gaijisString = (await readResourceFromFileSystem(`cbeta_gaiji/cbeta_gaiji.json`)); @@ -363,7 +377,11 @@ async function readResourceFromFileSystem(path: string) { electronBackendApi?.receiveOnce('fromMain', (data: any) => { switch (data.event) { case 'readResource': - ok(data.data); + if (data.error) { + fail(data.error); + } else { + ok(data.data); + } break; } }); @@ -376,7 +394,11 @@ async function readBookcaseFromFileSystem(path: string) { electronBackendApi?.receiveOnce('fromMain', (data: any) => { switch (data.event) { case 'readBookcase': - ok(data.data); + if (data.error) { + fail(data.error); + } else { + ok(data.data); + } break; } }); @@ -389,7 +411,8 @@ const CbetaOfflineDb = { fetchCatalogs, fetchAllCatalogs, fetchWork, - fetchJuan + fetchJuan, + setOfflineFileSystemV2Ready, }; export default CbetaOfflineDb; \ No newline at end of file diff --git a/srcElectron/main.ts b/srcElectron/main.ts index 3d5d334..e4dccb1 100644 --- a/srcElectron/main.ts +++ b/srcElectron/main.ts @@ -225,10 +225,21 @@ async function createWindow() { mainWindow?.webContents.send('fromMain', { event: 'version', version: PackageInfos.version }); break; case 'readResource': - mainWindow?.webContents.send('fromMain', Object.assign({ event: args.event }, { data: fs.readFileSync(`${isDevMode() ? '.' : resourcesPath}/${args.path}`).toString() })); + try { + const str = fs.readFileSync(`${isDevMode() ? '.' : resourcesPath}/${args.path}`).toString(); + mainWindow?.webContents.send('fromMain', Object.assign({ event: args.event }, { data: str })); + } catch (error) { + mainWindow?.webContents.send('fromMain', Object.assign({ event: args.event }, { error: error })); + } break; case 'readBookcase': - mainWindow?.webContents.send('fromMain', Object.assign({ event: args.event }, { data: fs.readFileSync(`${settings.cbetaBookcaseDir}/${args.path}`).toString() })); + try { + const str = fs.readFileSync(`${settings.cbetaBookcaseDir}/${args.path}`).toString(); + mainWindow?.webContents.send('fromMain', Object.assign({ event: args.event }, { data: str })); + } catch (error) { + mainWindow?.webContents.send('fromMain', Object.assign({ event: args.event }, { error: error })); + } + break; } });