Files
notesnook/apps/mobile/app/services/sync.ts

126 lines
3.5 KiB
TypeScript
Raw Permalink Normal View History

/*
This file is part of the Notesnook project (https://notesnook.com/)
2023-01-16 13:44:52 +05:00
Copyright (C) 2023 Streetwriters (Private) Limited
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-08-30 16:13:11 +05:00
2023-06-10 12:17:23 +05:00
import NetInfo from "@react-native-community/netinfo";
import { db } from "../common/database";
import { DatabaseLogger } from "../common/database/index";
2023-06-10 12:17:23 +05:00
import { initAfterSync } from "../stores/index";
import { SyncStatus, useUserStore } from "../stores/use-user-store";
2023-09-21 13:01:39 +05:00
import BackgroundSync from "./background-sync";
2023-08-29 20:42:45 +05:00
import { ToastManager } from "./event-manager";
2022-12-16 12:35:22 +05:00
import SettingsService from "./settings";
2020-12-20 23:01:35 +05:00
export const ignoredMessages = [
"Sync already running",
"Not allowed to start service intent",
"WebSocket failed to connect",
2024-08-27 15:20:34 +05:00
"Failed to start the HttpConnection before",
"Could not connect to the Sync server",
"Network request failed"
];
2024-05-06 15:57:47 +05:00
let pendingSync: any = undefined;
let syncTimer: NodeJS.Timeout;
2025-03-18 12:52:21 +05:00
let lastSyncType = "full";
2024-05-06 15:57:47 +05:00
const run = async (
context = "global",
forced = false,
2024-05-06 15:57:47 +05:00
type: "full" | "send" | "fetch" = "full",
onCompleted?: (status?: number) => void,
lastSyncTime?: number
) => {
if (useUserStore.getState().syncing) {
2023-09-21 13:01:39 +05:00
DatabaseLogger.info("Sync in progress");
pendingSync = {
2024-05-06 15:57:47 +05:00
forced,
type: type,
context: context,
onCompleted,
lastSyncTime
2023-09-21 13:01:39 +05:00
};
return;
}
2023-09-21 13:01:39 +05:00
if (pendingSync) {
pendingSync = undefined;
DatabaseLogger.info("Running pending sync...");
}
2022-07-20 23:12:52 +05:00
clearTimeout(syncTimer);
syncTimer = setTimeout(async () => {
const userstore = useUserStore.getState();
2024-08-27 15:20:34 +05:00
userstore.setSyncing(true);
2022-07-20 23:12:52 +05:00
const user = await db.user.getUser();
2023-09-21 13:01:39 +05:00
2024-08-27 15:20:34 +05:00
if (!user || SettingsService.get().disableSync) {
userstore.setSyncing(false);
2025-03-18 12:52:21 +05:00
initAfterSync(type != "send" ? "full" : "send");
2023-09-21 13:01:39 +05:00
pendingSync = undefined;
2024-05-06 15:57:47 +05:00
return onCompleted?.(SyncStatus.Failed);
2021-12-23 12:40:20 +05:00
}
2023-09-21 13:01:39 +05:00
2025-03-18 12:52:21 +05:00
lastSyncType = type != "send" ? "full" : "send";
2024-08-27 15:20:34 +05:00
let error: Error | undefined = undefined;
2022-08-31 13:03:22 +05:00
2022-07-20 23:12:52 +05:00
try {
await db.sync({
type: type,
force: forced,
offlineMode: SettingsService.get().offlineMode
2022-07-20 23:12:52 +05:00
});
2023-09-21 13:01:39 +05:00
2024-08-27 15:20:34 +05:00
if (error) throw error;
2022-07-20 23:12:52 +05:00
} catch (e) {
2024-08-27 15:20:34 +05:00
error = e as Error;
DatabaseLogger.error(error, "[Client] Failed to sync");
if (
2024-08-27 15:20:34 +05:00
!ignoredMessages.find((message) => error?.message?.includes(message)) &&
userstore.user
) {
2024-08-27 15:20:34 +05:00
const status = await NetInfo.fetch();
2023-08-29 20:42:45 +05:00
if (status.isConnected && status.isInternetReachable) {
2024-05-06 15:57:47 +05:00
ToastManager.error(e as Error, "Sync failed", context);
2023-08-29 20:42:45 +05:00
}
2022-07-20 23:12:52 +05:00
}
} finally {
userstore.setSyncing(
false,
error ? SyncStatus.Failed : SyncStatus.Passed
);
2023-09-21 13:01:39 +05:00
onCompleted?.(error ? SyncStatus.Failed : SyncStatus.Passed);
2024-08-27 15:20:34 +05:00
if (pendingSync)
Sync.run(
pendingSync.context,
pendingSync.forced,
pendingSync.type,
pendingSync.onCompleted,
pendingSync.lastSyncTime
);
2020-12-20 23:01:35 +05:00
}
}, 300);
2020-12-20 23:01:35 +05:00
};
2022-02-28 23:25:18 +05:00
const Sync = {
2025-03-18 12:52:21 +05:00
run,
getLastSyncType: () => lastSyncType
2020-12-20 23:01:35 +05:00
};
2022-02-28 23:25:18 +05:00
export default Sync;