@@ -4,9 +4,9 @@ import { generateId } from './id';
44
55/**
66 * A connection is the actual raw underlying transport connection.
7- * It’ s 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- * It’ s 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 */
1111export 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 /**
0 commit comments