Skip to content

Commit ca3aef1

Browse files
committed
update API
1 parent 502a69b commit ca3aef1

File tree

1 file changed

+7
-30
lines changed

1 file changed

+7
-30
lines changed

packages/polling/src/socketstream.ts

+7-30
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,13 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
2222
*
2323
* @param sender - The sender which owns the stream.
2424
*
25-
* @param options - Web socket `url` and optional `WebSocket` constructor.
25+
* @param connector - A factory that returns a new web socket connection.
2626
*/
27-
constructor(sender: T, options: SocketStream.IOptions) {
27+
constructor(
28+
sender: T,
29+
protected readonly connector: () => WebSocket
30+
) {
2831
super(sender);
29-
this.factory = () => new (options.WebSocket || WebSocket)(options.url);
3032
this.subscription = new Poll({ factory: () => this.subscribe() });
3133
}
3234

@@ -56,19 +58,14 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
5658
}
5759

5860
/**
59-
* Send a message to the underlying web socket.
61+
* Send a message via the underlying web socket.
6062
*
6163
* @param data - The payload of the message sent via the web socket.
6264
*/
6365
send(data: string | ArrayBufferLike | Blob | ArrayBufferView): void {
6466
this.socket?.send(data);
6567
}
6668

67-
/**
68-
* A factory that generates a new web socket instance for subscription.
69-
*/
70-
protected readonly factory: () => WebSocket;
71-
7269
/**
7370
* The current active socket. This value is updated by the `subscribe` method.
7471
*/
@@ -89,29 +86,9 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
8986
return;
9087
}
9188
return new Promise((_, reject) => {
92-
this.socket = this.factory();
89+
this.socket = this.connector();
9390
this.socket.onclose = () => reject(new Error('socket stream has closed'));
9491
this.socket.onmessage = ({ data }) => data && this.emit(JSON.parse(data));
9592
});
9693
}
9794
}
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

Comments
 (0)