Subject.js 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { __extends, __values } from "tslib";
  2. import { Observable } from './Observable';
  3. import { Subscription, EMPTY_SUBSCRIPTION } from './Subscription';
  4. import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
  5. import { arrRemove } from './util/arrRemove';
  6. import { errorContext } from './util/errorContext';
  7. var Subject = (function (_super) {
  8. __extends(Subject, _super);
  9. function Subject() {
  10. var _this = _super.call(this) || this;
  11. _this.closed = false;
  12. _this.currentObservers = null;
  13. _this.observers = [];
  14. _this.isStopped = false;
  15. _this.hasError = false;
  16. _this.thrownError = null;
  17. return _this;
  18. }
  19. Subject.prototype.lift = function (operator) {
  20. var subject = new AnonymousSubject(this, this);
  21. subject.operator = operator;
  22. return subject;
  23. };
  24. Subject.prototype._throwIfClosed = function () {
  25. if (this.closed) {
  26. throw new ObjectUnsubscribedError();
  27. }
  28. };
  29. Subject.prototype.next = function (value) {
  30. var _this = this;
  31. errorContext(function () {
  32. var e_1, _a;
  33. _this._throwIfClosed();
  34. if (!_this.isStopped) {
  35. if (!_this.currentObservers) {
  36. _this.currentObservers = Array.from(_this.observers);
  37. }
  38. try {
  39. for (var _b = __values(_this.currentObservers), _c = _b.next(); !_c.done; _c = _b.next()) {
  40. var observer = _c.value;
  41. observer.next(value);
  42. }
  43. }
  44. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  45. finally {
  46. try {
  47. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  48. }
  49. finally { if (e_1) throw e_1.error; }
  50. }
  51. }
  52. });
  53. };
  54. Subject.prototype.error = function (err) {
  55. var _this = this;
  56. errorContext(function () {
  57. _this._throwIfClosed();
  58. if (!_this.isStopped) {
  59. _this.hasError = _this.isStopped = true;
  60. _this.thrownError = err;
  61. var observers = _this.observers;
  62. while (observers.length) {
  63. observers.shift().error(err);
  64. }
  65. }
  66. });
  67. };
  68. Subject.prototype.complete = function () {
  69. var _this = this;
  70. errorContext(function () {
  71. _this._throwIfClosed();
  72. if (!_this.isStopped) {
  73. _this.isStopped = true;
  74. var observers = _this.observers;
  75. while (observers.length) {
  76. observers.shift().complete();
  77. }
  78. }
  79. });
  80. };
  81. Subject.prototype.unsubscribe = function () {
  82. this.isStopped = this.closed = true;
  83. this.observers = this.currentObservers = null;
  84. };
  85. Object.defineProperty(Subject.prototype, "observed", {
  86. get: function () {
  87. var _a;
  88. return ((_a = this.observers) === null || _a === void 0 ? void 0 : _a.length) > 0;
  89. },
  90. enumerable: false,
  91. configurable: true
  92. });
  93. Subject.prototype._trySubscribe = function (subscriber) {
  94. this._throwIfClosed();
  95. return _super.prototype._trySubscribe.call(this, subscriber);
  96. };
  97. Subject.prototype._subscribe = function (subscriber) {
  98. this._throwIfClosed();
  99. this._checkFinalizedStatuses(subscriber);
  100. return this._innerSubscribe(subscriber);
  101. };
  102. Subject.prototype._innerSubscribe = function (subscriber) {
  103. var _this = this;
  104. var _a = this, hasError = _a.hasError, isStopped = _a.isStopped, observers = _a.observers;
  105. if (hasError || isStopped) {
  106. return EMPTY_SUBSCRIPTION;
  107. }
  108. this.currentObservers = null;
  109. observers.push(subscriber);
  110. return new Subscription(function () {
  111. _this.currentObservers = null;
  112. arrRemove(observers, subscriber);
  113. });
  114. };
  115. Subject.prototype._checkFinalizedStatuses = function (subscriber) {
  116. var _a = this, hasError = _a.hasError, thrownError = _a.thrownError, isStopped = _a.isStopped;
  117. if (hasError) {
  118. subscriber.error(thrownError);
  119. }
  120. else if (isStopped) {
  121. subscriber.complete();
  122. }
  123. };
  124. Subject.prototype.asObservable = function () {
  125. var observable = new Observable();
  126. observable.source = this;
  127. return observable;
  128. };
  129. Subject.create = function (destination, source) {
  130. return new AnonymousSubject(destination, source);
  131. };
  132. return Subject;
  133. }(Observable));
  134. export { Subject };
  135. var AnonymousSubject = (function (_super) {
  136. __extends(AnonymousSubject, _super);
  137. function AnonymousSubject(destination, source) {
  138. var _this = _super.call(this) || this;
  139. _this.destination = destination;
  140. _this.source = source;
  141. return _this;
  142. }
  143. AnonymousSubject.prototype.next = function (value) {
  144. var _a, _b;
  145. (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.next) === null || _b === void 0 ? void 0 : _b.call(_a, value);
  146. };
  147. AnonymousSubject.prototype.error = function (err) {
  148. var _a, _b;
  149. (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.error) === null || _b === void 0 ? void 0 : _b.call(_a, err);
  150. };
  151. AnonymousSubject.prototype.complete = function () {
  152. var _a, _b;
  153. (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.complete) === null || _b === void 0 ? void 0 : _b.call(_a);
  154. };
  155. AnonymousSubject.prototype._subscribe = function (subscriber) {
  156. var _a, _b;
  157. return (_b = (_a = this.source) === null || _a === void 0 ? void 0 : _a.subscribe(subscriber)) !== null && _b !== void 0 ? _b : EMPTY_SUBSCRIPTION;
  158. };
  159. return AnonymousSubject;
  160. }(Subject));
  161. export { AnonymousSubject };
  162. //# sourceMappingURL=Subject.js.map