Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions spec/observables/dom/webSocket-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,52 @@ describe('webSocket', () => {
subject.unsubscribe();
});

it('should take a serializer', () => {
const subject = webSocket<{ data: string}>({
url: 'ws://mysocket',
serializer: (e: any) => {
return e.data + '!';
}
});

subject.subscribe();

const socket = MockWebSocket.lastSocket;
socket.open();


['ahoy', 'yarr', 'shove off'].forEach(x => {
subject.next({ data: x });
expect(socket.lastMessageSent).to.equal(x + '!');
});

subject.unsubscribe();
});

it('if the serializer fails it should close the connection and rethrow the error', () => {
const subject = webSocket<string>({
url: 'ws://mysocket',
serializer: (e: any) => {
throw new Error('I am a bad error');
}
});

subject.subscribe({ next: (x: any) => {
expect(x).to.equal('this should not happen');
}, error: (err: any) => {
expect(err).to.equal('this should not happen');
} });

const socket = MockWebSocket.lastSocket;
socket.open();

expect(() => subject.next('weee!')).to.throw('I am a bad error');
expect(socket.readyState).to.equal(WebSocketState.CLOSING);
expect(socket.closeCode).to.equal(1000);

subject.unsubscribe();
});

it('should accept a closingObserver', () => {
let calls = 0;
const subject = webSocket<string>(<any>{
Expand Down
23 changes: 15 additions & 8 deletions src/internal/observable/dom/WebSocketSubject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,18 +296,25 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {

const queue = this.destination;

this.destination = Subscriber.create<T>(
(x) => {
this.destination = new Subscriber<T>({
next: (x: T) => {
if (socket!.readyState === 1) {
try {
const { serializer } = this._config;
socket!.send(serializer!(x!));
} catch (e) {
this.destination!.error(e);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd say the actual bug was here. We clearly have a handle to the wrong thing. 🤔

} catch (e: any) {
if (e && e.code) {
this.destination!.error(e);
} else {
this.destination!.error({
code: 1000,
});
throw e;
}
}
}
},
(err) => {
error: (err: any) => {
const { closingObserver } = this._config;
if (closingObserver) {
closingObserver.next(undefined);
Expand All @@ -319,15 +326,15 @@ export class WebSocketSubject<T> extends AnonymousSubject<T> {
}
this._resetState();
},
() => {
complete: () => {
const { closingObserver } = this._config;
if (closingObserver) {
closingObserver.next(undefined);
}
socket!.close();
this._resetState();
}
) as Subscriber<any>;
},
}) as Subscriber<any>;

if (queue && queue instanceof ReplaySubject) {
subscription.add((queue as ReplaySubject<T>).subscribe(this.destination));
Expand Down