Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

mobile: push/pull force sync

Ammar Ahmed837c29682114
diff --git a/apps/mobile/app/components/announcements/cta.js b/apps/mobile/app/components/announcements/cta.js
index 2b6f95476..46b8d9215 100644
--- a/apps/mobile/app/components/announcements/cta.js
+++ b/apps/mobile/app/components/announcements/cta.js
@@ -60,7 +60,7 @@ export const Cta = ({ actions, style = {}, color, inline }) => {
eSendEvent(eCloseSheet);
await sleep(300);
Progress.present();
- Sync.run("global", true, true, () => {
+ Sync.run("global", true, "full", () => {
eSendEvent(eCloseSheet);
});
}
diff --git a/apps/mobile/app/components/auth/login.js b/apps/mobile/app/components/auth/login.js
index 556bd20f2..70d27b482 100644
--- a/apps/mobile/app/components/auth/login.js
+++ b/apps/mobile/app/components/auth/login.js
@@ -67,7 +67,7 @@ export const Login = ({ changeMode }) => {
Progress.present();
setTimeout(() => {
if (!useUserStore.getState().syncing) {
- Sync.run("global", false, true);
+ Sync.run("global", false, "full");
}
}, 5000);
});
diff --git a/apps/mobile/app/components/auth/session-expired.js b/apps/mobile/app/components/auth/session-expired.js
index 8aec60ea8..7c81d8d4c 100644
--- a/apps/mobile/app/components/auth/session-expired.js
+++ b/apps/mobile/app/components/auth/session-expired.js
@@ -112,7 +112,7 @@ export const SessionExpired = () => {
if (!res) throw new Error("no token found");
if (db.tokenManager._isTokenExpired(res))
throw new Error("token expired");
- Sync.run("global", false, true, async (complete) => {
+ Sync.run("global", false, "full", async (complete) => {
if (!complete) {
let user = await db.user.getUser();
if (!user) return;
diff --git a/apps/mobile/app/components/list/index.tsx b/apps/mobile/app/components/list/index.tsx
index 2e9f5c279..b13d8eefc 100644
--- a/apps/mobile/app/components/list/index.tsx
+++ b/apps/mobile/app/components/list/index.tsx
@@ -80,7 +80,7 @@ export default function List(props: ListProps) {
const groupOptions = useGroupOptions(groupType);
const _onRefresh = async () => {
- Sync.run("global", false, true, () => {
+ Sync.run("global", false, "full", () => {
props.onRefresh?.();
});
};
diff --git a/apps/mobile/app/hooks/use-app-events.tsx b/apps/mobile/app/hooks/use-app-events.tsx
index aab39827a..4512a5af2 100644
--- a/apps/mobile/app/hooks/use-app-events.tsx
+++ b/apps/mobile/app/hooks/use-app-events.tsx
@@ -200,11 +200,13 @@ const onRequestPartialSync = async (
`onRequestPartialSync full:${full}, force:${force}, lastSyncTime:${lastSyncTime}`
);
- if (full || force) {
- await Sync.run("global", force, full, undefined, lastSyncTime);
- } else {
- await Sync.run("global", false, false, undefined, lastSyncTime);
- }
+ await Sync.run(
+ "global",
+ force,
+ full ? "full" : "send",
+ undefined,
+ lastSyncTime
+ );
};
const onLogout = async (reason: string) => {
diff --git a/apps/mobile/app/screens/settings/settings-data.tsx b/apps/mobile/app/screens/settings/settings-data.tsx
index 4f77be13b..2d11a6c99 100644
--- a/apps/mobile/app/screens/settings/settings-data.tsx
+++ b/apps/mobile/app/screens/settings/settings-data.tsx
@@ -470,22 +470,45 @@ export const settingsGroups: SettingSection[] = [
}
},
{
- id: "sync-issues-fix",
- name: "Having problems with sync",
- description: "Try force sync to resolve issues with syncing",
- icon: "sync-alert",
- modifer: async () => {
+ id: "pull-sync",
+ name: "Force pull changes",
+ description:
+ "Force pull changes from the server. Use this if some changes are not appearing on this device.",
+ modifer: () => {
+ presentDialog({
+ title: "Force Pull changes",
+ paragraph:
+ "Force pull changes from the server. Run this if some changes are not appearing on this device.",
+ negativeText: "Cancel",
+ positiveText: "Start",
+ positivePress: async () => {
+ eSendEvent(eCloseSheet);
+ await sleep(300);
+ Progress.present();
+ Sync.run("global", true, "fetch", () => {
+ eSendEvent(eCloseSheet);
+ });
+ }
+ });
+ }
+ },
+ {
+ id: "push-sync",
+ name: "Force push changes",
+ description:
+ "Force push changes to the server. Use this if some changes from this device are not appearing on other devices.",
+ modifer: () => {
presentDialog({
- title: "Force sync",
+ title: "Force Push changes",
paragraph:
- "If your data on two devices is out of sync even after trying to sync normally. You can run force sync to solve such problems. Usually you should never need to run this otherwise. Force sync means that all your data on this device is reuploaded to the server.",
+ "Force push changes to the server. Run this only if some changes from this device are not appearing on other devices.",
negativeText: "Cancel",
positiveText: "Start",
positivePress: async () => {
eSendEvent(eCloseSheet);
await sleep(300);
Progress.present();
- Sync.run("global", true, true, () => {
+ Sync.run("global", true, "send", () => {
eSendEvent(eCloseSheet);
});
}
diff --git a/apps/mobile/app/services/sync.js b/apps/mobile/app/services/sync.ts
similarity index 79%
rename from apps/mobile/app/services/sync.js
rename to apps/mobile/app/services/sync.ts
index a6dbf7af0..1cd7481aa 100644
--- a/apps/mobile/app/services/sync.js
+++ b/apps/mobile/app/services/sync.ts
@@ -32,19 +32,24 @@ export const ignoredMessages = [
"WebSocket failed to connect",
"Failed to start the HttpConnection before"
];
-let pendingSync = undefined;
-let syncTimer = 0;
+let pendingSync: any = undefined;
+let syncTimer: NodeJS.Timeout;
+
const run = async (
context = "global",
forced = false,
- full = true,
- onCompleted,
- lastSyncTime
+ type: "full" | "send" | "fetch" = "full",
+ onCompleted?: (status?: number) => void,
+ lastSyncTime?: number
) => {
if (useUserStore.getState().syncing) {
DatabaseLogger.info("Sync in progress");
pendingSync = {
- full: full
+ forced,
+ type: type,
+ context: context,
+ onCompleted,
+ lastSyncTime
};
return;
}
@@ -70,7 +75,7 @@ const run = async (
) {
initAfterSync();
pendingSync = undefined;
- return onCompleted?.(false);
+ return onCompleted?.(SyncStatus.Failed);
}
userstore.setSyncing(true);
@@ -80,9 +85,8 @@ const run = async (
await BackgroundSync.doInBackground(async () => {
try {
await db.sync({
- type: full ? "full" : "send",
- force: forced,
- lastSyncTime
+ type: type,
+ force: forced
});
} catch (e) {
error = e;
@@ -95,14 +99,16 @@ const run = async (
} catch (e) {
error = e;
if (
- !ignoredMessages.find((message) => e.message?.includes(message)) &&
+ !ignoredMessages.find((message) =>
+ (e as Error).message?.includes(message)
+ ) &&
userstore.user &&
status.isConnected &&
status.isInternetReachable
) {
userstore.setSyncing(false, SyncStatus.Failed);
if (status.isConnected && status.isInternetReachable) {
- ToastManager.error(e, "Sync failed", context);
+ ToastManager.error(e as Error, "Sync failed", context);
}
}
@@ -115,7 +121,14 @@ const run = async (
);
onCompleted?.(error ? SyncStatus.Failed : SyncStatus.Passed);
setImmediate(() => {
- if (pendingSync) Sync.run("global", false, pendingSync.full);
+ if (pendingSync)
+ Sync.run(
+ pendingSync.context,
+ pendingSync.forced,
+ pendingSync.type,
+ pendingSync.onCompleted,
+ pendingSync.lastSyncTime
+ );
});
}
}, 300);