Subscriber.js 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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 _parentOrParents = this._parentOrParents;
  101. this._parentOrParents = null;
  102. this.unsubscribe();
  103. this.closed = false;
  104. this.isStopped = false;
  105. this._parentOrParents = _parentOrParents;
  106. return this;
  107. };
  108. return Subscriber;
  109. }(Subscription_1.Subscription));
  110. exports.Subscriber = Subscriber;
  111. var SafeSubscriber = (function (_super) {
  112. __extends(SafeSubscriber, _super);
  113. function SafeSubscriber(_parentSubscriber, observerOrNext, error, complete) {
  114. var _this = _super.call(this) || this;
  115. _this._parentSubscriber = _parentSubscriber;
  116. var next;
  117. var context = _this;
  118. if (isFunction_1.isFunction(observerOrNext)) {
  119. next = observerOrNext;
  120. }
  121. else if (observerOrNext) {
  122. next = observerOrNext.next;
  123. error = observerOrNext.error;
  124. complete = observerOrNext.complete;
  125. if (observerOrNext !== Observer_1.empty) {
  126. context = Object.create(observerOrNext);
  127. if (isFunction_1.isFunction(context.unsubscribe)) {
  128. _this.add(context.unsubscribe.bind(context));
  129. }
  130. context.unsubscribe = _this.unsubscribe.bind(_this);
  131. }
  132. }
  133. _this._context = context;
  134. _this._next = next;
  135. _this._error = error;
  136. _this._complete = complete;
  137. return _this;
  138. }
  139. SafeSubscriber.prototype.next = function (value) {
  140. if (!this.isStopped && this._next) {
  141. var _parentSubscriber = this._parentSubscriber;
  142. if (!config_1.config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  143. this.__tryOrUnsub(this._next, value);
  144. }
  145. else if (this.__tryOrSetError(_parentSubscriber, this._next, value)) {
  146. this.unsubscribe();
  147. }
  148. }
  149. };
  150. SafeSubscriber.prototype.error = function (err) {
  151. if (!this.isStopped) {
  152. var _parentSubscriber = this._parentSubscriber;
  153. var useDeprecatedSynchronousErrorHandling = config_1.config.useDeprecatedSynchronousErrorHandling;
  154. if (this._error) {
  155. if (!useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  156. this.__tryOrUnsub(this._error, err);
  157. this.unsubscribe();
  158. }
  159. else {
  160. this.__tryOrSetError(_parentSubscriber, this._error, err);
  161. this.unsubscribe();
  162. }
  163. }
  164. else if (!_parentSubscriber.syncErrorThrowable) {
  165. this.unsubscribe();
  166. if (useDeprecatedSynchronousErrorHandling) {
  167. throw err;
  168. }
  169. hostReportError_1.hostReportError(err);
  170. }
  171. else {
  172. if (useDeprecatedSynchronousErrorHandling) {
  173. _parentSubscriber.syncErrorValue = err;
  174. _parentSubscriber.syncErrorThrown = true;
  175. }
  176. else {
  177. hostReportError_1.hostReportError(err);
  178. }
  179. this.unsubscribe();
  180. }
  181. }
  182. };
  183. SafeSubscriber.prototype.complete = function () {
  184. var _this = this;
  185. if (!this.isStopped) {
  186. var _parentSubscriber = this._parentSubscriber;
  187. if (this._complete) {
  188. var wrappedComplete = function () { return _this._complete.call(_this._context); };
  189. if (!config_1.config.useDeprecatedSynchronousErrorHandling || !_parentSubscriber.syncErrorThrowable) {
  190. this.__tryOrUnsub(wrappedComplete);
  191. this.unsubscribe();
  192. }
  193. else {
  194. this.__tryOrSetError(_parentSubscriber, wrappedComplete);
  195. this.unsubscribe();
  196. }
  197. }
  198. else {
  199. this.unsubscribe();
  200. }
  201. }
  202. };
  203. SafeSubscriber.prototype.__tryOrUnsub = function (fn, value) {
  204. try {
  205. fn.call(this._context, value);
  206. }
  207. catch (err) {
  208. this.unsubscribe();
  209. if (config_1.config.useDeprecatedSynchronousErrorHandling) {
  210. throw err;
  211. }
  212. else {
  213. hostReportError_1.hostReportError(err);
  214. }
  215. }
  216. };
  217. SafeSubscriber.prototype.__tryOrSetError = function (parent, fn, value) {
  218. if (!config_1.config.useDeprecatedSynchronousErrorHandling) {
  219. throw new Error('bad call');
  220. }
  221. try {
  222. fn.call(this._context, value);
  223. }
  224. catch (err) {
  225. if (config_1.config.useDeprecatedSynchronousErrorHandling) {
  226. parent.syncErrorValue = err;
  227. parent.syncErrorThrown = true;
  228. return true;
  229. }
  230. else {
  231. hostReportError_1.hostReportError(err);
  232. return true;
  233. }
  234. }
  235. return false;
  236. };
  237. SafeSubscriber.prototype._unsubscribe = function () {
  238. var _parentSubscriber = this._parentSubscriber;
  239. this._context = null;
  240. this._parentSubscriber = null;
  241. _parentSubscriber.unsubscribe();
  242. };
  243. return SafeSubscriber;
  244. }(Subscriber));
  245. exports.SafeSubscriber = SafeSubscriber;
  246. //# sourceMappingURL=Subscriber.js.map