Skip to content
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
17 changes: 3 additions & 14 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ import useBeforeUnload from './hooks/useBeforeUnload';
import useVpnAuth from './hooks/useVpnAuth';

import workerUrl from 'pdfjs-dist/build/pdf.worker.min.mjs?raw';
import { eventHandler } from 'services/sockets/event-handler.service';
import RealtimeService from 'services/sockets/socket.service';
import { EventHandler } from 'services/sockets/event-handler.service';
const blob = new Blob([workerUrl], { type: 'application/javascript' });
pdfjs.GlobalWorkerOptions.workerSrc = URL.createObjectURL(blob);

Expand Down Expand Up @@ -90,17 +90,6 @@ const App = (props: AppProps): JSX.Element => {
i18next.changeLanguage();
}, []);

useEffect(() => {
try {
const realtimeService = RealtimeService.getInstance();
const cleanup = realtimeService.onEvent(eventHandler.onPlanUpdated);

return cleanup;
} catch (err) {
errorService.reportError(err);
}
}, []);

useEffect(() => {
if (!isWorkspaceIdParam) {
navigationService.resetB2BWorkspaceCredentials(dispatch);
Expand Down Expand Up @@ -132,8 +121,6 @@ const App = (props: AppProps): JSX.Element => {

await domainManager.fetchDomains();

RealtimeService.getInstance().init();

dispatch(workspaceThunks.fetchWorkspaces());
navigationService.setWorkspaceFromParams(workspaceThunks, dispatch, false);

Expand All @@ -142,6 +129,8 @@ const App = (props: AppProps): JSX.Element => {
redirectToLogin: !!currentRouteConfig?.auth,
}),
);

RealtimeService.getInstance().init(EventHandler.instance);
} catch (err: unknown) {
const error = errorService.castError(err);
errorService.reportError(error);
Expand Down
23 changes: 16 additions & 7 deletions src/services/sockets/event-handler.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ import { DriveItemData } from 'app/drive/types';
import { store } from 'app/store';
import { planActions, planThunks } from 'app/store/slices/plan';
import { storageActions } from 'app/store/slices/storage';
import storageSelectors from 'app/store/slices/storage/storage.selectors';

vi.mock('app/store', () => ({
store: {
dispatch: vi.fn(),
getState: vi.fn(),
},
}));

Expand All @@ -18,9 +20,6 @@ vi.mock('app/store/slices/plan', () => ({
},
planThunks: {
fetchLimitThunk: vi.fn(() => ({ type: 'plan/fetchLimitThunk' })),
fetchUsageThunk: vi.fn(() => ({ type: 'plan/fetchUsageThunk' })),
fetchSubscriptionThunk: vi.fn(() => ({ type: 'plan/fetchSubscriptionThunk' })),
fetchBusinessLimitUsageThunk: vi.fn(() => ({ type: 'plan/fetchBusinessLimitUsageThunk' })),
},
}));

Expand All @@ -30,6 +29,12 @@ vi.mock('app/store/slices/storage', () => ({
},
}));

vi.mock('app/store/slices/storage/storage.selectors', () => ({
default: {
currentFolderId: vi.fn(),
},
}));

describe('Event Handler', () => {
let eventHandler: EventHandler;
let consoleLogSpy: ReturnType<typeof vi.spyOn>;
Expand All @@ -52,7 +57,7 @@ describe('Event Handler', () => {
},
};

eventHandler.onPlanUpdated(eventData);
eventHandler.handleEvent(eventData);

expect(planActions.updatePlanLimit).toHaveBeenCalledWith(eventData.payload.maxSpaceBytes);
expect(store.dispatch).toHaveBeenCalledWith({
Expand All @@ -72,7 +77,7 @@ describe('Event Handler', () => {
},
};

eventHandler.onPlanUpdated(eventData);
eventHandler.handleEvent(eventData);

expect(store.dispatch).toHaveBeenCalledWith(planThunks.fetchLimitThunk());
});
Expand Down Expand Up @@ -110,6 +115,8 @@ describe('Event Handler', () => {
};

test('When a file is created, then it should push item to storage', () => {
vi.mocked(storageSelectors.currentFolderId).mockReturnValue('folder-123');

const eventData: EventData = {
event: SOCKET_EVENTS.FILE_CREATED,
email: '[email protected]',
Expand All @@ -118,7 +125,7 @@ describe('Event Handler', () => {
payload: mockFileItem as unknown as DriveItemData,
};

eventHandler.onFileCreated(eventData, 'folder-123');
eventHandler.handleEvent(eventData);

expect(storageActions.pushItems).toHaveBeenCalledWith({
updateRecents: true,
Expand All @@ -136,6 +143,8 @@ describe('Event Handler', () => {
});

test('When a file is created but the folder id does not match, then should not push the item', () => {
vi.mocked(storageSelectors.currentFolderId).mockReturnValue('different-folder-123');

const eventData: EventData = {
event: SOCKET_EVENTS.FILE_CREATED,
email: '[email protected]',
Expand All @@ -144,7 +153,7 @@ describe('Event Handler', () => {
payload: mockFileItem as unknown as DriveItemData,
};

eventHandler.onFileCreated(eventData, 'different-folder-123');
eventHandler.handleEvent(eventData);

expect(consoleLogSpy).toHaveBeenCalledWith('[Event Handler] Handling created file:', {
itemFolderId: 'folder-123',
Expand Down
21 changes: 17 additions & 4 deletions src/services/sockets/event-handler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,23 @@ import { planActions, planThunks } from 'app/store/slices/plan';
import { EventData, SOCKET_EVENTS } from './types/socket.types';
import { store } from 'app/store';
import { storageActions } from 'app/store/slices/storage';
import storageSelectors from 'app/store/slices/storage/storage.selectors';

export class EventHandler {
public onPlanUpdated(data: EventData) {
static readonly instance: EventHandler = new EventHandler();

public handleEvent(data: EventData) {
switch (data.event) {
case SOCKET_EVENTS.PLAN_UPDATED:
this.onPlanUpdated(data);
break;
case SOCKET_EVENTS.FILE_CREATED:
this.onFileCreated(data);
break;
}
}

private onPlanUpdated(data: EventData) {
if (data.event !== SOCKET_EVENTS.PLAN_UPDATED) return;
const newLimit = data.payload?.maxSpaceBytes;
console.log('[Event Handler] Updating plan limit: ', newLimit);
Expand All @@ -16,9 +30,10 @@ export class EventHandler {
}
}

public onFileCreated(data: EventData, currentFolderId: string) {
private onFileCreated(data: EventData) {
if (data.event !== SOCKET_EVENTS.FILE_CREATED) return;
const item = data.payload;
const currentFolderId = storageSelectors.currentFolderId(store.getState());

console.log('[Event Handler] Handling created file:', {
itemFolderId: item.folderUuid,
Expand All @@ -37,5 +52,3 @@ export class EventHandler {
);
}
}

export const eventHandler = new EventHandler();
Loading
Loading