Skip to content

Commit 1c6d9c0

Browse files
author
Megha Narayanan
committed
fixed unsubscribe naming.
1 parent 4861a0d commit 1c6d9c0

File tree

5 files changed

+131
-61
lines changed

5 files changed

+131
-61
lines changed

packages/cli/src/commands/sandbox/sandbox-devtools/react-app/src/App.tsx

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -282,17 +282,17 @@ function AppContent() {
282282

283283
// Clean up on unmount
284284
return () => {
285-
unsubscribeSandboxStatus();
286-
unsubscribeConnect();
287-
unsubscribeConnectError();
288-
unsubscribeConnectTimeout();
289-
unsubscribeReconnect();
290-
unsubscribeReconnectAttempt();
291-
unsubscribeReconnectError();
292-
unsubscribeReconnectFailed();
293-
unsubscribeDisconnect();
294-
unsubscribeLog();
295-
stopPing();
285+
unsubscribeSandboxStatus.unsubscribe();
286+
unsubscribeConnect.unsubscribe();
287+
unsubscribeConnectError.unsubscribe();
288+
unsubscribeConnectTimeout.unsubscribe();
289+
unsubscribeReconnect.unsubscribe();
290+
unsubscribeReconnectAttempt.unsubscribe();
291+
unsubscribeReconnectError.unsubscribe();
292+
unsubscribeReconnectFailed.unsubscribe();
293+
unsubscribeDisconnect.unsubscribe();
294+
unsubscribeLog.unsubscribe();
295+
stopPing.unsubscribe();
296296
clearLogs();
297297
sandboxClientService.disconnect();
298298
};

packages/cli/src/commands/sandbox/sandbox-devtools/react-app/src/hooks/useResourceManager.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -123,12 +123,12 @@ export const useResourceManager = (
123123

124124
// Cleanup function to unsubscribe from events
125125
return () => {
126-
unsubscribeSavedResources();
127-
unsubscribeDeployedBackendResources();
128-
unsubscribeCustomFriendlyNames();
129-
unsubscribeCustomFriendlyNameUpdated();
130-
unsubscribeCustomFriendlyNameRemoved();
131-
unsubscribeError();
126+
unsubscribeSavedResources.unsubscribe();
127+
unsubscribeDeployedBackendResources.unsubscribe();
128+
unsubscribeCustomFriendlyNames.unsubscribe();
129+
unsubscribeCustomFriendlyNameUpdated.unsubscribe();
130+
unsubscribeCustomFriendlyNameRemoved.unsubscribe();
131+
unsubscribeError.unsubscribe();
132132
};
133133
}, [resourceClientService, sandboxStatus, onResourcesLoaded]);
134134

