Skip to content

Commit

Permalink
Merge branch 'master' of github.com:MrMYHuang/cbetar2
Browse files Browse the repository at this point in the history
# Conflicts:
#	buildElectron/io.github.mrmyhuang.cbetar2.metainfo.xml
  • Loading branch information
MrMYHuang committed Jun 17, 2022
2 parents 3097b5d + cc6b1b6 commit 8afb252
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 7 deletions.
3 changes: 3 additions & 0 deletions VERSIONS.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
# <a id='history'>版本歷史</a>
* PWA 9.0.1, Electron app 20.0.1:
* [修正] Electron app 啟動時偶爾讀取離線 DB 失敗。

* PWA 9.0.0, Electron app 20.0.0:
* [變更] 用 JS 改寫讀取檔案系統離線 DB 程式碼。
* [新增] 樹狀目錄新增知名經典目錄。
Expand Down
7 changes: 7 additions & 0 deletions buildElectron/io.github.mrmyhuang.cbetar2.metainfo.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,13 @@
<content_rating type="oars-1.1"/>
<releases>
<release version="20.0.1" date="2022-06-17">
<description>
<ul>
<li>[修正] Electron app 啟動時偶爾讀取離線 DB 失敗。</li>
</ul>
</description>
</release>
<release version="20.0.0" date="2022-06-17">
<description>
<ul>
<li>[優化] 升級 Electron 19.0.4.</li>
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -177,11 +177,13 @@ class _AppOrig extends React.Component<AppOrigProps, State> {
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;
Expand Down
29 changes: 26 additions & 3 deletions src/CbetaOfflineDb.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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<void>(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`));
Expand Down Expand Up @@ -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;
}
});
Expand All @@ -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;
}
});
Expand All @@ -389,7 +411,8 @@ const CbetaOfflineDb = {
fetchCatalogs,
fetchAllCatalogs,
fetchWork,
fetchJuan
fetchJuan,
setOfflineFileSystemV2Ready,
};

export default CbetaOfflineDb;
15 changes: 13 additions & 2 deletions srcElectron/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
});
Expand Down

0 comments on commit 8afb252

Please sign in to comment.