UNPKG

@reactivex/rxjs

Version:

Reactive Extensions for modern JavaScript

296 lines (286 loc) 15.3 kB
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <base data-ice="baseUrl" href="../../../../"> <title data-ice="title">es6/observable/dom/WebSocketSubject.js | RxJS API Document</title> <link type="text/css" rel="stylesheet" href="css/style.css"> <link type="text/css" rel="stylesheet" href="css/prettify-tomorrow.css"> <script src="script/prettify/prettify.js"></script> <script src="script/manual.js"></script> <script data-ice="userScript" src="user/script/0-Rx.js"></script> <script data-ice="userScript" src="user/script/1-devtools-welcome.js"></script> <script data-ice="userScript" src="user/script/2-custom-manual-styles.js"></script> <script data-ice="userScript" src="user/script/3-decision-tree-widget.min.js"></script> <script data-ice="userScript" src="user/script/4-theme-toggler.js"></script> <link data-ice="userStyle" rel="stylesheet" href="user/css/0-main.css"> </head> <body class="layout-container" data-ice="rootContainer"> <header> <a href="./">Home</a> <a href="./manual/index.html" data-ice="manualHeaderLink">Manual</a> <a href="identifiers.html">Reference</a> <a href="source.html">Source</a> <a href="test.html" data-ice="testLink">Test</a> <a data-ice="repoURL" href="https://github.com/ReactiveX/RxJS" class="repo-url-github">Repository</a> <div class="search-box"> <span> <img src="./image/search.png"> <span class="search-input-edge"></span><input class="search-input"><span class="search-input-edge"></span> </span> <ul class="search-result"></ul> </div> </header> <nav class="navigation" data-ice="nav"><div> <ul> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AsyncSubject">AsyncSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-BehaviorSubject">BehaviorSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Notification">Notification</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Observable">Observable</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ReplaySubject">ReplaySubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Scheduler">Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AnonymousSubject">AnonymousSubject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subject">Subject</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-SubjectSubscriber">SubjectSubscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscriber">Subscriber</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Subscription">Subscription</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Scheduler">Rx.Scheduler</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-typedef">T</span><span data-ice="name"><span><a href="typedef/index.html#static-typedef-Rx.Symbol">Rx.Symbol</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ConnectableObservable">ConnectableObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">observable/dom</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxError">AjaxError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxResponse">AjaxResponse</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-AjaxTimeoutError">AjaxTimeoutError</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">operator</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-GroupedObservable">GroupedObservable</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">scheduler</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-Action">Action</a></span></span></li> <li data-ice="doc"><div data-ice="dirPath" class="nav-dir-path">util</div><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ArgumentOutOfRangeError">ArgumentOutOfRangeError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-EmptyError">EmptyError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-ObjectUnsubscribedError">ObjectUnsubscribedError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-TimeoutError">TimeoutError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-UnsubscriptionError">UnsubscriptionError</a></span></span></li> <li data-ice="doc"><span data-ice="kind" class="kind-variable">V</span><span data-ice="name"><span><a href="variable/index.html#static-variable-root">root</a></span></span></li> </ul> </div> </nav> <div class="content" data-ice="content"><h1 data-ice="title">es6/observable/dom/WebSocketSubject.js</h1> <pre class="source-code line-number raw-source-code"><code class="prettyprint linenums" data-ice="content">import { Subject, AnonymousSubject } from &apos;../../Subject&apos;; import { Subscriber } from &apos;../../Subscriber&apos;; import { Observable } from &apos;../../Observable&apos;; import { Subscription } from &apos;../../Subscription&apos;; import { root } from &apos;../../util/root&apos;; import { ReplaySubject } from &apos;../../ReplaySubject&apos;; import { tryCatch } from &apos;../../util/tryCatch&apos;; import { errorObject } from &apos;../../util/errorObject&apos;; import { assign } from &apos;../../util/assign&apos;; /** * We need this JSDoc comment for affecting ESDoc. * @extends {Ignored} * @hide true */ export var WebSocketSubject = (function (_super) { __extends(WebSocketSubject, _super); function WebSocketSubject(urlConfigOrSource, destination) { if (urlConfigOrSource instanceof Observable) { _super.call(this, destination, urlConfigOrSource); } else { _super.call(this); this.WebSocketCtor = root.WebSocket; this._output = new Subject(); if (typeof urlConfigOrSource === &apos;string&apos;) { this.url = urlConfigOrSource; } else { // WARNING: config object could override important members here. assign(this, urlConfigOrSource); } if (!this.WebSocketCtor) { throw new Error(&apos;no WebSocket constructor can be found&apos;); } this.destination = new ReplaySubject(); } } WebSocketSubject.prototype.resultSelector = function (e) { return JSON.parse(e.data); }; /** * @param urlConfigOrSource * @return {WebSocketSubject} * @static true * @name webSocket * @owner Observable */ WebSocketSubject.create = function (urlConfigOrSource) { return new WebSocketSubject(urlConfigOrSource); }; WebSocketSubject.prototype.lift = function (operator) { var sock = new WebSocketSubject(this, this.destination); sock.operator = operator; return sock; }; WebSocketSubject.prototype._resetState = function () { this.socket = null; if (!this.source) { this.destination = new ReplaySubject(); } this._output = new Subject(); }; // TODO: factor this out to be a proper Operator/Subscriber implementation and eliminate closures WebSocketSubject.prototype.multiplex = function (subMsg, unsubMsg, messageFilter) { var self = this; return new Observable(function (observer) { var result = tryCatch(subMsg)(); if (result === errorObject) { observer.error(errorObject.e); } else { self.next(result); } var subscription = self.subscribe(function (x) { var result = tryCatch(messageFilter)(x); if (result === errorObject) { observer.error(errorObject.e); } else if (result) { observer.next(x); } }, function (err) { return observer.error(err); }, function () { return observer.complete(); }); return function () { var result = tryCatch(unsubMsg)(); if (result === errorObject) { observer.error(errorObject.e); } else { self.next(result); } subscription.unsubscribe(); }; }); }; WebSocketSubject.prototype._connectSocket = function () { var _this = this; var WebSocketCtor = this.WebSocketCtor; var observer = this._output; var socket = null; try { socket = this.protocol ? new WebSocketCtor(this.url, this.protocol) : new WebSocketCtor(this.url); this.socket = socket; } catch (e) { observer.error(e); return; } var subscription = new Subscription(function () { _this.socket = null; if (socket &amp;&amp; socket.readyState === 1) { socket.close(); } }); socket.onopen = function (e) { var openObserver = _this.openObserver; if (openObserver) { openObserver.next(e); } var queue = _this.destination; _this.destination = Subscriber.create(function (x) { return socket.readyState === 1 &amp;&amp; socket.send(x); }, function (e) { var closingObserver = _this.closingObserver; if (closingObserver) { closingObserver.next(undefined); } if (e &amp;&amp; e.code) { socket.close(e.code, e.reason); } else { observer.error(new TypeError(&apos;WebSocketSubject.error must be called with an object with an error code, &apos; + &apos;and an optional reason: { code: number, reason: string }&apos;)); } _this._resetState(); }, function () { var closingObserver = _this.closingObserver; if (closingObserver) { closingObserver.next(undefined); } socket.close(); _this._resetState(); }); if (queue &amp;&amp; queue instanceof ReplaySubject) { subscription.add(queue.subscribe(_this.destination)); } }; socket.onerror = function (e) { _this._resetState(); observer.error(e); }; socket.onclose = function (e) { _this._resetState(); var closeObserver = _this.closeObserver; if (closeObserver) { closeObserver.next(e); } if (e.wasClean) { observer.complete(); } else { observer.error(e); } }; socket.onmessage = function (e) { var result = tryCatch(_this.resultSelector)(e); if (result === errorObject) { observer.error(errorObject.e); } else { observer.next(result); } }; }; WebSocketSubject.prototype._subscribe = function (subscriber) { var _this = this; var source = this.source; if (source) { return source.subscribe(subscriber); } if (!this.socket) { this._connectSocket(); } var subscription = new Subscription(); subscription.add(this._output.subscribe(subscriber)); subscription.add(function () { var socket = _this.socket; if (_this._output.observers.length === 0) { if (socket &amp;&amp; socket.readyState === 1) { socket.close(); } _this._resetState(); } }); return subscription; }; WebSocketSubject.prototype.unsubscribe = function () { var _a = this, source = _a.source, socket = _a.socket; if (socket &amp;&amp; socket.readyState === 1) { socket.close(); this._resetState(); } _super.prototype.unsubscribe.call(this); if (!source) { this.destination = new ReplaySubject(); } }; return WebSocketSubject; }(AnonymousSubject)); //# sourceMappingURL=WebSocketSubject.js.map</code></pre> </div> <footer class="footer"> Generated by <a href="https://esdoc.org">ESDoc<span data-ice="esdocVersion">(0.4.8)</span></a> </footer> <script src="script/search_index.js"></script> <script src="script/search.js"></script> <script src="script/pretty-print.js"></script> <script src="script/inherited-summary.js"></script> <script src="script/test-summary.js"></script> <script src="script/inner-link.js"></script> <script src="script/patch-for-local.js"></script> </body> </html>