Skip to content

Commit 5fd3e37

Browse files
committed
CU-868fdadum PR#166 fix
1 parent 388856e commit 5fd3e37

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/services/__tests__/signalr.service.enhanced.test.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { HubConnection, HubConnectionBuilder, LogLevel } from '@microsoft/signalr';
1+
import { HubConnection, HubConnectionBuilder, HubConnectionState, LogLevel } from '@microsoft/signalr';
22

33
import { logger } from '@/lib/logging';
44

@@ -72,6 +72,7 @@ describe('SignalRService - Enhanced Features', () => {
7272
onclose: jest.fn(),
7373
onreconnecting: jest.fn(),
7474
onreconnected: jest.fn(),
75+
state: HubConnectionState.Disconnected,
7576
} as any;
7677

7778
// Mock HubConnectionBuilder
@@ -268,25 +269,26 @@ describe('SignalRService - Enhanced Features', () => {
268269

269270
jest.useFakeTimers();
270271

271-
// Mock the connections map to indicate connection exists
272-
const connectionsMap = (service as any).connections;
273-
const originalHas = connectionsMap.has;
274-
connectionsMap.has = jest.fn().mockReturnValue(true);
272+
// Mock connection state to be Connected
273+
Object.defineProperty(mockConnection, 'state', {
274+
value: HubConnectionState.Connected,
275+
writable: true,
276+
});
275277

276-
// Trigger connection close
278+
// Clear previous logs to isolate subsequent logging
279+
jest.clearAllMocks();
280+
281+
// Trigger connection close to schedule a reconnect
277282
onCloseCallback();
278283

279-
// Fast forward time
284+
// Fast forward time to trigger the scheduled reconnect
280285
jest.advanceTimersByTime(5000);
281286

282287
// Should log skip message
283288
expect(mockLogger.debug).toHaveBeenCalledWith({
284289
message: `Hub ${mockConfig.name} is already connected, skipping reconnection attempt`,
285290
});
286291

287-
// Restore original method
288-
connectionsMap.has = originalHas;
289-
290292
jest.useRealTimers();
291293
});
292294

src/services/signalr.service.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { type HubConnection, HubConnectionBuilder, LogLevel } from '@microsoft/signalr';
1+
import { type HubConnection, HubConnectionBuilder, HubConnectionState, LogLevel } from '@microsoft/signalr';
22

33
import { Env } from '@/lib/env';
44
import { logger } from '@/lib/logging';
@@ -337,16 +337,20 @@ class SignalRService {
337337
return;
338338
}
339339

340-
// Check if connection was re-established during the delay
341-
if (this.connections.has(hubName)) {
340+
// If a live connection exists, skip; if it's stale/closed, drop it
341+
const existingConn = this.connections.get(hubName);
342+
if (existingConn && existingConn.state === HubConnectionState.Connected) {
342343
logger.debug({
343344
message: `Hub ${hubName} is already connected, skipping reconnection attempt`,
344345
});
345346
return;
346347
}
347348

348-
// Set reconnecting flag to indicate this hub is in the process of reconnecting
349+
// Mark reconnecting and remove stale entry (if any) to allow a fresh connect
349350
this.reconnectingHubs.add(hubName);
351+
if (existingConn) {
352+
this.connections.delete(hubName);
353+
}
350354

351355
try {
352356
// Refresh authentication token before reconnecting

0 commit comments

Comments
 (0)