Skip to content

Commit 292168b

Browse files
authored
Merge pull request #12 from appwrite/add-connection-close-logs
chore: update onClose callback to include logs
2 parents 6b30362 + 35fbcb7 commit 292168b

File tree

3 files changed

+42
-6
lines changed

3 files changed

+42
-6
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@appwrite.io/synapse",
3-
"version": "0.4.0",
3+
"version": "0.4.1",
44
"description": "Operating system gateway for remote serverless environments",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",

src/synapse.ts

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,16 @@ export type MessageHandler = (
2222
message: MessagePayload,
2323
connectionId: string,
2424
) => void;
25+
2526
export type ConnectionCallback = (connectionId: string) => void;
27+
28+
export type ConnectionCloseCallback = (
29+
connectionId: string,
30+
code?: number,
31+
reason?: string,
32+
wasClean?: boolean,
33+
) => void;
34+
2635
export type ErrorCallback = (error: Error, connectionId: string) => void;
2736
export type ServerConnectionCallback = (connectionId: string) => void;
2837
export type Logger = (message: string) => void;
@@ -33,7 +42,7 @@ class Synapse {
3342
private messageHandlers: Record<string, MessageHandler> = {};
3443
private connectionListeners = {
3544
onOpen: (() => {}) as ConnectionCallback,
36-
onClose: (() => {}) as ConnectionCallback,
45+
onClose: (() => {}) as ConnectionCloseCallback,
3746
onError: (() => {}) as ErrorCallback,
3847
};
3948

@@ -112,8 +121,13 @@ class Synapse {
112121

113122
ws.onmessage = (event) => this.handleMessage(event, connectionId);
114123

115-
ws.onclose = () => {
116-
this.connectionListeners.onClose(connectionId);
124+
ws.onclose = (event) => {
125+
this.connectionListeners.onClose(
126+
connectionId,
127+
event.code,
128+
event.reason,
129+
event.wasClean,
130+
);
117131
this.attemptReconnect(connectionId);
118132
};
119133

@@ -440,10 +454,10 @@ class Synapse {
440454

441455
/**
442456
* Registers a callback for when a WebSocket connection is closed
443-
* @param callback - Function to be called when connection closes
457+
* @param callback - Function to be called when connection closes. Receives (connectionId, code, reason)
444458
* @returns The Synapse instance for method chaining
445459
*/
446-
onClose(callback: ConnectionCallback): Synapse {
460+
onClose(callback: ConnectionCloseCallback): Synapse {
447461
this.connectionListeners.onClose = callback;
448462
return this;
449463
}

tests/synapse.test.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,28 @@ describe("Synapse", () => {
6262

6363
await expect(connectPromise).rejects.toThrow("WebSocket error");
6464
});
65+
66+
it("should call onClose with code, reason, and wasClean", () => {
67+
const mockWs = createMockWebSocket();
68+
(WebSocket as unknown as jest.Mock).mockImplementation(() => mockWs);
69+
70+
const onCloseMock = jest.fn();
71+
synapse.onClose(onCloseMock);
72+
73+
// Use the real setup method so event handlers are set
74+
(synapse as any).setupWebSocket(mockWs, { url: "/" }, "conn1");
75+
76+
// Simulate close event
77+
const closeEvent = { code: 4001, reason: "Test reason", wasClean: true };
78+
mockWs.onclose && mockWs.onclose(closeEvent as any);
79+
80+
expect(onCloseMock).toHaveBeenCalledWith(
81+
"conn1",
82+
4001,
83+
"Test reason",
84+
true,
85+
);
86+
});
6587
});
6688

6789
describe("message handling", () => {

0 commit comments

Comments
 (0)