Subscriber.js 9.1 KB

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