From 9f2bcd751cf0309968196e389ee0c4ff115b4c68 Mon Sep 17 00:00:00 2001 From: Johannes Millan Date: Thu, 22 Jul 2021 13:18:02 +0200 Subject: [PATCH] feat(localSync): prepare electron interface #690 --- electron/backup.ts | 4 +- electron/ipc-events.const.ts | 4 ++ electron/local-file-sync.ts | 53 +++++++++++++++++++ electron/main.ts | 3 ++ .../local-file-sync.service.ts | 37 +++++++++---- 5 files changed, 90 insertions(+), 11 deletions(-) create mode 100644 electron/local-file-sync.ts diff --git a/electron/backup.ts b/electron/backup.ts index dbf5a5b1bf..67ae5eb901 100644 --- a/electron/backup.ts +++ b/electron/backup.ts @@ -16,7 +16,7 @@ import { error, log } from 'electron-log'; let BACKUP_DIR = `${app.getPath('userData')}/backups`; // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -export function initBackupAdapter(backupDir: string) { +export function initBackupAdapter(backupDir: string): void { BACKUP_DIR = backupDir; console.log('Saving backups to', BACKUP_DIR); log('Saving backups to', BACKUP_DIR); @@ -59,7 +59,7 @@ export function initBackupAdapter(backupDir: string) { } // eslint-disable-next-line prefer-arrow/prefer-arrow-functions -function backupData(ev, data) { +function backupData(ev, data): void { if (!existsSync(BACKUP_DIR)) { mkdirSync(BACKUP_DIR); } diff --git a/electron/ipc-events.const.ts b/electron/ipc-events.const.ts index 89379633b7..8024ae9e8f 100644 --- a/electron/ipc-events.const.ts +++ b/electron/ipc-events.const.ts @@ -47,6 +47,10 @@ export enum IPC { UNREGISTER_BEFORE_CLOSE = 'UNREGISTER_BEFORE_CLOSE', BEFORE_CLOSE_DONE = 'BEFORE_CLOSE_DONE', + FILE_SYNC_LOAD = 'FILE_SYNC_LOAD', + FILE_SYNC_SAVE = 'FILE_SYNC_SAVE', + FILE_SYNC_GET_REV_AND_CLIENT_UPDATE = 'FILE_SYNC_GET_REV_AND_CLIENT_UPDATE', + // maybe_UPDATE_CURRENT_TASK = 'UPDATE_CURRENT_TASK', // maybe_IS_IDLE = 'IS_IDLE', // maybe_IS_BUSY = 'IS_BUSY', diff --git a/electron/local-file-sync.ts b/electron/local-file-sync.ts new file mode 100644 index 0000000000..5f1ee9b5f3 --- /dev/null +++ b/electron/local-file-sync.ts @@ -0,0 +1,53 @@ +import { answerRenderer } from './better-ipc'; +import { IPC } from './ipc-events.const'; +import { AppDataComplete, SyncGetRevResult } from '../src/app/imex/sync/sync.model'; + +export const initLocalFileSyncAdapter = (): void => { + answerRenderer( + IPC.FILE_SYNC_SAVE, + ( + ev: any, + { + filePath, + data, + localRev, + }: { filePath: string; data: AppDataComplete; localRev: string | null }, + ): string | Error => { + // TODO kill existing watcher before and kill after + console.log(ev); + console.log(filePath, data, localRev); + return undefined; + // if (!existsSync(BACKUP_DIR)) { + // return false; + // } + // const files = readdirSync(BACKUP_DIR); + // if (!files.length) { + // return false; + // } + }, + ); + + answerRenderer( + IPC.FILE_SYNC_GET_REV_AND_CLIENT_UPDATE, + ( + ev: any, + { filePath, localRev }: { filePath: string; localRev: string | null }, + ): { rev: string; clientUpdate?: number } | SyncGetRevResult => { + console.log(ev); + console.log(filePath, localRev); + return undefined; + }, + ); + + answerRenderer( + IPC.FILE_SYNC_LOAD, + ( + ev: any, + { filePath, localRev }: { filePath: string; localRev: string | null }, + ): { rev: string; data: AppDataComplete | undefined } => { + console.log(ev); + console.log(filePath, localRev); + return undefined; + }, + ); +}; diff --git a/electron/main.ts b/electron/main.ts index 245dff3d4d..4f07c892d8 100644 --- a/electron/main.ts +++ b/electron/main.ts @@ -22,6 +22,7 @@ import { errorHandlerWithFrontendInform } from './error-handler-with-frontend-in import { initDebug } from './debug'; import { IPC } from './ipc-events.const'; import { initBackupAdapter } from './backup'; +import { initLocalFileSyncAdapter } from './local-file-sync'; import { JiraCfg } from '../src/app/features/issue/providers/jira/jira.model'; import lockscreen from './lockscreen'; import { lazySetInterval } from './lazy-set-interval'; @@ -120,6 +121,8 @@ appIN.on('certificate-error', (event, webContents, url, err, certificate, callba // ------------------- appIN.on('ready', createMainWin); appIN.on('ready', () => initBackupAdapter(BACKUP_DIR)); +appIN.on('ready', () => initLocalFileSyncAdapter()); + if (!isDisableTray) { appIN.on('ready', createIndicator); } diff --git a/src/app/imex/sync/local-file-sync/local-file-sync.service.ts b/src/app/imex/sync/local-file-sync/local-file-sync.service.ts index 9655336903..a05c7fc7f5 100644 --- a/src/app/imex/sync/local-file-sync/local-file-sync.service.ts +++ b/src/app/imex/sync/local-file-sync/local-file-sync.service.ts @@ -3,6 +3,9 @@ import { SyncProvider, SyncProviderServiceInterface } from '../sync-provider.mod import { Observable, of } from 'rxjs'; import { IS_ELECTRON } from '../../../app.constants'; import { AppDataComplete, SyncGetRevResult } from '../sync.model'; +import { IPC } from '../../../../../electron/ipc-events.const'; +import { ElectronService } from '../../../core/electron/electron.service'; +import { first } from 'rxjs/operators'; @Injectable({ providedIn: 'root', @@ -12,14 +15,24 @@ export class LocalFileSyncService implements SyncProviderServiceInterface { isUploadForcePossible?: boolean; isReady$: Observable = of(IS_ELECTRON); - constructor() {} + private _filePath$: Observable = of( + 'TEST_PAAAAAAAAAAAAAAAAATH/asd/sync.json', + ); + private _filePathOnce$: Observable = this._filePath$.pipe(first()); + + constructor(private _electronService: ElectronService) {} async getRevAndLastClientUpdate( localRev: string | null, ): Promise<{ rev: string; clientUpdate?: number } | SyncGetRevResult> { - return { - rev: 'asd', - }; + const filePath = await this._filePathOnce$.toPromise(); + return this._electronService.callMain(IPC.FILE_SYNC_GET_REV_AND_CLIENT_UPDATE, { + filePath, + localRev, + }) as Promise<{ + rev: string; + clientUpdate?: number; + }>; } async uploadAppData( @@ -27,15 +40,21 @@ export class LocalFileSyncService implements SyncProviderServiceInterface { localRev: string | null, isForceOverwrite?: boolean, ): Promise { - return 'asd'; + const filePath = await this._filePathOnce$.toPromise(); + return this._electronService.callMain(IPC.FILE_SYNC_SAVE, { + localRev, + filePath, + data, + }) as Promise; } async downloadAppData( localRev: string | null, ): Promise<{ rev: string; data: AppDataComplete | undefined }> { - return { - rev: 'asd', - data: undefined, - }; + const filePath = await this._filePathOnce$.toPromise(); + return this._electronService.callMain(IPC.FILE_SYNC_LOAD, { + localRev, + filePath, + }) as Promise<{ rev: string; data: AppDataComplete | undefined }>; } }