Skip to content

Commit

Permalink
fix(WebSocketSubject): prevent early close (#1831)
Browse files Browse the repository at this point in the history
  • Loading branch information
deontologician authored and kwonoj committed Jul 26, 2016
1 parent 8f0dc01 commit 848a527
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
53 changes: 52 additions & 1 deletion spec/observables/dom/webSocket-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,5 +436,56 @@ describe('Observable.webSocket', () => {

expect(results).to.deep.equal(['sub', 'unsub', 'sub', error, 'unsub']);
});

it('should not close the socket until all subscriptions complete', () => {
const socketSubject = Rx.Observable.webSocket(<any>{url: 'ws://mysocket'});
const results = [];
const socketMessages = [
{id: 'A'},
{id: 'B'},
{id: 'A', complete: true},
{id: 'B'},
{id: 'B', complete: true},
];

socketSubject.multiplex(
() => 'no-op',
() => results.push('A unsub'),
(req: any) => req.id === 'A')
.takeWhile((req: any) => !req.complete)
.subscribe(
() => results.push('A next'),
(e) => results.push('A error ' + e),
() => results.push('A complete')
);

socketSubject.multiplex(
() => 'no-op',
() => results.push('B unsub'),
(req: any) => req.id === 'B')
.takeWhile((req: any) => !req.complete)
.subscribe(
() => results.push('B next'),
(e) => results.push('B error ' + e),
() => results.push('B complete')
);

// Setup socket and send messages
let socket = MockWebSocket.lastSocket;
socket.open();
socketMessages.forEach((msg) => {
socket.triggerMessage(JSON.stringify(msg));
});

expect(results).to.deep.equal([
'A next',
'B next',
'A complete',
'A unsub',
'B next',
'B complete',
'B unsub',
]);
});
});
});
});
2 changes: 1 addition & 1 deletion src/observable/dom/WebSocketSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
subscription.add(this._output.subscribe(subscriber));
subscription.add(() => {
const { socket } = this;
if (socket && socket.readyState === 1) {
if (this._output.observers.length === 0 && socket && socket.readyState === 1) {
socket.close();
this.socket = null;
}
Expand Down

0 comments on commit 848a527

Please sign in to comment.