Skip to content

Commit

Permalink
peer offline sync enhancements
Browse files Browse the repository at this point in the history
  • Loading branch information
XilinJia committed Dec 18, 2023
1 parent df4c21e commit 67486a3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
22 changes: 18 additions & 4 deletions packages/app-mobile/utils/socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ function initSocketIOClient() {
const offlineTime = Setting.value('offlineTime');
let onlineTime = Setting.value('onlineTime');
const quitTime = Setting.value('shutdownTime');
const offTime = offlineTime === onlineTime ? quitTime : offlineTime > quitTime ? offlineTime : quitTime; // TODO: needs review
const lastSyncTime = Setting.value('peerSyncTime');
// offlineTime === onlineTime means the app was thrown out while online without having time to record offlineTime
// const offTime = offlineTime === onlineTime ? quitTime : offlineTime > quitTime ? offlineTime : quitTime; // TODO: needs review
const offTime = lastSyncTime === 0 ? quitTime : lastSyncTime;
reg.logger().info('Client: device offline and shutdown times', new Date(offlineTime), new Date(quitTime), new Date(offTime));

socketIOWorker.emit('updateRequest', { offTime: offTime });
Expand Down Expand Up @@ -103,6 +106,7 @@ function initSocketIOClient() {
onlineTime = new Date().getTime();
Setting.setValue('onlineTime', onlineTime);
Setting.setValue('offlineTime', onlineTime);
Setting.setValue('peerSyncTime', onlineTime);
});

// socketIOClient.on('message', (message: string) => {
Expand Down Expand Up @@ -148,12 +152,22 @@ function initSocketIOClient() {
}

export function workerEmit(tag: string, data: Record<string, string>) {
if (socketIOWorker && socketIOWorker.connected) socketIOWorker.emit(tag, data);
if (socketIOWorker && socketIOWorker.connected) {
socketIOWorker.emit(tag, data);
Setting.setValue('peerSyncTime', new Date().getTime());
}
}

export function workerEmitToPeer(tag: string, data: Record<string, string>, clientName: string = null) {
if (clientName) data = { clientName: clientName, ...data };
if (socketIOWorker && socketIOWorker.connected) socketIOClient.emit('privateMessage', { tag: tag, recipient: clientName, data: data });
if (!clientName) {
reg.logger().error('workerEmitToPeer: clientName empty');
return;
}
data = { clientName: clientName, ...data };
if (socketIOWorker && socketIOWorker.connected) {
socketIOClient.emit('privateMessage', { tag: tag, recipient: clientName, data: data });
Setting.setValue('peerSyncTime', new Date().getTime());
}
}

// Stop the socket connection
Expand Down
1 change: 1 addition & 0 deletions packages/lib/models/Setting.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ class Setting extends BaseModel {
// TODO: not used yet
localFilesDir: { value: '', type: SettingItemType.String, public: false },

peerSyncTime: { value: 0, type: SettingItemType.Int, public: false },
onlineTime: { value: 0, type: SettingItemType.Int, public: false },
offlineTime: { value: 0, type: SettingItemType.Int, public: false },
lastTimeAlive: { value: 0, type: SettingItemType.Int, public: false },
Expand Down
5 changes: 4 additions & 1 deletion packages/lib/socketio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,8 @@ export function initSocketIOServer() {

// handle notes updates
const noteidsClient: string[] = data['noteids'];
reg.logger().info(`Server: received ${noteidsClient.length} node ids from client ${socket.id} for sync`);

const [noteids, notes] = await Note.notesUpdatedAfter(offTime);
let conflistids: string[] = [];
if (noteids.length > 0 && noteidsClient.length > 0) {
Expand All @@ -141,8 +143,9 @@ export function initSocketIOServer() {
await Note.createConflictNote(note, 111);
}
}
socketIOServer.to(socket.id).emit('conflictNotes', { noteids: conflistids });
reg.logger().info(`Server: among the note ids from client, ${conflistids.length} are in conflict`);

socketIOServer.to(socket.id).emit('conflictNotes', { noteids: conflistids });
socket.on('conflictReceived', async (_data) => {
reg.logger().info('Server: received conflictReceived from ', socket.id, new Date(offTime));
if (noteids.length > 0) {
Expand Down

0 comments on commit 67486a3

Please sign in to comment.