@@ -22,11 +22,13 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
22
22
*
23
23
* @param sender - The sender which owns the stream.
24
24
*
25
- * @param options - Web socket `url` and optional `WebSocket` constructor .
25
+ * @param connector - A factory that returns a new web socket connection .
26
26
*/
27
- constructor ( sender : T , options : SocketStream . IOptions ) {
27
+ constructor (
28
+ sender : T ,
29
+ protected readonly connector : ( ) => WebSocket
30
+ ) {
28
31
super ( sender ) ;
29
- this . factory = ( ) => new ( options . WebSocket || WebSocket ) ( options . url ) ;
30
32
this . subscription = new Poll ( { factory : ( ) => this . subscribe ( ) } ) ;
31
33
}
32
34
@@ -56,19 +58,14 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
56
58
}
57
59
58
60
/**
59
- * Send a message to the underlying web socket.
61
+ * Send a message via the underlying web socket.
60
62
*
61
63
* @param data - The payload of the message sent via the web socket.
62
64
*/
63
65
send ( data : string | ArrayBufferLike | Blob | ArrayBufferView ) : void {
64
66
this . socket ?. send ( data ) ;
65
67
}
66
68
67
- /**
68
- * A factory that generates a new web socket instance for subscription.
69
- */
70
- protected readonly factory : ( ) => WebSocket ;
71
-
72
69
/**
73
70
* The current active socket. This value is updated by the `subscribe` method.
74
71
*/
@@ -89,29 +86,9 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
89
86
return ;
90
87
}
91
88
return new Promise ( ( _ , reject ) => {
92
- this . socket = this . factory ( ) ;
89
+ this . socket = this . connector ( ) ;
93
90
this . socket . onclose = ( ) => reject ( new Error ( 'socket stream has closed' ) ) ;
94
91
this . socket . onmessage = ( { data } ) => data && this . emit ( JSON . parse ( data ) ) ;
95
92
} ) ;
96
93
}
97
94
}
98
-
99
- /**
100
- * A namespace for `SocketStream` statics.
101
- */
102
- export namespace SocketStream {
103
- /**
104
- * Instantiation options for a socket stream.
105
- */
106
- export interface IOptions {
107
- /**
108
- * The web socket URL to open.
109
- */
110
- url : string ;
111
-
112
- /**
113
- * An optional web socket constructor.
114
- */
115
- WebSocket ?: typeof WebSocket ;
116
- }
117
- }
0 commit comments