Skip to content

Commit 34b99f5

Browse files
committed
fix(rxjs/webSocket): No longer tries to send unsub when the socket source disconnects
1 parent 61b3966 commit 34b99f5

File tree

2 files changed

+16
-3
lines changed

2 files changed

+16
-3
lines changed

packages/rxjs/spec/observables/dom/webSocket-spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ describe('webSocket', () => {
669669
});
670670
socket.triggerClose({ wasClean: true });
671671

672-
expect(results).to.deep.equal(['A next', 'A unsub', 'B next', 'B next', 'B next', 'B unsub', 'B complete']);
672+
expect(results).to.deep.equal(['A next', 'A unsub', 'B next', 'B next', 'B next', 'B complete']);
673673
});
674674

675675
it('should not close the socket until all subscriptions complete', () => {

packages/rxjs/src/internal/observable/dom/WebSocketSubject.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { Subscriber, Subscription} from '../../Observable.js';
1+
import type { Subscriber, Subscription } from '../../Observable.js';
22
import { Observable, operate } from '../../Observable.js';
33
import type { NextObserver } from '../../types.js';
44

@@ -219,8 +219,13 @@ export class WebSocketSubject<In, Out = In> extends Observable<Out> {
219219
multiplex(subMsg: () => In, unsubMsg: () => In, messageFilter: (value: Out) => boolean) {
220220
return new Observable<Out>((destination) => {
221221
this.next(subMsg());
222+
223+
let isUnsub = true;
224+
222225
destination.add(() => {
223-
this.next(unsubMsg());
226+
if (isUnsub) {
227+
this.next(unsubMsg());
228+
}
224229
});
225230
this.subscribe(
226231
operate({
@@ -230,6 +235,14 @@ export class WebSocketSubject<In, Out = In> extends Observable<Out> {
230235
destination.next(x);
231236
}
232237
},
238+
error: (err) => {
239+
isUnsub = false;
240+
destination.error(err);
241+
},
242+
complete: () => {
243+
isUnsub = false;
244+
destination.complete();
245+
},
233246
})
234247
);
235248
});

0 commit comments

Comments
 (0)