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.

Subscriber.js 9.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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. Object.defineProperty(exports, "__esModule", { value: true });
  16. var isFunction_1 = require("./util/isFunction");
  17. var Observer_1 = require("./Observer");
  18. var Subscription_1 = require("./Subscription");
  19. var rxSubscriber_1 = require("../internal/symbol/rxSubscriber");
  20. var config_1 = require("./config");
  21. var hostReportError_1 = require("./util/hostReportError");
  22. var Subscriber = (function (_super) {
  23. __extends(Subscriber, _super);
  24. function Subscriber(destinationOrNext, error, complete) {
  25. var _this = _super.call(this) || this;
  26. _this.syncErrorValue = null;
  27. _this.syncErrorThrown = false;
  28. _this.syncErrorThrowable = false;
  29. _this.isStopped = false;
  30. _this._parentSubscription = null;
  31. switch (arguments.length) {
  32. case 0:
  33. _this.destination = Observer_1.empty;
  34. break;
  35. case 1:
  36. if (!destinationOrNext) {
  37. _this.destination = Observer_1.empty;
  38. break;
  39. }
  40. if (typeof destinationOrNext === 'object') {
  41. if (destinationOrNext instanceof Subscriber) {
  42. _this.syncErrorThrowable = destinationOrNext.syncErrorThrowable;
  43. _this.destination = destinationOrNext;
  44. destinationOrNext.add(_this);
  45. }
  46. else {
  47. _this.syncErrorThrowable = true;
  48. _this.destination = new SafeSubscriber(_this, destinationOrNext);
  49. }
  50. break;
  51. }
  52. default:
  53. _this.syncErrorThrowable = true;
  54. _this.destination = new SafeSubscriber(_this, destinationOrNext, error, complete);
  55. break;
  56. }
  57. return _this;
  58. }
  59. Subscriber.prototype[rxSubscriber_1.rxSubscriber] = function () { return this; };
  60. Subscriber.create = function (next, error, complete) {
  61. var subscriber = new Subscriber(next, error, complete);
  62. subscriber.syncErrorThrowable = false;
  63. return subscriber;
  64. };
  65. Subscriber.prototype.next = function (value) {
  66. if (!this.isStopped) {
  67. this._next(value);
  68. }
  69. };
  70. Subscriber.prototype.error = function (err) {
  71. if (!this.isStopped) {
  72. this.isStopped = true;
  73. this._error(err);
  74. }
  75. };
  76. Subscriber.prototype.complete = function () {
  77. if (!this.isStopped) {
  78. this.isStopped = true;
  79. this._complete();
  80. }
  81. };
  82. Subscriber.prototype.unsubscribe = function () {
  83. if (this.closed) {
  84. return;
  85. }
  86. this.isStopped = true;
  87. _super.prototype.unsubscribe.call(this);
  88. };
  89. Subscriber.prototype._next = function (value) {
  90. this.destination.next(value);
  91. };
  92. Subscriber.prototype._error = function (err) {
  93. this.destination.error(err);
  94. this.unsubscribe();
  95. };
  96. Subscriber.prototype._complete = function () {
  97. this.destination.complete();
  98. this.unsubscribe();
  99. };
  100. Subscriber.prototype._unsubscribeAndRecycle = function () {
  101. var _a = this, _parent = _a._parent, _parents = _a._parents;
  102. this._parent = null;
  103. this._parents = null;
  104. this.unsubscribe();
  105. this.closed = false;
  106. this.isStopped = false;
  107. this._parent = _parent;
  108. this._parents = _parents;
  109. this._parentSubscription = null;
  110. return this;
  111. };
  112. return Subscriber;
  113. }(Subscription_1.Subscription));
  114. exports.Subscriber = Subscriber;
  115. var SafeSubscriber = (function (_super) {
  116. __extends(SafeSubscriber, _super);
  117. function SafeSubscriber(_parentSubscriber, observerOrNext, error, complete) {
  118. var _this = _super.call(this) || this;
  119. _this._parentSubscriber = _parentSubscriber;
  120. var next;
  121. var context = _this;
  122. if (isFunction_1.isFunction(observerOrNext)) {
  123. next = observerOrNext;
  124. }
  125. else if (observerOrNext) {
  126. next = observerOrNext.next;
  127. error = observerOrNext.error;
  128. complete = observerOrNext.complete;
  129. if (observerOrNext !== Observer_1.empty) {
  130. context = Object.create(observerOrNext);
  131. if (isFunction_1.isFunction(context.unsubscribe)) {
  132. _this.add(context.unsubscribe.bind(context));
  133. }
  134. context.unsubscribe = _this.unsubscribe.bind(_this);
  135. }
  136. }
  137. _this._context = context;
  138. _this._next = next;
  139. _this._error = error;
  140. _this._complete = complete;
  141. return _this;
  142. }
  143. SafeSubscriber.prototype.next = function (value) {
  144. if (!this.isStopped && this._next) {
  145. var _parentSubscriber = this._parentSubscriber;
  146. if (!config_1.config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  147. this.__tryOrUnsub(this._next, value);
  148. }
  149. else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
  150. this.unsubscribe();
  151. }
  152. }
  153. };
  154. SafeSubscriber.prototype.error = function (err) {
  155. if (!this.isStopped) {
  156. var _parentSubscriber = this._parentSubscriber;
  157. var useDeprecatedSynchronousErrorHandling = config_1.config.useDeprecatedSynchronousErrorHandling;
  158. if (this._error) {
  159. if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  160. this.__tryOrUnsub(this._error, err);
  161. this.unsubscribe();
  162. }
  163. else {
  164. this.__tryOrSetError(_parentSubscriber, this._error, err);
  165. this.unsubscribe();
  166. }
  167. }
  168. else if (!_parentSubscriber.syncErrorThrowable) {
  169. this.unsubscribe();
  170. if (useDeprecatedSynchronousErrorHandling) {
  171. throw err;
  172. }
  173. hostReportError_1.hostReportError(err);
  174. }
  175. else {
  176. if (useDeprecatedSynchronousErrorHandling) {
  177. _parentSubscriber.syncErrorValue = err;
  178. _parentSubscriber.syncErrorThrown = true;
  179. }
  180. else {
  181. hostReportError_1.hostReportError(err);
  182. }
  183. this.unsubscribe();
  184. }
  185. }
  186. };
  187. SafeSubscriber.prototype.complete = function () {
  188. var _this = this;
  189. if (!this.isStopped) {
  190. var _parentSubscriber = this._parentSubscriber;
  191. if (this._complete) {
  192. var wrappedComplete = function () { return _this._complete.call(_this._context); };
  193. if (!config_1.config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  194. this.__tryOrUnsub(wrappedComplete);
  195. this.unsubscribe();
  196. }
  197. else {
  198. this.__tryOrSetError(_parentSubscriber, wrappedComplete);
  199. this.unsubscribe();
  200. }
  201. }
  202. else {
  203. this.unsubscribe();
  204. }
  205. }
  206. };
  207. SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) {
  208. try {
  209. fn.call(this._context, value);
  210. }
  211. catch (err) {
  212. this.unsubscribe();
  213. if (config_1.config.useDeprecatedSynchronousErrorHandling) {
  214. throw err;
  215. }
  216. else {
  217. hostReportError_1.hostReportError(err);
  218. }
  219. }
  220. };
  221. SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) {
  222. if (!config_1.config.useDeprecatedSynchronousErrorHandling) {
  223. throw new Error('bad call');
  224. }
  225. try {
  226. fn.call(this._context, value);
  227. }
  228. catch (err) {
  229. if (config_1.config.useDeprecatedSynchronousErrorHandling) {
  230. parent.syncErrorValue = err;
  231. parent.syncErrorThrown = true;
  232. return true;
  233. }
  234. else {
  235. hostReportError_1.hostReportError(err);
  236. return true;
  237. }
  238. }
  239. return false;
  240. };
  241. SafeSubscriber.prototype._unsubscribe = function () {
  242. var _parentSubscriber = this._parentSubscriber;
  243. this._context = null;
  244. this._parentSubscriber = null;
  245. _parentSubscriber.unsubscribe();
  246. };
  247. return SafeSubscriber;
  248. }(Subscriber));
  249. exports.SafeSubscriber = SafeSubscriber;
  250. //# sourceMappingURL=Subscriber.js.map