Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

WebSocketSubject.js 7.7KB

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