Skip to content

Commit

Permalink
fix(rxjs/webSocket): No longer tries to send unsub when the socket so…
Browse files Browse the repository at this point in the history
…urce disconnects
  • Loading branch information
benlesh committed Feb 28, 2024
1 parent 61b3966 commit 34b99f5
Show file tree
Hide file tree
Showing 2 changed files with 16 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
17 changes: 15 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 '../../Observable.js';
import type { Subscriber, Subscription } from '../../Observable.js';
import { Observable, operate } from '../../Observable.js';
import type { NextObserver } from '../../types.js';

Expand Down Expand Up @@ -219,8 +219,13 @@ 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 +235,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 34b99f5

Please sign in to comment.