1 import { Subject, AnonymousSubject } from '../../Subject';
2 import { Subscriber } from '../../Subscriber';
3 import { Observable } from '../../Observable';
4 import { Subscription } from '../../Subscription';
5 import { ReplaySubject } from '../../ReplaySubject';
6 const DEFAULT_WEBSOCKET_CONFIG = {
8 deserializer: (e) => JSON.parse(e.data),
9 serializer: (value) => JSON.stringify(value),
11 const WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT = 'WebSocketSubject.error must be called with an object with an error code, and an optional reason: { code: number, reason: string }';
12 export class WebSocketSubject extends AnonymousSubject {
13 constructor(urlConfigOrSource, destination) {
15 if (urlConfigOrSource instanceof Observable) {
16 this.destination = destination;
17 this.source = urlConfigOrSource;
20 const config = this._config = Object.assign({}, DEFAULT_WEBSOCKET_CONFIG);
21 this._output = new Subject();
22 if (typeof urlConfigOrSource === 'string') {
23 config.url = urlConfigOrSource;
26 for (let key in urlConfigOrSource) {
27 if (urlConfigOrSource.hasOwnProperty(key)) {
28 config[key] = urlConfigOrSource[key];
32 if (!config.WebSocketCtor && WebSocket) {
33 config.WebSocketCtor = WebSocket;
35 else if (!config.WebSocketCtor) {
36 throw new Error('no WebSocket constructor can be found');
38 this.destination = new ReplaySubject();
42 const sock = new WebSocketSubject(this._config, this.destination);
43 sock.operator = operator;
50 this.destination = new ReplaySubject();
52 this._output = new Subject();
54 multiplex(subMsg, unsubMsg, messageFilter) {
56 return new Observable((observer) => {
63 const subscription = self.subscribe(x => {
65 if (messageFilter(x)) {
72 }, err => observer.error(err), () => observer.complete());
75 self.next(unsubMsg());
80 subscription.unsubscribe();
85 const { WebSocketCtor, protocol, url, binaryType } = this._config;
86 const observer = this._output;
90 new WebSocketCtor(url, protocol) :
91 new WebSocketCtor(url);
92 this._socket = socket;
94 this._socket.binaryType = binaryType;
101 const subscription = new Subscription(() => {
103 if (socket && socket.readyState === 1) {
107 socket.onopen = (e) => {
108 const { _socket } = this;
114 const { openObserver } = this._config;
116 openObserver.next(e);
118 const queue = this.destination;
119 this.destination = Subscriber.create((x) => {
120 if (socket.readyState === 1) {
122 const { serializer } = this._config;
123 socket.send(serializer(x));
126 this.destination.error(e);
130 const { closingObserver } = this._config;
131 if (closingObserver) {
132 closingObserver.next(undefined);
135 socket.close(e.code, e.reason);
138 observer.error(new TypeError(WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT));
142 const { closingObserver } = this._config;
143 if (closingObserver) {
144 closingObserver.next(undefined);
149 if (queue && queue instanceof ReplaySubject) {
150 subscription.add(queue.subscribe(this.destination));
153 socket.onerror = (e) => {
157 socket.onclose = (e) => {
159 const { closeObserver } = this._config;
161 closeObserver.next(e);
170 socket.onmessage = (e) => {
172 const { deserializer } = this._config;
173 observer.next(deserializer(e));
180 _subscribe(subscriber) {
181 const { source } = this;
183 return source.subscribe(subscriber);
186 this._connectSocket();
188 this._output.subscribe(subscriber);
189 subscriber.add(() => {
190 const { _socket } = this;
191 if (this._output.observers.length === 0) {
192 if (_socket && _socket.readyState === 1) {
201 const { _socket } = this;
202 if (_socket && _socket.readyState === 1) {
209 //# sourceMappingURL=WebSocketSubject.js.map