diff --git a/src/apps/main/remote-sync/handlers.ts b/src/apps/main/remote-sync/handlers.ts index c4462f76c..ce5f2a807 100644 --- a/src/apps/main/remote-sync/handlers.ts +++ b/src/apps/main/remote-sync/handlers.ts @@ -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'; @@ -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) => { @@ -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; +}); diff --git a/src/apps/sync-engine/BindingManager.ts b/src/apps/sync-engine/BindingManager.ts index 503b8690a..0a8968d94 100644 --- a/src/apps/sync-engine/BindingManager.ts +++ b/src/apps/sync-engine/BindingManager.ts @@ -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)); } diff --git a/src/context/virtual-drive/shared/application/PollingMonitorStart.ts b/src/context/virtual-drive/shared/application/PollingMonitorStart.ts index 6c0f2918c..82ff9ca94 100644 --- a/src/context/virtual-drive/shared/application/PollingMonitorStart.ts +++ b/src/context/virtual-drive/shared/application/PollingMonitorStart.ts @@ -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; } } diff --git a/src/context/virtual-drive/shared/domain/PollingMonitor.ts b/src/context/virtual-drive/shared/domain/PollingMonitor.ts index ef232677a..3972e566c 100644 --- a/src/context/virtual-drive/shared/domain/PollingMonitor.ts +++ b/src/context/virtual-drive/shared/domain/PollingMonitor.ts @@ -1,4 +1,5 @@ export type MonitorFn = () => Promise; +export type PermissionFn = () => Promise; export class PollingMonitor { constructor(private readonly delay: number) {} @@ -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() {