Skip to content

Open-source gym & body-weight tracker, now in the App Store (iPhone + Apple Watch) and as an Android APK. Plan routines, log workouts, see which muscles are trained, import from Strong/Hevy/FitNotes. Self-host it or use it on the phone alone.

mobile: the App listener's rejection reaches the catch

Boris 'B' Kurktchievca4dddd32639
diff --git a/frontend/src/lib/mobile.js b/frontend/src/lib/mobile.js
index 6f5da81e2..edd95a136 100644
--- a/frontend/src/lib/mobile.js
+++ b/frontend/src/lib/mobile.js
@@ -158,9 +158,12 @@ export function initReminderSync(getState) {
syncReminder(getState()).catch(() => {})
}
document.addEventListener('visibilitychange', resync)
- import('@capacitor/app').then(({ App }) => {
+ // addListener is a promise of its own — it rejects when the App plugin is not behind the bridge
+ // (a native project cap sync never updated, a custom platform) — so it is returned into the
+ // catch below. Dropped inside a block, as it was before, that rejection went unhandled.
+ import('@capacitor/app').then(({ App }) =>
App.addListener('appStateChange', ({ isActive }) => { if (isActive) resync() })
- }).catch(() => {})
+ ).catch(() => {})
}
// Runs cb whenever the native shell returns to the foreground — the store pulls the account's
@@ -168,9 +171,10 @@ export function initReminderSync(getState) {
// off mobile; the store's own visibility/focus listeners cover the browser.
export function onAppActive(cb) {
if (!MOBILE) return
- import('@capacitor/app').then(({ App }) => {
+ // Returned, not dropped, for the same reason as in initReminderSync.
+ import('@capacitor/app').then(({ App }) =>
App.addListener('appStateChange', ({ isActive }) => { if (isActive) cb() })
- }).catch(() => {})
+ ).catch(() => {})
}
// WKWebView can't do blob-URL downloads, so the backup goes out through the OS share sheet
diff --git a/frontend/src/lib/mobile.listener.test.js b/frontend/src/lib/mobile.listener.test.js
new file mode 100644
index 000000000..18a706183
--- /dev/null
+++ b/frontend/src/lib/mobile.listener.test.js
@@ -0,0 +1,64 @@
+// @vitest-environment happy-dom
+/* initReminderSync and onAppActive both drop the promise App.addListener returns inside a block
+ * body ( .then(({ App }) => { App.addListener(...) }) ), so when the App plugin is not behind the
+ * bridge — a native project whose cap sync never ran, or a custom platform — Capacitor's proxy
+ * rejects the call (UNIMPLEMENTED) and that rejection never reaches the existing .catch() right
+ * behind it: every boot raised an unhandled rejection.
+ *
+ * A vi.fn cannot reproduce this: vitest attaches its own handlers to every promise a spy returns,
+ * which counts as handling it and hides exactly the leak this pins. rejectingApp below is a plain
+ * object instead, the same shape Capacitor's own proxy has.
+ */
+import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest'
+
+const mocks = vi.hoisted(() => ({ App: null }))
+vi.mock('@capacitor/app', () => ({ get App() { return mocks.App } }))
+
+const load = async () => {
+ vi.stubEnv('VITE_MOBILE', '1')
+ vi.resetModules()
+ return import('./mobile.js')
+}
+const tick = () => new Promise(r => setTimeout(r, 0))
+// What Capacitor's plugin proxy does when the App plugin is not behind the bridge: addListener
+// returns a promise that rejects. Deliberately not a vi.fn — see the file header.
+const rejectingApp = () => ({
+ calls: 0,
+ addListener() { this.calls++; return Promise.reject(new Error('"App" plugin is not implemented on android')) },
+})
+const settled = async ready => {
+ for (let i = 0; i < 50 && !ready(); i++) await tick()
+ await tick()
+}
+
+let unhandled
+const trap = reason => unhandled.push(reason instanceof Error ? reason.message : String(reason))
+beforeEach(() => {
+ unhandled = []
+ process.on('unhandledRejection', trap)
+ mocks.App = rejectingApp()
+})
+afterEach(async () => {
+ await tick()
+ process.off('unhandledRejection', trap)
+ vi.unstubAllEnvs()
+ expect(unhandled).toEqual([]) // no test in this file may leave a rejection behind
+})
+
+describe('initReminderSync — the App plugin rejecting must not go unhandled', () => {
+ it('reaches the catch instead of raising an unhandled rejection', async () => {
+ const m = await load()
+ m.initReminderSync(() => ({ routines: [], week: {}, dayPlan: {}, workouts: [], reminder: { on: false } }))
+ await settled(() => mocks.App.calls > 0)
+ expect(unhandled).toEqual([])
+ })
+})
+
+describe('onAppActive — the App plugin rejecting must not go unhandled', () => {
+ it('reaches the catch instead of raising an unhandled rejection', async () => {
+ const m = await load()
+ m.onAppActive(() => {})
+ await settled(() => mocks.App.calls > 0)
+ expect(unhandled).toEqual([])
+ })
+})