Skip to content

Commit

Permalink
wip:
Browse files Browse the repository at this point in the history
* add handle to receive get sync status
* add check sync status on fallback
  • Loading branch information
migueldesarrollosoftware authored and miguelsw committed Apr 11, 2024
1 parent c0534ea commit f36a055
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 10 deletions.
12 changes: 7 additions & 5 deletions src/apps/main/remote-sync/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import eventBus from '../event-bus';
import { RemoteSyncManager } from './RemoteSyncManager';
import { DriveFilesCollection } from '../database/collections/DriveFileCollection';
import { DriveFoldersCollection } from '../database/collections/DriveFolderCollection';
import { clearRemoteSyncStore } from './helpers';
import { clearRemoteSyncStore, RemoteSyncStatus } from './helpers';
import { getNewTokenClient } from '../../shared/HttpClient/main-process-client';
import Logger from 'electron-log';
import { ipcMain } from 'electron';
Expand Down Expand Up @@ -104,10 +104,7 @@ eventBus.on('USER_LOGGED_OUT', () => {

ipcMain.on('CHECK_SYNC', (event) => {
Logger.info('Checking sync');
event.sender.send(
'CHECK_SYNC_ENGINE_RESPONSE',
'Dato obtenido del proceso de sincronización'
);
event.sender.send('CHECK_SYNC_ENGINE_RESPONSE', '');
});

ipcMain.on('CHECK_SYNC_CHANGE_STATUS', async (_, placeholderStates) => {
Expand All @@ -117,3 +114,8 @@ ipcMain.on('CHECK_SYNC_CHANGE_STATUS', async (_, placeholderStates) => {
await sleep(7_00);
remoteSyncManager.placeholderStatus = placeholderStates;
});

ipcMain.handle('CHECK_SYNC_IN_PROGRESS', async () => {
const syncingStatus: RemoteSyncStatus = 'SYNCING';
return remoteSyncManager.getSyncStatus() === syncingStatus;
});
1 change: 1 addition & 0 deletions src/apps/sync-engine/BindingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ export class BindingsManager {
}

private async pollingStart() {
Logger.debug('[SYNC ENGINE] Starting polling');
return this.container.pollingMonitorStart.run(this.polling.bind(this));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { MonitorFn, PollingMonitor } from '../domain/PollingMonitor';
import { ipcRenderer } from 'electron';
import Logger from 'electron-log';

export class PollingMonitorStart {
constructor(private readonly polling: PollingMonitor) {}
run(fn: MonitorFn) {
return this.polling.start(fn);
Logger.info('[SYNC ENGINE] check sync engine');

const permission = this.permissionFn.bind(this);
return this.polling.start(fn, permission);
}

private async permissionFn() {
const isSyncing = await ipcRenderer.invoke('CHECK_SYNC_IN_PROGRESS');
Logger.info('[SYNC ENGINE] is syncing', isSyncing);

const isPermitted = !isSyncing;
return isPermitted;
}
}
18 changes: 14 additions & 4 deletions src/context/virtual-drive/shared/domain/PollingMonitor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type MonitorFn = () => Promise<void>;
export type PermissionFn = () => Promise<boolean>;
export class PollingMonitor {
constructor(private readonly delay: number) {}

Expand All @@ -11,16 +12,25 @@ export class PollingMonitor {
}
}

private setTimeout(fn: MonitorFn) {
private setTimeout(fn: MonitorFn, permissionFn: PermissionFn) {
this.clearTimeout();
this.timeout = setTimeout(async () => {
if (!(await permissionFn())) {
// wait for the next interval
this.repeatDelay(fn, permissionFn);
return;
}
await fn();
this.setTimeout(fn);
this.repeatDelay(fn, permissionFn);
}, this.delay);
}

start(fn: MonitorFn) {
this.setTimeout(fn);
private repeatDelay(fn: MonitorFn, runPermissionFn: PermissionFn) {
this.setTimeout(fn, runPermissionFn);
}

start(fn: MonitorFn, runPermissionFn: PermissionFn) {
this.setTimeout(fn, runPermissionFn);
}

stop() {
Expand Down

0 comments on commit f36a055

Please sign in to comment.