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 8.7KB

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