packages/cli/src/commands/sandbox/sandbox-devtools/react-app/src/services/resource_client_service.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,9 @@ export class ResourceClientService extends SocketClientService {
7777
* @param handler The event handler
7878
* @returns A function to unsubscribe
7979
*/
80-
public onSavedResources(
81-
handler: (data: BackendResourcesData) => void,
82-
): () => void {
80+
public onSavedResources(handler: (data: BackendResourcesData) => void): {
81+
unsubscribe: () => void;
82+
} {
8383
return this.on(SOCKET_EVENTS.SAVED_RESOURCES, handler);
8484
}
8585

@@ -90,7 +90,7 @@ export class ResourceClientService extends SocketClientService {
9090
*/
9191
public onDeployedBackendResources(
9292
handler: (data: BackendResourcesData) => void,
93-
): () => void {
93+
): { unsubscribe: () => void } {
9494
return this.on(SOCKET_EVENTS.DEPLOYED_BACKEND_RESOURCES, handler);
9595
}
9696

@@ -101,7 +101,7 @@ export class ResourceClientService extends SocketClientService {
101101
*/
102102
public onCustomFriendlyNames(
103103
handler: (data: Record<string, string>) => void,
104-
): () => void {
104+
): { unsubscribe: () => void } {
105105
return this.on(SOCKET_EVENTS.CUSTOM_FRIENDLY_NAMES, handler);
106106
}
107107

@@ -112,7 +112,7 @@ export class ResourceClientService extends SocketClientService {
112112
*/
113113
public onCustomFriendlyNameUpdated(
114114
handler: (data: { resourceId: string; friendlyName: string }) => void,
115-
): () => void {
115+
): { unsubscribe: () => void } {
116116
return this.on(SOCKET_EVENTS.CUSTOM_FRIENDLY_NAME_UPDATED, handler);
117117
}
118118

@@ -123,7 +123,7 @@ export class ResourceClientService extends SocketClientService {
123123
*/
124124
public onCustomFriendlyNameRemoved(
125125
handler: (data: { resourceId: string }) => void,
126-
): () => void {
126+
): { unsubscribe: () => void } {
127127
return this.on(SOCKET_EVENTS.CUSTOM_FRIENDLY_NAME_REMOVED, handler);
128128
}
129129

@@ -132,7 +132,9 @@ export class ResourceClientService extends SocketClientService {
132132
* @param handler The event handler
133133
* @returns A function to unsubscribe
134134
*/
135-
public onError(handler: (data: { message: string }) => void): () => void {
135+
public onError(handler: (data: { message: string }) => void): {
136+
unsubscribe: () => void;
137+
} {
136138
return this.on(SOCKET_EVENTS.ERROR, handler);
137139
}
138140
}

packages/cli/src/commands/sandbox/sandbox-devtools/react-app/src/services/sandbox_client_service.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ export class SandboxClientService extends SocketClientService {
5959
/**
6060
* Registers a handler for sandbox status events
6161
* @param handler The event handler
62-
* @returns A function to unsubscribe
62+
* @returns An object with an unsubscribe method
6363
*/
64-
public onSandboxStatus(
65-
handler: (data: SandboxStatusData) => void,
66-
): () => void {
64+
public onSandboxStatus(handler: (data: SandboxStatusData) => void): {
65+
unsubscribe: () => void;
66+
} {
6767
return this.on(SOCKET_EVENTS.SANDBOX_STATUS, handler);
6868
}
6969

@@ -84,15 +84,15 @@ export class SandboxClientService extends SocketClientService {
8484
/**
8585
* Registers a handler for log events
8686
* @param handler The event handler
87-
* @returns A function to unsubscribe
87+
* @returns An object with an unsubscribe method
8888
*/
8989
public onLog(
9090
handler: (data: {
9191
timestamp: string;
9292
level: string;
9393
message: string;
9494
}) => void,
95-
): () => void {
95+
): { unsubscribe: () => void } {
9696
return this.on('log', handler);
9797
}
9898
}

packages/cli/src/commands/sandbox/sandbox-devtools/react-app/src/services/socket_client_service.ts

Lines changed: 98 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -59,96 +59,155 @@ export class SocketClientService {
5959
* @param handler The event handler
6060
* @returns A function to unsubscribe
6161
*/
62-
public onConnect(handler: ConnectionHandler): () => void {
63-
if (!this.socket) return () => {};
62+
/**
63+
* Registers a handler for connection events
64+
* @param handler The event handler
65+
* @returns An object with an unsubscribe method
66+
*/
67+
public onConnect(handler: ConnectionHandler): { unsubscribe: () => void } {
68+
if (!this.socket) return { unsubscribe: () => {} };
6469
this.socket.on('connect', handler);
65-
return () => this.socket?.off('connect', handler);
70+
return { unsubscribe: () => this.socket?.off('connect', handler) };
6671
}
6772

6873
/**
6974
* Registers a handler for disconnection events
7075
* @param handler The event handler
7176
* @returns A function to unsubscribe
7277
*/
73-
public onDisconnect(handler: (reason: string) => void): () => void {
74-
if (!this.socket) return () => {};
78+
/**
79+
* Registers a handler for disconnection events
80+
* @param handler The event handler
81+
* @returns An object with an unsubscribe method
82+
*/
83+
public onDisconnect(handler: (reason: string) => void): {
84+
unsubscribe: () => void;
85+
} {
86+
if (!this.socket) return { unsubscribe: () => {} };
7587
this.socket.on('disconnect', handler);
76-
return () => this.socket?.off('disconnect', handler);
88+
return { unsubscribe: () => this.socket?.off('disconnect', handler) };
7789
}
7890

7991
/**
8092
* Registers a handler for connection error events
8193
* @param handler The event handler
8294
* @returns A function to unsubscribe
8395
*/
84-
public onConnectError(handler: ErrorHandler): () => void {
85-
if (!this.socket) return () => {};
96+
/**
97+
* Registers a handler for connection error events
98+
* @param handler The event handler
99+
* @returns An object with an unsubscribe method
100+
*/
101+
public onConnectError(handler: ErrorHandler): { unsubscribe: () => void } {
102+
if (!this.socket) return { unsubscribe: () => {} };
86103
this.socket.on('connect_error', handler);
87-
return () => this.socket?.off('connect_error', handler);
104+
return { unsubscribe: () => this.socket?.off('connect_error', handler) };
88105
}
89106

90107
/**
91108
* Registers a handler for connection timeout events
92109
* @param handler The event handler
93110
* @returns A function to unsubscribe
94111
*/
95-
public onConnectTimeout(handler: ConnectionHandler): () => void {
96-
if (!this.socket) return () => {};
112+
/**
113+
* Registers a handler for connection timeout events
114+
* @param handler The event handler
115+
* @returns An object with an unsubscribe method
116+
*/
117+
public onConnectTimeout(handler: ConnectionHandler): {
118+
unsubscribe: () => void;
119+
} {
120+
if (!this.socket) return { unsubscribe: () => {} };
97121
this.socket.on('connect_timeout', handler);
98-
return () => this.socket?.off('connect_timeout', handler);
122+
return { unsubscribe: () => this.socket?.off('connect_timeout', handler) };
99123
}
100124

101125
/**
102126
* Registers a handler for reconnection events
103127
* @param handler The event handler
104128
* @returns A function to unsubscribe
105129
*/
106-
public onReconnect(handler: (attempt: number) => void): () => void {
107-
if (!this.socket) return () => {};
130+
/**
131+
* Registers a handler for reconnection events
132+
* @param handler The event handler
133+
* @returns An object with an unsubscribe method
134+
*/
135+
public onReconnect(handler: (attempt: number) => void): {
136+
unsubscribe: () => void;
137+
} {
138+
if (!this.socket) return { unsubscribe: () => {} };
108139
this.socket.on('reconnect', handler);
109-
return () => this.socket?.off('reconnect', handler);
140+
return { unsubscribe: () => this.socket?.off('reconnect', handler) };
110141
}
111142

112143
/**
113144
* Registers a handler for reconnection attempt events
114145
* @param handler The event handler
115146
* @returns A function to unsubscribe
116147
*/
117-
public onReconnectAttempt(handler: (attempt: number) => void): () => void {
118-
if (!this.socket) return () => {};
148+
/**
149+
* Registers a handler for reconnection attempt events
150+
* @param handler The event handler
151+
* @returns An object with an unsubscribe method
152+
*/
153+
public onReconnectAttempt(handler: (attempt: number) => void): {
154+
unsubscribe: () => void;
155+
} {
156+
if (!this.socket) return { unsubscribe: () => {} };
119157
this.socket.on('reconnect_attempt', handler);
120-
return () => this.socket?.off('reconnect_attempt', handler);
158+
return {
159+
unsubscribe: () => this.socket?.off('reconnect_attempt', handler),
160+
};
121161
}
122162

123163
/**
124164
* Registers a handler for reconnection error events
125165
* @param handler The event handler
126166
* @returns A function to unsubscribe
127167
*/
128-
public onReconnectError(handler: ErrorHandler): () => void {
129-
if (!this.socket) return () => {};
168+
/**
169+
* Registers a handler for reconnection error events
170+
* @param handler The event handler
171+
* @returns An object with an unsubscribe method
172+
*/
173+
public onReconnectError(handler: ErrorHandler): { unsubscribe: () => void } {
174+
if (!this.socket) return { unsubscribe: () => {} };
130175
this.socket.on('reconnect_error', handler);
131-
return () => this.socket?.off('reconnect_error', handler);
176+
return { unsubscribe: () => this.socket?.off('reconnect_error', handler) };
132177
}
133178

134179
/**
135180
* Registers a handler for reconnection failed events
136181
* @param handler The event handler
137182
* @returns A function to unsubscribe
138183
*/
139-
public onReconnectFailed(handler: ConnectionHandler): () => void {
140-
if (!this.socket) return () => {};
184+
/**
185+
* Registers a handler for reconnection failed events
186+
* @param handler The event handler
187+
* @returns An object with an unsubscribe method
188+
*/
189+
public onReconnectFailed(handler: ConnectionHandler): {
190+
unsubscribe: () => void;
191+
} {
192+
if (!this.socket) return { unsubscribe: () => {} };
141193
this.socket.on('reconnect_failed', handler);
142-
return () => this.socket?.off('reconnect_failed', handler);
194+
return { unsubscribe: () => this.socket?.off('reconnect_failed', handler) };
143195
}
144196

145197
/**
146198
* Starts a periodic ping to check connection health
147199
* @param interval The ping interval in milliseconds
148200
* @returns A function to stop the ping
149201
*/
150-
public startPingInterval(interval: number = 30000): () => void {
151-
if (!this.socket) return () => {};
202+
/**
203+
* Starts a periodic ping to check connection health
204+
* @param interval The ping interval in milliseconds
205+
* @returns An object with an unsubscribe method to stop the ping
206+
*/
207+
public startPingInterval(interval: number = 30000): {
208+
unsubscribe: () => void;
209+
} {
210+
if (!this.socket) return { unsubscribe: () => {} };
152211

153212
const pingInterval = setInterval(() => {
154213
if (this.socket?.connected) {
@@ -164,7 +223,7 @@ export class SocketClientService {
164223
}
165224
}, interval);
166225

167-
return () => clearInterval(pingInterval);
226+
return { unsubscribe: () => clearInterval(pingInterval) };
168227
}
169228

170229
/**
@@ -242,9 +301,18 @@ export class SocketClientService {
242301
* @param handler The event handler
243302
* @returns A function to unsubscribe
244303
*/
245-
protected on<T>(event: string, handler: (data: T) => void): () => void {
246-
if (!this.socket) return () => {};
304+
/**
305+
* Registers a handler for an event
306+
* @param event The event name
307+
* @param handler The event handler
308+
* @returns An object with an unsubscribe method
309+
*/
310+
protected on<T>(
311+
event: string,
312+
handler: (data: T) => void,
313+
): { unsubscribe: () => void } {
314+
if (!this.socket) return { unsubscribe: () => {} };
247315
this.socket.on(event, handler);
248-
return () => this.socket?.off(event, handler);
316+
return { unsubscribe: () => this.socket?.off(event, handler) };
249317
}
250318
}

0 commit comments

Comments
 (0)