Sync all offline changes to the remote server

This commit is contained in:
Andras Schmelczer 2024-12-18 20:40:29 +00:00
parent eb87de8e68
commit 742e0fd7e5
No known key found for this signature in database
GPG key ID: FC8F2C3D3D1A718C
3 changed files with 140 additions and 7 deletions

View file

@ -0,0 +1,53 @@
import { Database } from "src/database/database";
import { FileOperations } from "src/file-operations/file-operations";
import { Logger } from "src/logger";
import { SyncService } from "src/services/sync_service";
import { syncRemotelyUpdatedFile } from "./sync-remotely-updated-file";
let isRunning = false;
export async function applyRemoteChangesLocally(
database: Database,
syncServer: SyncService,
operations: FileOperations
) {
if (isRunning) {
Logger.getInstance().info("Pull sync already in progress, skipping");
return;
}
isRunning = true;
try {
if (!database.getSettings().isSyncEnabled) {
return;
}
const remote = await syncServer.getAll(database.getLastSeenUpdateId());
if (remote.latestDocuments.length === 0) {
Logger.getInstance().debug("No remote changes to apply");
return;
}
Logger.getInstance().info("Applying remote changes locally");
await Promise.all(
remote.latestDocuments.map((remoteDocument) =>
syncRemotelyUpdatedFile({
database,
syncServer,
operations: operations,
remoteVersion: remoteDocument,
})
)
);
await database.setLastSeenUpdateId(remote.lastUpdateId);
} catch (e) {
Logger.getInstance().error(
`Failed to apply remote changes locally: ${e}`
);
} finally {
isRunning = false;
}
}