Skip to content

Commit

Permalink
reload sync engine on realtime sync changes
Browse files Browse the repository at this point in the history
  • Loading branch information
larryrider committed Sep 26, 2023
1 parent f048532 commit fc50401
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/main/background-processes/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ export async function stopSyncEngineWatcher() {
}
}

export function updateSyncEngine() {
try {
worker?.webContents.send('UPDATE_SYNC_ENGINE_PROCESS');
} catch (err) {
// TODO: handle error
Logger.error(err);
}
}

eventBus.on('USER_LOGGED_OUT', stopSyncEngineWatcher);
eventBus.on('USER_WAS_UNAUTHORIZED', stopSyncEngineWatcher);
eventBus.on('INITIAL_SYNC_READY', spawnSyncEngineWorker);
8 changes: 6 additions & 2 deletions src/main/remote-sync/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { ipcMain } from 'electron';
import { reportError } from '../bug-report/service';
import { sleep } from '../util';
import { broadcastToWindows } from '../windows';
import { updateSyncEngine } from '../background-processes/sync-engine';

let initialSyncReady = false;
const driveFilesCollection = new DriveFilesCollection();
Expand All @@ -27,7 +28,7 @@ const remoteSyncManager = new RemoteSyncManager(
}
);

export async function getUpdtaedRemoteItems() {
export async function getUpdatedRemoteItems() {
try {
const [allDriveFiles, allDriveFolders] = await Promise.all([
driveFilesCollection.getAll(),
Expand All @@ -54,7 +55,7 @@ export async function getUpdtaedRemoteItems() {

ipcMain.handle('GET_UPDATED_REMOTE_ITEMS', async () => {
Logger.debug('[MAIN] Getting updated remote items');
return getUpdtaedRemoteItems();
return getUpdatedRemoteItems();
});

export function startRemoteSync(): Promise<void> {
Expand All @@ -81,8 +82,11 @@ eventBus.on('RECEIVED_REMOTE_CHANGES', async () => {
// that we received the notification, but if we check
// for new data we don't receive it
await sleep(500);

await remoteSyncManager.startRemoteSync();
updateSyncEngine();
});

eventBus.on('USER_LOGGED_IN', async () => {
Logger.info('Received user logged in event');

Expand Down
4 changes: 4 additions & 0 deletions src/workers/sync-engine/BindingManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@ export class BindingsManager {
async stop() {
await this.drive.disconnectSyncRoot();
}

cleanUp() {
VirtualDrive.unregisterSyncRoot(this.paths.root);
}
}
18 changes: 18 additions & 0 deletions src/workers/sync-engine/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,24 @@ async function setUp() {
event.sender.send('SYNC_ENGINE_STOP_SUCCESS');
});

ipcRenderer.on('UPDATE_SYNC_ENGINE_PROCESS', async () => {
Logger.info('[SYNC ENGINE] Updating sync engine');

await bindings.stop();
bindings.cleanUp();

await bindings.start(
packageJson.version,
'{E9D7EB38-B229-5DC5-9396-017C449D59CD}'
);

const tree = await container.treeBuilder.run();

bindings.createPlaceHolders(tree);

Logger.info('[SYNC ENGINE] sync engine updated successfully');
});

await bindings.start(
packageJson.version,
'{E9D7EB38-B229-5DC5-9396-017C449D59CD}'
Expand Down

0 comments on commit fc50401

Please sign in to comment.