Skip to content

Commit 2a0c360

Browse files
committed
dont need multipler listeners on connection
1 parent 6575aa5 commit 2a0c360

File tree

6 files changed

+94
-152
lines changed

6 files changed

+94
-152
lines changed

testUtil/fixtures/mockTransport.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,15 @@ export class InMemoryConnection extends Connection {
1818
this.conn.allowHalfOpen = false;
1919

2020
this.conn.on('data', (data: Uint8Array) => {
21-
for (const cb of this.dataListeners) {
22-
cb(data);
23-
}
21+
this.dataListener?.(data);
2422
});
2523

2624
this.conn.on('close', () => {
27-
for (const cb of this.closeListeners) {
28-
cb();
29-
}
25+
this.closeListener?.();
3026
});
3127

3228
this.conn.on('error', (err) => {
33-
for (const cb of this.errorListeners) {
34-
cb(err);
35-
}
29+
this.errorListener?.(err);
3630
});
3731
}
3832

transport/connection.ts

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { generateId } from './id';
44

55
/**
66
* A connection is the actual raw underlying transport connection.
7-
* Its responsible for dispatching to/from the actual connection itself
7+
* It's responsible for dispatching to/from the actual connection itself
88
* This should be instantiated as soon as the client/server has a connection
9-
* Its tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)
9+
* It's tied to the lifecycle of the underlying transport connection (i.e. if the WS drops, this connection should be deleted)
1010
*/
1111
export abstract class Connection {
1212
id: string;
@@ -30,85 +30,65 @@ export abstract class Connection {
3030
return metadata;
3131
}
3232

33-
// can't use event emitter because we need this to work in both node + browser
34-
private _dataListeners = new Set<(msg: Uint8Array) => void>();
35-
private _closeListeners = new Set<() => void>();
36-
private _errorListeners = new Set<(err: Error) => void>();
37-
38-
get dataListeners() {
39-
return [...this._dataListeners];
40-
}
41-
42-
get closeListeners() {
43-
return [...this._closeListeners];
44-
}
45-
46-
get errorListeners() {
47-
return [...this._errorListeners];
48-
}
33+
dataListener?: (msg: Uint8Array) => void;
34+
closeListener?: () => void;
35+
errorListener?: (err: Error) => void;
4936

5037
onData(msg: Uint8Array) {
51-
for (const cb of this.dataListeners) {
52-
cb(msg);
53-
}
38+
this.dataListener?.(msg);
5439
}
5540

5641
onError(err: Error) {
57-
for (const cb of this.errorListeners) {
58-
cb(err);
59-
}
42+
this.errorListener?.(err);
6043
}
6144

6245
onClose() {
63-
for (const cb of this.closeListeners) {
64-
cb();
65-
}
66-
46+
this.closeListener?.();
6747
this.telemetry?.span.end();
6848
}
6949

7050
/**
71-
* Handle adding a callback for when a message is received.
72-
* @param msg The message that was received.
51+
* Set the callback for when a message is received.
52+
* @param cb The message handler callback.
7353
*/
74-
addDataListener(cb: (msg: Uint8Array) => void) {
75-
this._dataListeners.add(cb);
54+
setDataListener(cb: (msg: Uint8Array) => void) {
55+
this.dataListener = cb;
7656
}
7757

78-
removeDataListener(cb: (msg: Uint8Array) => void): void {
79-
this._dataListeners.delete(cb);
58+
removeDataListener() {
59+
this.dataListener = undefined;
8060
}
8161

8262
/**
83-
* Handle adding a callback for when the connection is closed.
84-
* This should also be called if an error happens and after notifying all the error listeners.
63+
* Set the callback for when the connection is closed.
64+
* This should also be called if an error happens and after notifying the error listener.
8565
* @param cb The callback to call when the connection is closed.
8666
*/
87-
addCloseListener(cb: () => void): void {
88-
this._closeListeners.add(cb);
67+
setCloseListener(cb: () => void): void {
68+
this.closeListener = cb;
8969
}
9070

91-
removeCloseListener(cb: () => void): void {
92-
this._closeListeners.delete(cb);
71+
removeCloseListener(): void {
72+
this.closeListener = undefined;
9373
}
9474

9575
/**
96-
* Handle adding a callback for when an error is received.
97-
* This should only be used for this.logging errors, all cleanup
98-
* should be delegated to addCloseListener.
76+
* Set the callback for when an error is received.
77+
* This should only be used for logging errors, all cleanup
78+
* should be delegated to setCloseListener.
9979
*
10080
* The implementer should take care such that the implemented
10181
* connection will call both the close and error callbacks
10282
* on an error.
10383
*
10484
* @param cb The callback to call when an error is received.
10585
*/
106-
addErrorListener(cb: (err: Error) => void): void {
107-
this._errorListeners.add(cb);
86+
setErrorListener(cb: (err: Error) => void): void {
87+
this.errorListener = cb;
10888
}
10989

110-
removeErrorListener(cb: (err: Error) => void): void {
111-
this._errorListeners.delete(cb);
90+
removeErrorListener(): void {
91+
this.errorListener = undefined;
11292
}
11393

11494
/**

transport/sessionStateMachine/SessionConnected.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,9 @@ export class SessionConnected<
9191
this.conn = props.conn;
9292
this.listeners = props.listeners;
9393

94-
this.conn.addDataListener(this.onMessageData);
95-
this.conn.addCloseListener(this.listeners.onConnectionClosed);
96-
this.conn.addErrorListener(this.listeners.onConnectionErrored);
94+
this.conn.setDataListener(this.onMessageData);
95+
this.conn.setCloseListener(this.listeners.onConnectionClosed);
96+
this.conn.setErrorListener(this.listeners.onConnectionErrored);
9797
}
9898

9999
sendBufferedMessages(): SendBufferResult {
@@ -240,9 +240,9 @@ export class SessionConnected<
240240

241241
_handleStateExit(): void {
242242
super._handleStateExit();
243-
this.conn.removeDataListener(this.onMessageData);
244-
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
245-
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
243+
this.conn.removeDataListener();
244+
this.conn.removeCloseListener();
245+
this.conn.removeErrorListener();
246246

247247
if (this.heartbeatHandle) {
248248
clearInterval(this.heartbeatHandle);

transport/sessionStateMachine/SessionHandshaking.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,9 @@ export class SessionHandshaking<
5656
this.listeners.onHandshakeTimeout();
5757
}, this.options.handshakeTimeoutMs);
5858

59-
this.conn.addDataListener(this.onHandshakeData);
60-
this.conn.addErrorListener(this.listeners.onConnectionErrored);
61-
this.conn.addCloseListener(this.listeners.onConnectionClosed);
59+
this.conn.setDataListener(this.onHandshakeData);
60+
this.conn.setErrorListener(this.listeners.onConnectionErrored);
61+
this.conn.setCloseListener(this.listeners.onConnectionClosed);
6262
}
6363

6464
get loggingMetadata() {
@@ -88,9 +88,9 @@ export class SessionHandshaking<
8888

8989
_handleStateExit(): void {
9090
super._handleStateExit();
91-
this.conn.removeDataListener(this.onHandshakeData);
92-
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
93-
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
91+
this.conn.removeDataListener();
92+
this.conn.removeErrorListener();
93+
this.conn.removeCloseListener();
9494

9595
if (this.handshakeTimeout) {
9696
clearTimeout(this.handshakeTimeout);

transport/sessionStateMachine/SessionWaitingForHandshake.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -54,9 +54,9 @@ export class SessionWaitingForHandshake<
5454
this.listeners.onHandshakeTimeout();
5555
}, this.options.handshakeTimeoutMs);
5656

57-
this.conn.addDataListener(this.onHandshakeData);
58-
this.conn.addErrorListener(this.listeners.onConnectionErrored);
59-
this.conn.addCloseListener(this.listeners.onConnectionClosed);
57+
this.conn.setDataListener(this.onHandshakeData);
58+
this.conn.setErrorListener(this.listeners.onConnectionErrored);
59+
this.conn.setCloseListener(this.listeners.onConnectionClosed);
6060
}
6161

6262
get loggingMetadata() {
@@ -88,9 +88,9 @@ export class SessionWaitingForHandshake<
8888
}
8989

9090
_handleStateExit(): void {
91-
this.conn.removeDataListener(this.onHandshakeData);
92-
this.conn.removeErrorListener(this.listeners.onConnectionErrored);
93-
this.conn.removeCloseListener(this.listeners.onConnectionClosed);
91+
this.conn.removeDataListener();
92+
this.conn.removeErrorListener();
93+
this.conn.removeCloseListener();
9494
clearTimeout(this.handshakeTimeout);
9595
this.handshakeTimeout = undefined;
9696
}

0 commit comments

Comments
 (0)