Skip to content

Commit

Permalink
feat: create backgorund process messenger
Browse files Browse the repository at this point in the history
  • Loading branch information
JoanVicens committed Jan 17, 2024
1 parent 687914d commit e936ff6
Show file tree
Hide file tree
Showing 15 changed files with 218 additions and 41 deletions.
5 changes: 4 additions & 1 deletion src/apps/fuse/callbacks/MakeDirectoryCallback.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ export class MakeDirectoryCallback extends NotifyFuseCallback {
} catch (err: unknown) {
const error = this.resolveError({ path, mode }, err);

this.container.syncFolderMessenger.error(path, 'unknown error');
this.container.syncFolderMessenger.errorWhileCreating(
path,
'unknown error'
);

return this.left(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { SameFileWasMoved } from '../../../../../context/virtual-drive/files/app
import { File } from '../../../../../context/virtual-drive/files/domain/File';
import { FuseLocalFileSystem } from '../../../../../context/virtual-drive/files/infrastructure/FuseLocalFileSystem';
import { InMemoryFileRepository } from '../../../../../context/virtual-drive/files/infrastructure/InMemoryFileRepository';
import { MainProcessSyncFileMessenger } from '../../../../../context/virtual-drive/files/infrastructure/MainProcessSyncFileMessenger';
import { MainProcessSyncFileMessenger } from '../../../../../context/virtual-drive/files/infrastructure/SyncFileMessengers/MainProcessSyncFileMessenger';
import { SDKRemoteFileSystem } from '../../../../../context/virtual-drive/files/infrastructure/SDKRemoteFileSystem';
import { DependencyInjectionHttpClientsProvider } from '../../common/clients';
import { DependencyInjectionEventBus } from '../../common/eventBus';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import { Folder } from '../../../../../context/virtual-drive/folders/domain/Fold
import { FuseLocalFileSystem } from '../../../../../context/virtual-drive/folders/infrastructure/FuseLocalFileSystem';
import { HttpRemoteFileSystem } from '../../../../../context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem';
import { InMemoryFolderRepository } from '../../../../../context/virtual-drive/folders/infrastructure/InMemoryFolderRepository';
import { MainProcessSyncFolderMessenger } from '../../../../../context/virtual-drive/folders/infrastructure/MainProcessSyncFolderMessenger';
import { MainProcessSyncFolderMessenger } from '../../../../../context/virtual-drive/folders/infrastructure/SyncMessengers/MainProcessSyncFolderMessenger';
import { DependencyInjectionHttpClientsProvider } from '../../common/clients';
import { DependencyInjectionEventBus } from '../../common/eventBus';

Expand Down
6 changes: 6 additions & 0 deletions src/apps/shared/IPC/events/sync-engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,15 @@ type FileUpdatePayload = {
export type FolderEvents = {
FOLDER_CREATING: (payload: { name: string }) => void;
FOLDER_CREATED: (payload: { name: string }) => void;
FOLDER_CREATION_ERROR: (payload: { name: string; error: string }) => void;

FOLDER_RENAMING: (payload: { oldName: string; newName: string }) => void;
FOLDER_RENAMED: (payload: { oldName: string; newName: string }) => void;
FOLDER_RENAME_ERROR: (payload: {
oldName: string;
newName: string;
error: string;
}) => void;
};

export type FilesEvents = {
Expand Down
26 changes: 15 additions & 11 deletions src/apps/sync-engine/dependency-injection/files/builder.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
import crypt from '../../../../context/shared/infrastructure/crypt';
import { DependencyInjectionEventBus } from '../common/eventBus';
import { DependencyInjectionEventRepository } from '../common/eventRepository';
import { DependencyInjectionUserProvider } from '../common/user';
import { DependencyInjectionVirtualDrive } from '../common/virtualDrive';
import { FoldersContainer } from '../folders/FoldersContainer';
import { SharedContainer } from '../shared/SharedContainer';
import { FilesContainer } from './FilesContainer';
import { DependencyInjectionStorageSdk } from '../common/sdk';
import { CreateFilePlaceholderOnDeletionFailed } from '../../../../context/virtual-drive/files/application/CreateFilePlaceholderOnDeletionFailed';
import { FileCreator } from '../../../../context/virtual-drive/files/application/FileCreator';
import { FileDeleter } from '../../../../context/virtual-drive/files/application/FileDeleter';
Expand All @@ -19,11 +11,20 @@ import { RepositoryPopulator } from '../../../../context/virtual-drive/files/app
import { RetrieveAllFiles } from '../../../../context/virtual-drive/files/application/RetrieveAllFiles';
import { SameFileWasMoved } from '../../../../context/virtual-drive/files/application/SameFileWasMoved';
import { InMemoryFileRepository } from '../../../../context/virtual-drive/files/infrastructure/InMemoryFileRepository';
import { SDKRemoteFileSystem } from '../../../../context/virtual-drive/files/infrastructure/SDKRemoteFileSystem';
import { NodeWinLocalFileSystem } from '../../../../context/virtual-drive/files/infrastructure/NodeWinLocalFileSystem';
import { SDKRemoteFileSystem } from '../../../../context/virtual-drive/files/infrastructure/SDKRemoteFileSystem';
import { BackgroundProcessSyncFileMessenger } from '../../../../context/virtual-drive/files/infrastructure/SyncFileMessengers/BackgroundProcessSyncFileMessenger';
import { LocalFileIdProvider } from '../../../../context/virtual-drive/shared/application/LocalFileIdProvider';
import { MainProcessSyncFileMessenger } from '../../../../context/virtual-drive/files/infrastructure/MainProcessSyncFileMessenger';
import { ipcRendererSyncEngine } from '../../ipcRendererSyncEngine';
import { DependencyInjectionHttpClientsProvider } from '../common/clients';
import { DependencyInjectionEventBus } from '../common/eventBus';
import { DependencyInjectionEventRepository } from '../common/eventRepository';
import { DependencyInjectionStorageSdk } from '../common/sdk';
import { DependencyInjectionUserProvider } from '../common/user';
import { DependencyInjectionVirtualDrive } from '../common/virtualDrive';
import { FoldersContainer } from '../folders/FoldersContainer';
import { SharedContainer } from '../shared/SharedContainer';
import { FilesContainer } from './FilesContainer';

export async function buildFilesContainer(
folderContainer: FoldersContainer,
Expand All @@ -50,7 +51,10 @@ export async function buildFilesContainer(
virtualDrive,
sharedContainer.relativePathToAbsoluteConverter
);
const syncFileMessenger = new MainProcessSyncFileMessenger();

const syncFileMessenger = new BackgroundProcessSyncFileMessenger(
ipcRendererSyncEngine
);

const repository = new InMemoryFileRepository();

Expand Down
9 changes: 6 additions & 3 deletions src/apps/sync-engine/dependency-injection/folders/builder.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { NodeWinLocalFileSystem } from '../../../../context/virtual-drive/folders/infrastructure/NodeWinLocalFileSystem';
import { AllParentFoldersStatusIsExists } from '../../../../context/virtual-drive/folders/application/AllParentFoldersStatusIsExists';
import { FolderByPartialSearcher } from '../../../../context/virtual-drive/folders/application/FolderByPartialSearcher';
import { FolderCreatorFromOfflineFolder } from '../../../../context/virtual-drive/folders/application/FolderCreatorFromOfflineFolder';
Expand All @@ -20,13 +19,15 @@ import { FolderPlaceholderUpdater } from '../../../../context/virtual-drive/fold
import { HttpRemoteFileSystem } from '../../../../context/virtual-drive/folders/infrastructure/HttpRemoteFileSystem';
import { InMemoryFolderRepository } from '../../../../context/virtual-drive/folders/infrastructure/InMemoryFolderRepository';
import { InMemoryOfflineFolderRepository } from '../../../../context/virtual-drive/folders/infrastructure/InMemoryOfflineFolderRepository';
import { NodeWinLocalFileSystem } from '../../../../context/virtual-drive/folders/infrastructure/NodeWinLocalFileSystem';
import { BackgroundProcessSyncFolderMessenger } from '../../../../context/virtual-drive/folders/infrastructure/SyncMessengers/BackgroundProcessSyncFolderMessenger';
import { ipcRendererSyncEngine } from '../../ipcRendererSyncEngine';
import { DependencyInjectionHttpClientsProvider } from '../common/clients';
import { DependencyInjectionEventBus } from '../common/eventBus';
import { DependencyInjectionEventRepository } from '../common/eventRepository';
import { DependencyInjectionVirtualDrive } from '../common/virtualDrive';
import { SharedContainer } from '../shared/SharedContainer';
import { FoldersContainer } from './FoldersContainer';
import { MainProcessSyncFolderMessenger } from '../../../../context/virtual-drive/folders/infrastructure/MainProcessSyncFolderMessenger';

export async function buildFoldersContainer(
shredContainer: SharedContainer
Expand All @@ -38,7 +39,9 @@ export async function buildFoldersContainer(

const repository = new InMemoryFolderRepository();

const syncFolderMessenger = new MainProcessSyncFolderMessenger();
const syncFolderMessenger = new BackgroundProcessSyncFolderMessenger(
ipcRendererSyncEngine
);

const localFileSystem = new NodeWinLocalFileSystem(virtualDrive);
const remoteFileSystem = new HttpRemoteFileSystem(
Expand Down
6 changes: 5 additions & 1 deletion src/context/virtual-drive/files/application/FileCreator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,11 @@ export class FileCreator {

const filePath = new FilePath(path);

await this.notifier.error(filePath.name(), filePath.extension(), message);
await this.notifier.errorWhileCreating(
filePath.name(),
filePath.extension(),
message
);

throw error;
}
Expand Down
12 changes: 8 additions & 4 deletions src/context/virtual-drive/files/domain/SyncFileMessenger.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
export interface SyncFileMessenger {
created(name: string, extension: string): Promise<void>;
error(name: string, extension: string, message: string): Promise<void>;
trashing(name: string, type: string, size: number): Promise<void>;
trashed(name: string, type: string, size: number): Promise<void>;
errorWhileCreating(
name: string,
extension: string,
message: string
): Promise<void>;
trashing(name: string, extension: string, size: number): Promise<void>;
trashed(name: string, extension: string, size: number): Promise<void>;
errorWhileTrashing(
name: string,
type: string,
extension: string,
message: string
): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { SyncEngineIpc } from '../../../../../apps/sync-engine/ipcRendererSyncEngine';
import { SyncMessenger } from '../../../../shared/domain/SyncMessenger';
import { SyncFileMessenger } from '../../domain/SyncFileMessenger';

export class BackgroundProcessSyncFileMessenger
extends SyncMessenger
implements SyncFileMessenger
{
constructor(private readonly ipc: SyncEngineIpc) {
super();
}

async created(name: string, extension: string): Promise<void> {
this.ipc.send('FILE_CREATED', {
name,
extension,
nameWithExtension: this.nameWithExtension(name, extension),
});
}

async errorWhileCreating(
name: string,
extension: string,
message: string
): Promise<void> {
this.ipc.send('FILE_UPLOAD_ERROR', {
name,
extension,
nameWithExtension: this.nameWithExtension(name, extension),
error: message,
});
}

async trashing(name: string, extension: string, size: number): Promise<void> {
this.ipc.send('FILE_DELETING', {
name,
extension,
nameWithExtension: this.nameWithExtension(name, extension),
size,
});
}

async trashed(name: string, extension: string, size: number): Promise<void> {
this.ipc.send('FILE_DELETED', {
name,
extension,
nameWithExtension: this.nameWithExtension(name, extension),
size,
});
}

async errorWhileTrashing(
name: string,
extension: string,
message: string
): Promise<void> {
this.ipc.send('FILE_DELETION_ERROR', {
name,
extension,
nameWithExtension: this.nameWithExtension(name, extension),
error: message,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
trackError,
trackEvent,
} from '../../../../apps/main/analytics/service';
import { addProcessIssue } from '../../../../apps/main/background-processes/process-issues';
import { setTrayStatus } from '../../../../apps/main/tray/tray';
import { broadcastToWindows } from '../../../../apps/main/windows';
import { ProcessIssue } from '../../../../apps/shared/types';
import { SyncMessenger } from '../../../shared/domain/SyncMessenger';
import { SyncFileMessenger } from '../domain/SyncFileMessenger';
} from '../../../../../apps/main/analytics/service';
import { addProcessIssue } from '../../../../../apps/main/background-processes/process-issues';
import { setTrayStatus } from '../../../../../apps/main/tray/tray';
import { broadcastToWindows } from '../../../../../apps/main/windows';
import { ProcessIssue } from '../../../../../apps/shared/types';
import { SyncMessenger } from '../../../../shared/domain/SyncMessenger';
import { SyncFileMessenger } from '../../domain/SyncFileMessenger';

export class MainProcessSyncFileMessenger
extends SyncMessenger
Expand All @@ -22,7 +22,11 @@ export class MainProcessSyncFileMessenger
});
}

async error(name: string, extension: string, message: string): Promise<void> {
async errorWhileCreating(
name: string,
extension: string,
message: string
): Promise<void> {
const nameWithExtension = this.nameWithExtension(name, extension);

setTrayStatus('ALERT');
Expand Down
16 changes: 11 additions & 5 deletions src/context/virtual-drive/folders/domain/SyncFolderMessenger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
export interface SyncFolderMessenger {
creating(name: string): Promise<void>;
created(name: string): Promise<void>;
rename(name: string, newName: string): Promise<void>;
renamed(name: string, newName: string): Promise<void>;
error(name: string, message: string): Promise<void>;
creating(desiredName: string): Promise<void>;
created(desiredName: string): Promise<void>;
errorWhileCreating(desiredName: string, message: string): Promise<void>;

rename(currentName: string, desiredName: string): Promise<void>;
renamed(currentName: string, desiredName: string): Promise<void>;
errorWhileRenaming(
currentName: string,
desiredName: string,
message: string
): Promise<void>;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { SyncEngineIpc } from '../../../../../apps/sync-engine/ipcRendererSyncEngine';
import { SyncMessenger } from '../../../../shared/domain/SyncMessenger';
import { SyncFolderMessenger } from '../../domain/SyncFolderMessenger';

export class BackgroundProcessSyncFolderMessenger
extends SyncMessenger
implements SyncFolderMessenger
{
constructor(private readonly ipc: SyncEngineIpc) {
super();
}

async creating(currentName: string): Promise<void> {
this.ipc.send('FOLDER_CREATING', { name: currentName });
}

async created(currentName: string): Promise<void> {
this.ipc.send('FOLDER_CREATED', { name: currentName });
}

async errorWhileCreating(
currentName: string,
message: string
): Promise<void> {
this.ipc.send('FOLDER_CREATION_ERROR', {
name: currentName,
error: message,
});
}

async rename(currentName: string, desiredName: string): Promise<void> {
this.ipc.send('FOLDER_RENAMING', {
oldName: currentName,
newName: desiredName,
});
}

async renamed(currentName: string, desiredName: string): Promise<void> {
this.ipc.send('FOLDER_RENAMED', {
oldName: currentName,
newName: desiredName,
});
}

async errorWhileRenaming(
currentName: string,
desiredName: string,
message: string
): Promise<void> {
this.ipc.send('FOLDER_RENAME_ERROR', {
oldName: currentName,
newName: desiredName,
error: message,
});
}
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,40 @@
import { trackError } from '../../../../apps/main/analytics/service';
import { setTrayStatus } from '../../../../apps/main/tray/tray';
import { SyncFolderMessenger } from '../domain/SyncFolderMessenger';
import { trackError } from '../../../../../apps/main/analytics/service';
import { setTrayStatus } from '../../../../../apps/main/tray/tray';
import { SyncFolderMessenger } from '../../domain/SyncFolderMessenger';

export class MainProcessSyncFolderMessenger implements SyncFolderMessenger {
async rename(_name: string, _newName: string): Promise<void> {
setTrayStatus('SYNCING');
}

async renamed(_name: string, _newName: string): Promise<void> {
setTrayStatus('IDLE');
}

async errorWhileRenaming(
currentName: string,
_desiredName: string,
message: string
): Promise<void> {
setTrayStatus('ALERT');

trackError('Rename Error', new Error(message), {
itemType: 'Folder',
root: '',
from: currentName,
action: 'Rename',
});
}

async creating(_name: string): Promise<void> {
setTrayStatus('SYNCING');
}

async created(_name: string): Promise<void> {
setTrayStatus('IDLE');
}
async error(name: string, message: string): Promise<void> {

async errorWhileCreating(name: string, message: string): Promise<void> {
setTrayStatus('ALERT');

trackError('Upload Error', new Error(message), {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,11 @@ export class FileSyncNotifierMock implements SyncFileMessenger {
created(name: string, extension: string): Promise<void> {
return this.createdMock(name, extension);
}
error(name: string, extension: string, message: string): Promise<void> {
errorWhileCreating(
name: string,
extension: string,
message: string
): Promise<void> {
return this.errorMock(name, extension, message);
}
trashing(name: string, type: string, size: number): Promise<void> {
Expand Down
Loading

0 comments on commit e936ff6

Please sign in to comment.