Compare commits

...

2 Commits

Author SHA1 Message Date
ammarahm-ed
566273356d mobile: support running pending sync 2023-09-21 13:01:39 +05:00
Abdullah Atta
d3f3a72b7f web: add support for running pending sync 2023-09-20 20:37:19 +05:00
2 changed files with 39 additions and 26 deletions

View File

@@ -17,15 +17,14 @@ You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import { EVENTS } from "@notesnook/core/dist/common";
import NetInfo from "@react-native-community/netinfo";
import { db } from "../common/database";
import { DatabaseLogger } from "../common/database/index";
import { initAfterSync } from "../stores/index";
import { SyncStatus, useUserStore } from "../stores/use-user-store";
import BackgroundSync from "./background-sync";
import { ToastEvent } from "./event-manager";
import SettingsService from "./settings";
import BackgroundSync from "./background-sync";
export const ignoredMessages = [
"Sync already running",
@@ -33,7 +32,7 @@ export const ignoredMessages = [
"WebSocket failed to connect",
"Failed to start the HttpConnection before"
];
let pendingSync = undefined;
let syncTimer = 0;
const run = async (
context = "global",
@@ -43,10 +42,18 @@ const run = async (
lastSyncTime
) => {
if (useUserStore.getState().syncing) {
DatabaseLogger.log("Sync in progress");
console.log("Sync in progress");
DatabaseLogger.info("Sync in progress");
pendingSync = {
full: full
};
return;
}
if (pendingSync) {
pendingSync = undefined;
DatabaseLogger.info("Running pending sync...");
}
clearTimeout(syncTimer);
syncTimer = setTimeout(async () => {
const status = await NetInfo.fetch();
@@ -55,56 +62,54 @@ const run = async (
if (!status.isInternetReachable) {
DatabaseLogger.warn("Internet not reachable");
}
if (
!user ||
!status.isInternetReachable ||
SettingsService.get().disableSync
) {
initAfterSync();
pendingSync = undefined;
return onCompleted?.(false);
}
userstore.setSyncing(true);
let error = null;
try {
let res = await BackgroundSync.doInBackground(async () => {
await BackgroundSync.doInBackground(async () => {
try {
await db.sync(full, forced, lastSyncTime);
return true;
} catch (e) {
error = e;
return e.message;
}
});
if (!res) {
initAfterSync();
userstore.setSyncing(false, SyncStatus.Failed);
return onCompleted?.(false);
if (error) {
throw error;
}
if (typeof res === "string") throw error;
userstore.setSyncing(false);
return onCompleted?.(true);
} catch (e) {
error = e;
if (
!ignoredMessages.find((im) => e.message?.includes(im)) &&
userstore.user
!ignoredMessages.find((message) => e.message?.includes(message)) &&
userstore.user &&
status.isConnected &&
status.isInternetReachable
) {
userstore.setSyncing(false, SyncStatus.Failed);
if (status.isConnected && status.isInternetReachable) {
ToastEvent.error(e, "Sync failed", context);
}
ToastEvent.error(e, "Sync failed", context);
}
DatabaseLogger.error(e, "[Client] Failed to sync");
onCompleted?.(false);
} finally {
initAfterSync();
userstore.setSyncing(
false,
error ? SyncStatus.Failed : SyncStatus.Passed
);
if (full || forced) {
db.eventManager.publish(EVENTS.syncCompleted);
}
onCompleted?.(error ? SyncStatus.Failed : SyncStatus.Passed);
setImmediate(() => {
if (pendingSync) Sync.run("global", false, pendingSync.full);
});
}
}, 300);
};

View File

@@ -40,7 +40,7 @@ import { NetworkCheck } from "../utils/network-check";
const networkCheck = new NetworkCheck();
var syncStatusTimeout = 0;
const BATCH_SIZE = 50;
let pendingSync = false;
/**
* @extends {BaseStore<AppStore>}
@@ -288,8 +288,10 @@ class AppStore extends BaseStore {
syncDisabled: !this.get().isSyncEnabled,
offline: !navigator.onLine
});
if (this.isSyncing()) pendingSync = { full };
return;
}
pendingSync = false;
clearTimeout(syncStatusTimeout);
this.updateLastSynced();
@@ -302,6 +304,12 @@ class AppStore extends BaseStore {
this.updateSyncStatus("completed", true);
await this.updateLastSynced();
if (pendingSync) {
logger.info("Running pending sync", pendingSync);
pendingSync = false;
await this.get().sync(pendingSync.full, false);
}
} catch (err) {
logger.error(err);
if (err.cause === "MERGE_CONFLICT") {