Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: auto refresh app on db crash #1829

Merged
merged 4 commits into from
Jan 31, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions src/core/listeners/db.ts
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!");
});
}
1 change: 1 addition & 0 deletions src/core/listeners/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import HapticsListener from "./HapticsListener";

// Listeners
import "./androidSafeArea";
import "./db";
import "./ionActivatable";
import "./network/listener";
import "./statusTap";
Expand Down