-
-
Notifications
You must be signed in to change notification settings - Fork 168
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: auto refresh app on db crash (#1829)
- Loading branch information
Showing
2 changed files
with
48 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { App } from "@capacitor/app"; | ||
import Dexie from "dexie"; | ||
|
||
import { isAppleDeviceInstallable, isNative } from "#/helpers/device"; | ||
|
||
const DB_CLOSED_STORAGE_KEY = "db-closed"; | ||
|
||
/** | ||
* https://github.com/ionic-team/cordova-plugin-ionic-webview/issues/354#issuecomment-1305878417 | ||
*/ | ||
if (isNative() && isAppleDeviceInstallable()) { | ||
App.addListener("appStateChange", async (state) => { | ||
const dbLastClosed = +(localStorage.getItem(DB_CLOSED_STORAGE_KEY) || 0); | ||
const thirtySecondsAgo = Date.now() - 30_000; | ||
|
||
const reloadedRecently = dbLastClosed > thirtySecondsAgo; | ||
|
||
if (!state.isActive) return; | ||
if (reloadedRecently) return; | ||
|
||
console.info("Checking database integrity..."); | ||
|
||
// Fix connection Storage sometime lost (iOS) | ||
try { | ||
await Dexie.exists("WefwefDB"); | ||
} catch (error) { | ||
if (!(error instanceof Error)) throw error; | ||
if (error.name !== "UnknownError") throw error; | ||
if ( | ||
!error.message.includes( | ||
"Connection to Indexed Database server lost. Refresh the page to try again", | ||
) | ||
) | ||
throw error; | ||
|
||
console.info("Failed database integrity check!", error); | ||
|
||
localStorage.setItem(DB_CLOSED_STORAGE_KEY, Date.now().toString()); | ||
|
||
window.location.reload(); | ||
|
||
throw error; | ||
} | ||
|
||
console.info("Passed database integrity check!"); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters