Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
kitce committed Jan 16, 2022
2 parents e05ddd2 + da30d92 commit 8215f55
Showing 1 changed file with 11 additions and 7 deletions.
18 changes: 11 additions & 7 deletions src/helpers/cloud.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,26 @@ export const sync = async () => {
const subscriptions = selectSubscriptions(state) as Subscription[];
// merge with local data
const modifiedTime = new Date(file.modifiedTime!).getTime();
// if it has synced before and local is newer than remote, merge local into remote
// otherwise, merge remote into local
const mergeDirection = (!!meta.lastSyncedTime && meta.lastModifiedTime > modifiedTime) ? MergeDirection.Incoming : MergeDirection.Local;
const { lastModifiedTime, lastSyncedTime } = meta;
// NOTE: `lastSyncedTime` will never be greater than `modifiedTime`
// `lastSyncedTime === modifiedTime` => the file was updated from this instance in the previous sync
// => if local is newer than remote, merge local into remote, otherwise, merge remote into local
// `lastSyncedTime < modifiedTime` => the file has been updated from another instance since the previous sync
// => then always merge remote into local
const mergeDirection = ((lastSyncedTime === modifiedTime) && (lastModifiedTime > modifiedTime)) ? MergeDirection.Incoming : MergeDirection.Local;
const _storage: ISerializedStorage = {
personal: mergePersonal(personal, remoteStorage.personal, mergeDirection),
subscriptions: mergeSubscriptions(subscriptions, remoteStorage.subscriptions, mergeDirection)
};
// load the merged data into store
await loadDataIntoStore(_storage);
// update the data to cloud
storage.update(_storage);
}
// upload the data to cloud
const json = storage.json();
await upload(file.id!, json);
const now = Date.now();
dispatch(metaActions.setLastSyncedTime(now));
const { result } = await upload(file.id!, json);
const lastSyncedTime = new Date(result.modifiedTime!).getTime();
dispatch(metaActions.setLastSyncedTime(lastSyncedTime));
dispatch(syncActions.setError(null));
} catch (err) {
dispatch(syncActions.setError(err));
Expand Down

0 comments on commit 8215f55

Please sign in to comment.