Subscriber.js 5.9 KB

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