Subscriber.js 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.EMPTY_OBSERVER = exports.SafeSubscriber = exports.Subscriber = void 0;
  19. var isFunction_1 = require("./util/isFunction");
  20. var Subscription_1 = require("./Subscription");
  21. var config_1 = require("./config");
  22. var reportUnhandledError_1 = require("./util/reportUnhandledError");
  23. var noop_1 = require("./util/noop");
  24. var NotificationFactories_1 = require("./NotificationFactories");
  25. var timeoutProvider_1 = require("./scheduler/timeoutProvider");
  26. var errorContext_1 = require("./util/errorContext");
  27. var Subscriber = (function (_super) {
  28. __extends(Subscriber, _super);
  29. function Subscriber(destination) {
  30. var _this = _super.call(this) || this;
  31. _this.isStopped = false;
  32. if (destination) {
  33. _this.destination = destination;
  34. if (Subscription_1.isSubscription(destination)) {
  35. destination.add(_this);
  36. }
  37. }
  38. else {
  39. _this.destination = exports.EMPTY_OBSERVER;
  40. }
  41. return _this;
  42. }
  43. Subscriber.create = function (next, error, complete) {
  44. return new SafeSubscriber(next, error, complete);
  45. };
  46. Subscriber.prototype.next = function (value) {
  47. if (this.isStopped) {
  48. handleStoppedNotification(NotificationFactories_1.nextNotification(value), this);
  49. }
  50. else {
  51. this._next(value);
  52. }
  53. };
  54. Subscriber.prototype.error = function (err) {
  55. if (this.isStopped) {
  56. handleStoppedNotification(NotificationFactories_1.errorNotification(err), this);
  57. }
  58. else {
  59. this.isStopped = true;
  60. this._error(err);
  61. }
  62. };
  63. Subscriber.prototype.complete = function () {
  64. if (this.isStopped) {
  65. handleStoppedNotification(NotificationFactories_1.COMPLETE_NOTIFICATION, this);
  66. }
  67. else {
  68. this.isStopped = true;
  69. this._complete();
  70. }
  71. };
  72. Subscriber.prototype.unsubscribe = function () {
  73. if (!this.closed) {
  74. this.isStopped = true;
  75. _super.prototype.unsubscribe.call(this);
  76. this.destination = null;
  77. }
  78. };
  79. Subscriber.prototype._next = function (value) {
  80. this.destination.next(value);
  81. };
  82. Subscriber.prototype._error = function (err) {
  83. try {
  84. this.destination.error(err);
  85. }
  86. finally {
  87. this.unsubscribe();
  88. }
  89. };
  90. Subscriber.prototype._complete = function () {
  91. try {
  92. this.destination.complete();
  93. }
  94. finally {
  95. this.unsubscribe();
  96. }
  97. };
  98. return Subscriber;
  99. }(Subscription_1.Subscription));
  100. exports.Subscriber = Subscriber;
  101. var _bind = Function.prototype.bind;
  102. function bind(fn, thisArg) {
  103. return _bind.call(fn, thisArg);
  104. }
  105. var ConsumerObserver = (function () {
  106. function ConsumerObserver(partialObserver) {
  107. this.partialObserver = partialObserver;
  108. }
  109. ConsumerObserver.prototype.next = function (value) {
  110. var partialObserver = this.partialObserver;
  111. if (partialObserver.next) {
  112. try {
  113. partialObserver.next(value);
  114. }
  115. catch (error) {
  116. handleUnhandledError(error);
  117. }
  118. }
  119. };
  120. ConsumerObserver.prototype.error = function (err) {
  121. var partialObserver = this.partialObserver;
  122. if (partialObserver.error) {
  123. try {
  124. partialObserver.error(err);
  125. }
  126. catch (error) {
  127. handleUnhandledError(error);
  128. }
  129. }
  130. else {
  131. handleUnhandledError(err);
  132. }
  133. };
  134. ConsumerObserver.prototype.complete = function () {
  135. var partialObserver = this.partialObserver;
  136. if (partialObserver.complete) {
  137. try {
  138. partialObserver.complete();
  139. }
  140. catch (error) {
  141. handleUnhandledError(error);
  142. }
  143. }
  144. };
  145. return ConsumerObserver;
  146. }());
  147. var SafeSubscriber = (function (_super) {
  148. __extends(SafeSubscriber, _super);
  149. function SafeSubscriber(observerOrNext, error, complete) {
  150. var _this = _super.call(this) || this;
  151. var partialObserver;
  152. if (isFunction_1.isFunction(observerOrNext) || !observerOrNext) {
  153. partialObserver = {
  154. next: (observerOrNext !== null && observerOrNext !== void 0 ? observerOrNext : undefined),
  155. error: error !== null && error !== void 0 ? error : undefined,
  156. complete: complete !== null && complete !== void 0 ? complete : undefined,
  157. };
  158. }
  159. else {
  160. var context_1;
  161. if (_this && config_1.config.useDeprecatedNextContext) {
  162. context_1 = Object.create(observerOrNext);
  163. context_1.unsubscribe = function () { return _this.unsubscribe(); };
  164. partialObserver = {
  165. next: observerOrNext.next && bind(observerOrNext.next, context_1),
  166. error: observerOrNext.error && bind(observerOrNext.error, context_1),
  167. complete: observerOrNext.complete && bind(observerOrNext.complete, context_1),
  168. };
  169. }
  170. else {
  171. partialObserver = observerOrNext;
  172. }
  173. }
  174. _this.destination = new ConsumerObserver(partialObserver);
  175. return _this;
  176. }
  177. return SafeSubscriber;
  178. }(Subscriber));
  179. exports.SafeSubscriber = SafeSubscriber;
  180. function handleUnhandledError(error) {
  181. if (config_1.config.useDeprecatedSynchronousErrorHandling) {
  182. errorContext_1.captureError(error);
  183. }
  184. else {
  185. reportUnhandledError_1.reportUnhandledError(error);
  186. }
  187. }
  188. function defaultErrorHandler(err) {
  189. throw err;
  190. }
  191. function handleStoppedNotification(notification, subscriber) {
  192. var onStoppedNotification = config_1.config.onStoppedNotification;
  193. onStoppedNotification && timeoutProvider_1.timeoutProvider.setTimeout(function () { return onStoppedNotification(notification, subscriber); });
  194. }
  195. exports.EMPTY_OBSERVER = {
  196. closed: true,
  197. next: noop_1.noop,
  198. error: defaultErrorHandler,
  199. complete: noop_1.noop,
  200. };
  201. //# sourceMappingURL=Subscriber.js.map