Skip to content

Commit

Permalink
fix(rxjs): WebSocketSubject multiplex no longer tries to send unsubsc…
Browse files Browse the repository at this point in the history
…ribe messages to closed sockets
  • Loading branch information
benlesh committed Feb 29, 2024
1 parent 63fff0f commit 9a05f17
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 3 deletions.
2 changes: 1 addition & 1 deletion packages/rxjs/spec/observables/dom/webSocket-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -669,7 +669,7 @@ describe('webSocket', () => {
});
socket.triggerClose({ wasClean: true });

expect(results).to.deep.equal(['A next', 'A unsub', 'B next', 'B next', 'B next', 'B unsub', 'B complete']);
expect(results).to.deep.equal(['A next', 'A unsub', 'B next', 'B next', 'B next', 'B complete']);
});

it('should not close the socket until all subscriptions complete', () => {
Expand Down
15 changes: 13 additions & 2 deletions packages/rxjs/src/internal/observable/dom/WebSocketSubject.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { Subscriber, Subscription} from '@rxjs/observable';
import type { Subscriber, Subscription } from '@rxjs/observable';
import { Observable, operate } from '@rxjs/observable';
import type { NextObserver } from '../../types.js';

Expand Down Expand Up @@ -219,8 +219,11 @@ export class WebSocketSubject<In, Out = In> extends Observable<Out> {
multiplex(subMsg: () => In, unsubMsg: () => In, messageFilter: (value: Out) => boolean) {
return new Observable<Out>((destination) => {
this.next(subMsg());
let isUnsub = true;
destination.add(() => {
this.next(unsubMsg());
if (isUnsub) {
this.next(unsubMsg());
}
});
this.subscribe(
operate({
Expand All @@ -230,6 +233,14 @@ export class WebSocketSubject<In, Out = In> extends Observable<Out> {
destination.next(x);
}
},
error: (err) => {
isUnsub = false;
destination.error(err);
},
complete: () => {
isUnsub = false;
destination.complete();
},
})
);
});
Expand Down

0 comments on commit 9a05f17

Please sign in to comment.