Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[_]: Feat/fallback permission #477

Merged
merged 7 commits into from
Apr 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/apps/main/preload.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,5 +168,6 @@ declare interface Window {
openUrl: (url: string) => Promise<void>;
getPreferredAppLanguage: () => Promise<Array<string>>;
syncManually: () => Promise<void>;
getRecentlywasSyncing: () => Promise<boolean>;
};
}
4 changes: 4 additions & 0 deletions src/apps/main/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,5 +299,9 @@ contextBridge.exposeInMainWorld('electron', {
syncManually() {
return ipcRenderer.invoke('SYNC_MANUALLY');
},
getRecentlywasSyncing() {
return ipcRenderer.invoke('CHECK_SYNC_IN_PROGRESS');
},

path,
});
27 changes: 27 additions & 0 deletions src/apps/main/remote-sync/RemoteSyncManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
RemoteSyncedFile,
SyncConfig,
SYNC_OFFSET_MS,
WAITING_AFTER_SYNCING
} from './helpers';
import { reportError } from '../bug-report/service';

Expand All @@ -24,6 +25,8 @@ export class RemoteSyncManager {
> = [];
private totalFilesSynced = 0;
private totalFoldersSynced = 0;
private lastSyncingFinishedTimestamp: Date | null = null;

constructor(
private db: {
files: DatabaseCollectionAdapter<DriveFile>;
Expand All @@ -47,10 +50,15 @@ export class RemoteSyncManager {
if (typeof callback !== 'function') return;
this.onStatusChangeCallbacks.push(callback);
}

getSyncStatus(): RemoteSyncStatus {
return this.status;
}

private getLastSyncingFinishedTimestamp() {
return this.lastSyncingFinishedTimestamp;
}

/**
* Check if the RemoteSyncManager is in SYNCED status
*
Expand All @@ -60,10 +68,22 @@ export class RemoteSyncManager {
return this.status === 'SYNCED';
}

/**
* Consult if recently the RemoteSyncManager was syncing
* @returns True if the RemoteSyncManager was syncing recently
* @returns False if the RemoteSyncManager was not syncing recently
*/
recentlyWasSyncing() {
const passedTime = Date.now() - ( this.getLastSyncingFinishedTimestamp()?.getTime() ?? Date.now() );
return passedTime < WAITING_AFTER_SYNCING;
}

resetRemoteSync() {
this.changeStatus('IDLE');
this.filesSyncStatus = 'IDLE';
this.foldersSyncStatus = 'IDLE';
this._placeholdersStatus = 'IDLE';
this.lastSyncingFinishedTimestamp = null;
this.totalFilesSynced = 0;
this.totalFoldersSynced = 0;
}
Expand Down Expand Up @@ -148,6 +168,7 @@ export class RemoteSyncManager {
return true;
}
private changeStatus(newStatus: RemoteSyncStatus) {
this.addLastSyncingFinishedTimestamp();
if (newStatus === this.status) return;
Logger.info(`RemoteSyncManager ${this.status} -> ${newStatus}`);
this.status = newStatus;
Expand All @@ -157,6 +178,12 @@ export class RemoteSyncManager {
});
}

private addLastSyncingFinishedTimestamp() {
if (this.status !== 'SYNCING') return;
Logger.info('Adding last syncing finished timestamp');
this.lastSyncingFinishedTimestamp = new Date();
}

private checkRemoteSyncStatus() {
if (this._placeholdersStatus === 'SYNCING') {
this.changeStatus('SYNCING');
Expand Down
20 changes: 11 additions & 9 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 @@ -120,16 +120,18 @@ 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) => {
await sleep(2_000);
Logger.info('[SYNC ENGINE] Changing status');
remoteSyncManager.placeholderStatus = 'SYNCING';
await sleep(7_00);
Logger.info('[SYNC ENGINE] Changing status', placeholderStates);
await sleep(5_000);
remoteSyncManager.placeholderStatus = placeholderStates;
});

ipcMain.handle('CHECK_SYNC_IN_PROGRESS', async () => {
const syncingStatus: RemoteSyncStatus = 'SYNCING';
const isSyncing = remoteSyncManager.getSyncStatus() === syncingStatus;
const recentlySyncing = remoteSyncManager.recentlyWasSyncing();
return isSyncing || recentlySyncing; // If it's syncing or recently was syncing
});
1 change: 1 addition & 0 deletions src/apps/main/remote-sync/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ export type SyncConfig = {
};

export const SYNC_OFFSET_MS = 0;
export const WAITING_AFTER_SYNCING = 1000 * 60 * 3;

export const lastSyncedAtIsNewer = (
itemUpdatedAt: Date,
Expand Down
24 changes: 18 additions & 6 deletions src/apps/renderer/pages/Widget/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import useUsage from '../../hooks/useUsage';
import useVirtualDriveStatus from '../../hooks/VirtualDriveStatus';
import { reportError } from '../../utils/errors';


export default function Header() {
const { translate } = useTranslationContext();
const { virtualDriveCanBeOpened } = useVirtualDriveStatus();
Expand Down Expand Up @@ -45,10 +46,19 @@ export default function Header() {
window.electron.quit();
}

function onSyncClick() {
const wasSyncing = () => {
return window.electron.getRecentlywasSyncing();
};

async function onSyncClick() {
const notAllowed = await wasSyncing();
if (notAllowed) {
return;
}
window.electron.syncManually();
}


const handleOpenURL = async (URL: string) => {
try {
await window.electron.openUrl(URL);
Expand Down Expand Up @@ -200,13 +210,15 @@ export default function Header() {
)}
</Menu.Item>
<Menu.Item>
{({ active }) => (
<div>
<DropdownItem active={active} onClick={onSyncClick}>
{({active}) => {

return (<div>
<DropdownItem active={active} onClick={onSyncClick} >
<span>{translate('widget.header.dropdown.sync')}</span>
</DropdownItem>
</div>
)}
</div>);
}
}
</Menu.Item>
<Menu.Item>
{({ active }) => (
Expand Down
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
Expand Up @@ -27,16 +27,16 @@ export class FileCheckerStatusInRoot {

const ps = placeholderStatus.pinState;
const ss = placeholderStatus.syncState;
const status =
const notSynced =
ps &&
ss &&
ps !== PinState.AlwaysLocal &&
ps !== PinState.OnlineOnly &&
ss !== SyncState.InSync;

if (status) {
if (notSynced) {
Logger.debug(
`[File Checker Status In Root] item ${path} with status: ${status}`
`[File Checker Status In Root] item ${path} with status: ${notSynced}`
);
finalStatus = 'SYNC_PENDING';
break;
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('[START FALLBAK] Starting fallback sync...');

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('[START FALLBAK] Not permitted to start fallback sync: ', 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
Loading