Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WebSocketSubject.js 9.2KB

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