Skip to content

Commit

Permalink
mobile: push/pull force sync
Browse files Browse the repository at this point in the history
  • Loading branch information
ammarahm-ed authored and thecodrr committed May 9, 2024
1 parent 9aed6fa commit 837c296
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 30 deletions.
2 changes: 1 addition & 1 deletion apps/mobile/app/components/announcements/cta.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/app/components/auth/login.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/app/components/auth/session-expired.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion apps/mobile/app/components/list/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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?.();
});
};
Expand Down
12 changes: 7 additions & 5 deletions apps/mobile/app/hooks/use-app-events.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
39 changes: 31 additions & 8 deletions apps/mobile/app/screens/settings/settings-data.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -70,7 +75,7 @@ const run = async (
) {
initAfterSync();
pendingSync = undefined;
return onCompleted?.(false);
return onCompleted?.(SyncStatus.Failed);
}
userstore.setSyncing(true);

Expand All @@ -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;
Expand All @@ -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);
}
}

Expand All @@ -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);
Expand Down

0 comments on commit 837c296

Please sign in to comment.