Skip to content

Commit 502a69b

Browse files
committed
Abstract the underlying poll
1 parent 7bc5092 commit 502a69b

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

packages/polling/src/socketstream.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ import { Poll } from './poll';
1818
*/
1919
export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
2020
/**
21-
* Construct a new socket stream.
21+
* Construct a new web socket stream.
2222
*
2323
* @param sender - The sender which owns the stream.
2424
*
25-
* @param options = The socket stream instantiation options.
25+
* @param options - Web socket `url` and optional `WebSocket` constructor.
2626
*/
2727
constructor(sender: T, options: SocketStream.IOptions) {
2828
super(sender);
@@ -41,9 +41,8 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
4141
* Dispose the stream.
4242
*/
4343
dispose() {
44-
super.stop();
45-
this.subscription.dispose();
46-
const { socket } = this;
44+
const { socket, subscription } = this;
45+
subscription.dispose();
4746
if (socket) {
4847
this.socket = null;
4948
socket.onclose = () => undefined;
@@ -53,6 +52,7 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
5352
socket.close();
5453
}
5554
Signal.clearData(this);
55+
super.stop();
5656
}
5757

5858
/**
@@ -75,9 +75,9 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
7575
protected socket: WebSocket | null = null;
7676

7777
/**
78-
* The poll instance that mediates the web socket lifecycle.
78+
* A handle to the socket subscription to dispose when necessary.
7979
*/
80-
protected readonly subscription: Poll;
80+
protected readonly subscription: IDisposable;
8181

8282
/**
8383
* Open a web socket and subscribe to its updates.
@@ -88,7 +88,7 @@ export class SocketStream<T, U> extends Stream<T, U> implements IDisposable {
8888
if (this.isDisposed) {
8989
return;
9090
}
91-
return new Promise<void>((_, reject) => {
91+
return new Promise((_, reject) => {
9292
this.socket = this.factory();
9393
this.socket.onclose = () => reject(new Error('socket stream has closed'));
9494
this.socket.onmessage = ({ data }) => data && this.emit(JSON.parse(data));

0 commit comments

Comments
 (0)