Skip to content

AI-native knowledge base & Evernote/Obsidian alternative with instant sync & native MCP. Free on Cloudflare or self-hosted via Docker.

fix(mobile): keep automatic sync alive when settling a pass fails (#597)

Dariusz Korytoee5e9621d50e
diff --git a/apps/mobile/src/hooks/mobile-sync-settle.test.ts b/apps/mobile/src/hooks/mobile-sync-settle.test.ts
new file mode 100644
index 00000000..c27824b0
--- /dev/null
+++ b/apps/mobile/src/hooks/mobile-sync-settle.test.ts
@@ -0,0 +1,69 @@
+import { describe, expect, test } from "bun:test";
+import { settleMobileSyncPass } from "./mobile-sync-settle";
+
+const createState = (requested = false, requestedForce = false) => ({
+ running: { current: true },
+ requested: { current: requested },
+ requestedForce: { current: requestedForce },
+});
+
+const createEffects = (overrides: Partial<Parameters<typeof settleMobileSyncPass>[1]> = {}) => {
+ const calls = { reruns: [] as boolean[], retries: [] as number[] };
+ return {
+ calls,
+ effects: {
+ refreshQueueItems: async () => [],
+ getRetryDelay: async () => null,
+ rerun: (force: boolean) => calls.reruns.push(force),
+ scheduleRetry: (delay: number) => calls.retries.push(delay),
+ ...overrides,
+ },
+ };
+};
+
+describe("settleMobileSyncPass", () => {
+ test("marks the runner idle and schedules the next retry", async () => {
+ const state = createState();
+ const { calls, effects } = createEffects({ getRetryDelay: async () => 5000 });
+ await settleMobileSyncPass(state, effects);
+ expect(state.running.current).toBe(false);
+ expect(calls.retries).toEqual([5000]);
+ expect(calls.reruns).toEqual([]);
+ });
+
+ test("runs a pass requested meanwhile instead of scheduling a retry", async () => {
+ const state = createState(true, true);
+ const { calls, effects } = createEffects({ getRetryDelay: async () => 5000 });
+ await settleMobileSyncPass(state, effects);
+ expect(state.running.current).toBe(false);
+ expect(state.requested.current).toBe(false);
+ expect(state.requestedForce.current).toBe(false);
+ expect(calls.reruns).toEqual([true]);
+ expect(calls.retries).toEqual([]);
+ });
+
+ test("keeps automatic sync alive when refreshing the visible queue fails", async () => {
+ const state = createState();
+ const { calls, effects } = createEffects({
+ refreshQueueItems: async () => {
+ throw new Error("storage unavailable");
+ },
+ getRetryDelay: async () => 1000,
+ });
+ await settleMobileSyncPass(state, effects);
+ expect(state.running.current).toBe(false);
+ expect(calls.retries).toEqual([1000]);
+ });
+
+ test("does not reject when the retry delay cannot be read", async () => {
+ const state = createState();
+ const { calls, effects } = createEffects({
+ getRetryDelay: async () => {
+ throw new Error("storage unavailable");
+ },
+ });
+ await expect(settleMobileSyncPass(state, effects)).resolves.toBeUndefined();
+ expect(state.running.current).toBe(false);
+ expect(calls.retries).toEqual([]);
+ });
+});
diff --git a/apps/mobile/src/hooks/mobile-sync-settle.ts b/apps/mobile/src/hooks/mobile-sync-settle.ts
new file mode 100644
index 00000000..bb73b173
--- /dev/null
+++ b/apps/mobile/src/hooks/mobile-sync-settle.ts
@@ -0,0 +1,52 @@
+type Flag = { current: boolean };
+
+export type MobileSyncPassState = {
+ running: Flag;
+ requested: Flag;
+ requestedForce: Flag;
+};
+
+/**
+ * Finishes an automatic sync pass. Each step handles its own failure so a
+ * failed queue read can never leave the runner marked as running, which would
+ * silently stop automatic sync until the app restarts.
+ */
+export const settleMobileSyncPass = async (
+ state: MobileSyncPassState,
+ {
+ refreshQueueItems,
+ getRetryDelay,
+ rerun,
+ scheduleRetry,
+ }: {
+ refreshQueueItems: () => Promise<unknown>;
+ getRetryDelay: () => Promise<number | null>;
+ rerun: (force: boolean) => void;
+ scheduleRetry: (delay: number) => void;
+ },
+) => {
+ try {
+ await refreshQueueItems();
+ } catch {
+ // The visible queue refreshes again after the next pass.
+ }
+ state.running.current = false;
+
+ if (state.requested.current) {
+ const nextForce = state.requestedForce.current;
+ state.requested.current = false;
+ state.requestedForce.current = false;
+ rerun(nextForce);
+ return;
+ }
+
+ let retryDelay: number | null = null;
+ try {
+ retryDelay = await getRetryDelay();
+ } catch {
+ // Without a readable queue there is nothing to schedule; foregrounding the app retries.
+ }
+ if (retryDelay !== null) {
+ scheduleRetry(retryDelay);
+ }
+};
diff --git a/apps/mobile/src/hooks/useMobileAutomaticSync.ts b/apps/mobile/src/hooks/useMobileAutomaticSync.ts
index 8d5920db..a8dea6a9 100644
--- a/apps/mobile/src/hooks/useMobileAutomaticSync.ts
+++ b/apps/mobile/src/hooks/useMobileAutomaticSync.ts
@@ -11,6 +11,7 @@ import {
type MobileSyncQueueItem,
} from "../lib/sync-queue";
import { replaceLocalMemoId, upsertLocalMemo } from "../lib/local-mirror";
+import { settleMobileSyncPass } from "./mobile-sync-settle";
type MobileClient = ReturnType<typeof createEdgeEverClient>;
@@ -103,27 +104,24 @@ export const useMobileAutomaticSync = ({
}
} catch {
// Queue metadata remains durable; the scheduled pass resumes it.
- } finally {
- await refreshSyncQueueItems();
- runningRef.current = false;
-
- if (requestedRef.current) {
- const nextForce = requestedForceRef.current;
- requestedRef.current = false;
- requestedForceRef.current = false;
- void runRef.current(nextForce);
- // oxlint-disable-next-line no-unsafe-finally -- the catch above handles every error, so nothing is overridden; the finally block is restructured separately
- return;
- }
-
- const retryDelay = await getMobileSyncRetryDelay(syncQueueScope);
- if (retryDelay !== null) {
- retryTimerRef.current = setTimeout(() => {
- retryTimerRef.current = null;
- void runRef.current(false);
- }, retryDelay);
- }
}
+
+ await settleMobileSyncPass(
+ { running: runningRef, requested: requestedRef, requestedForce: requestedForceRef },
+ {
+ refreshQueueItems: refreshSyncQueueItems,
+ getRetryDelay: () => getMobileSyncRetryDelay(syncQueueScope),
+ rerun: (nextForce) => {
+ void runRef.current(nextForce);
+ },
+ scheduleRetry: (retryDelay) => {
+ retryTimerRef.current = setTimeout(() => {
+ retryTimerRef.current = null;
+ void runRef.current(false);
+ }, retryDelay);
+ },
+ },
+ );
}, [client, dataScope, invalidateSyncedWorkspace, onMemoIdRemapped, queryClient, refreshSyncQueueItems, syncQueueScope]);
const runForcedSync = useCallback(async () => {