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
14 changes: 5 additions & 9 deletions src/services/sockets/socket.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,12 @@ describe('RealtimeService', () => {
});

test.each([
{ env: 'production', reconnection: true, withCredentials: true, logs: false },
{ env: 'development', reconnection: true, withCredentials: false, logs: true },
{ isProduction: true, reconnection: true, withCredentials: true, logs: false },
{ isProduction: false, reconnection: true, withCredentials: false, logs: true },
])(
'When running in $env environment, then it adjusts reconnection=$reconnection, withCredentials=$withCredentials and logging=$logs',
({ env, reconnection, withCredentials, logs }) => {
vi.spyOn(envService, 'getVariable').mockImplementation((key: string) => {
if (key === 'nodeEnv') return env;
if (key === 'notifications') return 'https://notifications.example.com';
return '';
});
'When running in isProduction=$isProduction environment, then it adjusts reconnection=$reconnection, withCredentials=$withCredentials and logging=$logs',
({ isProduction, reconnection, withCredentials, logs }) => {
vi.spyOn(envService, 'isProduction').mockReturnValue(isProduction);

service.init();

Expand Down
24 changes: 9 additions & 15 deletions src/services/sockets/socket.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export default class RealtimeService {
}

init(onConnected?: () => void): void {
if (!isProduction()) {
if (!envService.isProduction()) {
console.log('[REALTIME]: CONNECTING...');
}

Expand All @@ -27,20 +27,18 @@ export default class RealtimeService {
token: getToken(),
},
reconnection: true,
withCredentials: isProduction(),
withCredentials: envService.isProduction(),
});

this.socket.on('connect', () => {
if (!isProduction()) {
if (!envService.isProduction()) {
console.log('[REALTIME]: CONNECTED WITH ID', this.socket?.id);
}
onConnected?.();
});

this.socket.on('event', (data) => {
if (!isProduction()) {
console.log('[REALTIME] EVENT RECEIVED:', JSON.stringify(data, null, 2));
}
console.log('[REALTIME] EVENT RECEIVED:', JSON.stringify(data, null, 2));

this.eventHandlers.forEach((handler) => {
try {
Expand All @@ -52,13 +50,13 @@ export default class RealtimeService {
});

this.socket.on('disconnect', (reason) => {
if (!isProduction()) {
if (!envService.isProduction()) {
console.log('[REALTIME] DISCONNECTED:', reason);
}
});

this.socket.on('connect_error', (error) => {
if (!isProduction()) console.error('[REALTIME] CONNECTION ERROR:', error);
if (!envService.isProduction()) console.error('[REALTIME] CONNECTION ERROR:', error);
});
}

Expand All @@ -70,22 +68,22 @@ export default class RealtimeService {
}

onEvent(cb: (data: any) => void): () => void {
if (!isProduction()) {
if (!envService.isProduction()) {
console.log('[REALTIME] Registering event handler. Total handlers:', this.eventHandlers.size + 1);
}

this.eventHandlers.add(cb);

return () => {
if (!isProduction()) {
if (!envService.isProduction()) {
console.log('[REALTIME] Removing event handler. Remaining handlers:', this.eventHandlers.size - 1);
}
this.eventHandlers.delete(cb);
};
}

removeAllListeners() {
if (!isProduction()) {
if (!envService.isProduction()) {
console.log('[REALTIME] Clearing all event handlers');
}
this.eventHandlers.clear();
Expand All @@ -101,10 +99,6 @@ export default class RealtimeService {
}
}

function isProduction(): boolean {
return envService.getVariable('nodeEnv') === 'production';
}

function getToken(): string {
return localStorageService.get('xNewToken') as string;
}
Loading