WebSocketSubject.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. "use strict";
  2. var __extends = (this && this.__extends) || (function () {
  3. var extendStatics = function (d, b) {
  4. extendStatics = Object.setPrototypeOf ||
  5. ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
  6. function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __assign = (this && this.__assign) || function () {
  18. __assign = Object.assign || function(t) {
  19. for (var s, i = 1, n = arguments.length; i < n; i++) {
  20. s = arguments[i];
  21. for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
  22. t[p] = s[p];
  23. }
  24. return t;
  25. };
  26. return __assign.apply(this, arguments);
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.WebSocketSubject = void 0;
  30. var Subject_1 = require("../../Subject");
  31. var Subscriber_1 = require("../../Subscriber");
  32. var Observable_1 = require("../../Observable");
  33. var Subscription_1 = require("../../Subscription");
  34. var ReplaySubject_1 = require("../../ReplaySubject");
  35. var DEFAULT_WEBSOCKET_CONFIG = {
  36. url: '',
  37. deserializer: function (e) { return JSON.parse(e.data); },
  38. serializer: function (value) { return JSON.stringify(value); },
  39. };
  40. var WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT = 'WebSocketSubject.error must be called with an object with an error code, and an optional reason: { code: number, reason: string }';
  41. var WebSocketSubject = (function (_super) {
  42. __extends(WebSocketSubject, _super);
  43. function WebSocketSubject(urlConfigOrSource, destination) {
  44. var _this = _super.call(this) || this;
  45. _this._socket = null;
  46. if (urlConfigOrSource instanceof Observable_1.Observable) {
  47. _this.destination = destination;
  48. _this.source = urlConfigOrSource;
  49. }
  50. else {
  51. var config = (_this._config = __assign({}, DEFAULT_WEBSOCKET_CONFIG));
  52. _this._output = new Subject_1.Subject();
  53. if (typeof urlConfigOrSource === 'string') {
  54. config.url = urlConfigOrSource;
  55. }
  56. else {
  57. for (var key in urlConfigOrSource) {
  58. if (urlConfigOrSource.hasOwnProperty(key)) {
  59. config[key] = urlConfigOrSource[key];
  60. }
  61. }
  62. }
  63. if (!config.WebSocketCtor && WebSocket) {
  64. config.WebSocketCtor = WebSocket;
  65. }
  66. else if (!config.WebSocketCtor) {
  67. throw new Error('no WebSocket constructor can be found');
  68. }
  69. _this.destination = new ReplaySubject_1.ReplaySubject();
  70. }
  71. return _this;
  72. }
  73. WebSocketSubject.prototype.lift = function (operator) {
  74. var sock = new WebSocketSubject(this._config, this.destination);
  75. sock.operator = operator;
  76. sock.source = this;
  77. return sock;
  78. };
  79. WebSocketSubject.prototype._resetState = function () {
  80. this._socket = null;
  81. if (!this.source) {
  82. this.destination = new ReplaySubject_1.ReplaySubject();
  83. }
  84. this._output = new Subject_1.Subject();
  85. };
  86. WebSocketSubject.prototype.multiplex = function (subMsg, unsubMsg, messageFilter) {
  87. var self = this;
  88. return new Observable_1.Observable(function (observer) {
  89. try {
  90. self.next(subMsg());
  91. }
  92. catch (err) {
  93. observer.error(err);
  94. }
  95. var subscription = self.subscribe({
  96. next: function (x) {
  97. try {
  98. if (messageFilter(x)) {
  99. observer.next(x);
  100. }
  101. }
  102. catch (err) {
  103. observer.error(err);
  104. }
  105. },
  106. error: function (err) { return observer.error(err); },
  107. complete: function () { return observer.complete(); },
  108. });
  109. return function () {
  110. try {
  111. self.next(unsubMsg());
  112. }
  113. catch (err) {
  114. observer.error(err);
  115. }
  116. subscription.unsubscribe();
  117. };
  118. });
  119. };
  120. WebSocketSubject.prototype._connectSocket = function () {
  121. var _this = this;
  122. var _a = this._config, WebSocketCtor = _a.WebSocketCtor, protocol = _a.protocol, url = _a.url, binaryType = _a.binaryType;
  123. var observer = this._output;
  124. var socket = null;
  125. try {
  126. socket = protocol ? new WebSocketCtor(url, protocol) : new WebSocketCtor(url);
  127. this._socket = socket;
  128. if (binaryType) {
  129. this._socket.binaryType = binaryType;
  130. }
  131. }
  132. catch (e) {
  133. observer.error(e);
  134. return;
  135. }
  136. var subscription = new Subscription_1.Subscription(function () {
  137. _this._socket = null;
  138. if (socket && socket.readyState === 1) {
  139. socket.close();
  140. }
  141. });
  142. socket.onopen = function (evt) {
  143. var _socket = _this._socket;
  144. if (!_socket) {
  145. socket.close();
  146. _this._resetState();
  147. return;
  148. }
  149. var openObserver = _this._config.openObserver;
  150. if (openObserver) {
  151. openObserver.next(evt);
  152. }
  153. var queue = _this.destination;
  154. _this.destination = Subscriber_1.Subscriber.create(function (x) {
  155. if (socket.readyState === 1) {
  156. try {
  157. var serializer = _this._config.serializer;
  158. socket.send(serializer(x));
  159. }
  160. catch (e) {
  161. _this.destination.error(e);
  162. }
  163. }
  164. }, function (err) {
  165. var closingObserver = _this._config.closingObserver;
  166. if (closingObserver) {
  167. closingObserver.next(undefined);
  168. }
  169. if (err && err.code) {
  170. socket.close(err.code, err.reason);
  171. }
  172. else {
  173. observer.error(new TypeError(WEBSOCKETSUBJECT_INVALID_ERROR_OBJECT));
  174. }
  175. _this._resetState();
  176. }, function () {
  177. var closingObserver = _this._config.closingObserver;
  178. if (closingObserver) {
  179. closingObserver.next(undefined);
  180. }
  181. socket.close();
  182. _this._resetState();
  183. });
  184. if (queue && queue instanceof ReplaySubject_1.ReplaySubject) {
  185. subscription.add(queue.subscribe(_this.destination));
  186. }
  187. };
  188. socket.onerror = function (e) {
  189. _this._resetState();
  190. observer.error(e);
  191. };
  192. socket.onclose = function (e) {
  193. if (socket === _this._socket) {
  194. _this._resetState();
  195. }
  196. var closeObserver = _this._config.closeObserver;
  197. if (closeObserver) {
  198. closeObserver.next(e);
  199. }
  200. if (e.wasClean) {
  201. observer.complete();
  202. }
  203. else {
  204. observer.error(e);
  205. }
  206. };
  207. socket.onmessage = function (e) {
  208. try {
  209. var deserializer = _this._config.deserializer;
  210. observer.next(deserializer(e));
  211. }
  212. catch (err) {
  213. observer.error(err);
  214. }
  215. };
  216. };
  217. WebSocketSubject.prototype._subscribe = function (subscriber) {
  218. var _this = this;
  219. var source = this.source;
  220. if (source) {
  221. return source.subscribe(subscriber);
  222. }
  223. if (!this._socket) {
  224. this._connectSocket();
  225. }
  226. this._output.subscribe(subscriber);
  227. subscriber.add(function () {
  228. var _socket = _this._socket;
  229. if (_this._output.observers.length === 0) {
  230. if (_socket && (_socket.readyState === 1 || _socket.readyState === 0)) {
  231. _socket.close();
  232. }
  233. _this._resetState();
  234. }
  235. });
  236. return subscriber;
  237. };
  238. WebSocketSubject.prototype.unsubscribe = function () {
  239. var _socket = this._socket;
  240. if (_socket && (_socket.readyState === 1 || _socket.readyState === 0)) {
  241. _socket.close();
  242. }
  243. this._resetState();
  244. _super.prototype.unsubscribe.call(this);
  245. };
  246. return WebSocketSubject;
  247. }(Subject_1.AnonymousSubject));
  248. exports.WebSocketSubject = WebSocketSubject;
  249. //# sourceMappingURL=WebSocketSubject.js.map