Subject.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /** PURE_IMPORTS_START tslib,_Observable,_Subscriber,_Subscription,_util_ObjectUnsubscribedError,_SubjectSubscription,_internal_symbol_rxSubscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Observable } from './Observable';
  4. import { Subscriber } from './Subscriber';
  5. import { Subscription } from './Subscription';
  6. import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError';
  7. import { SubjectSubscription } from './SubjectSubscription';
  8. import { rxSubscriber as rxSubscriberSymbol } from '../internal/symbol/rxSubscriber';
  9. var SubjectSubscriber = /*@__PURE__*/ (function (_super) {
  10. tslib_1.__extends(SubjectSubscriber, _super);
  11. function SubjectSubscriber(destination) {
  12. var _this = _super.call(this, destination) || this;
  13. _this.destination = destination;
  14. return _this;
  15. }
  16. return SubjectSubscriber;
  17. }(Subscriber));
  18. export { SubjectSubscriber };
  19. var Subject = /*@__PURE__*/ (function (_super) {
  20. tslib_1.__extends(Subject, _super);
  21. function Subject() {
  22. var _this = _super.call(this) || this;
  23. _this.observers = [];
  24. _this.closed = false;
  25. _this.isStopped = false;
  26. _this.hasError = false;
  27. _this.thrownError = null;
  28. return _this;
  29. }
  30. Subject.prototype[rxSubscriberSymbol] = function () {
  31. return new SubjectSubscriber(this);
  32. };
  33. Subject.prototype.lift = function (operator) {
  34. var subject = new AnonymousSubject(this, this);
  35. subject.operator = operator;
  36. return subject;
  37. };
  38. Subject.prototype.next = function (value) {
  39. if (this.closed) {
  40. throw new ObjectUnsubscribedError();
  41. }
  42. if (!this.isStopped) {
  43. var observers = this.observers;
  44. var len = observers.length;
  45. var copy = observers.slice();
  46. for (var i = 0; i < len; i++) {
  47. copy[i].next(value);
  48. }
  49. }
  50. };
  51. Subject.prototype.error = function (err) {
  52. if (this.closed) {
  53. throw new ObjectUnsubscribedError();
  54. }
  55. this.hasError = true;
  56. this.thrownError = err;
  57. this.isStopped = true;
  58. var observers = this.observers;
  59. var len = observers.length;
  60. var copy = observers.slice();
  61. for (var i = 0; i < len; i++) {
  62. copy[i].error(err);
  63. }
  64. this.observers.length = 0;
  65. };
  66. Subject.prototype.complete = function () {
  67. if (this.closed) {
  68. throw new ObjectUnsubscribedError();
  69. }
  70. this.isStopped = true;
  71. var observers = this.observers;
  72. var len = observers.length;
  73. var copy = observers.slice();
  74. for (var i = 0; i < len; i++) {
  75. copy[i].complete();
  76. }
  77. this.observers.length = 0;
  78. };
  79. Subject.prototype.unsubscribe = function () {
  80. this.isStopped = true;
  81. this.closed = true;
  82. this.observers = null;
  83. };
  84. Subject.prototype._trySubscribe = function (subscriber) {
  85. if (this.closed) {
  86. throw new ObjectUnsubscribedError();
  87. }
  88. else {
  89. return _super.prototype._trySubscribe.call(this, subscriber);
  90. }
  91. };
  92. Subject.prototype._subscribe = function (subscriber) {
  93. if (this.closed) {
  94. throw new ObjectUnsubscribedError();
  95. }
  96. else if (this.hasError) {
  97. subscriber.error(this.thrownError);
  98. return Subscription.EMPTY;
  99. }
  100. else if (this.isStopped) {
  101. subscriber.complete();
  102. return Subscription.EMPTY;
  103. }
  104. else {
  105. this.observers.push(subscriber);
  106. return new SubjectSubscription(this, subscriber);
  107. }
  108. };
  109. Subject.prototype.asObservable = function () {
  110. var observable = new Observable();
  111. observable.source = this;
  112. return observable;
  113. };
  114. Subject.create = function (destination, source) {
  115. return new AnonymousSubject(destination, source);
  116. };
  117. return Subject;
  118. }(Observable));
  119. export { Subject };
  120. var AnonymousSubject = /*@__PURE__*/ (function (_super) {
  121. tslib_1.__extends(AnonymousSubject, _super);
  122. function AnonymousSubject(destination, source) {
  123. var _this = _super.call(this) || this;
  124. _this.destination = destination;
  125. _this.source = source;
  126. return _this;
  127. }
  128. AnonymousSubject.prototype.next = function (value) {
  129. var destination = this.destination;
  130. if (destination && destination.next) {
  131. destination.next(value);
  132. }
  133. };
  134. AnonymousSubject.prototype.error = function (err) {
  135. var destination = this.destination;
  136. if (destination && destination.error) {
  137. this.destination.error(err);
  138. }
  139. };
  140. AnonymousSubject.prototype.complete = function () {
  141. var destination = this.destination;
  142. if (destination && destination.complete) {
  143. this.destination.complete();
  144. }
  145. };
  146. AnonymousSubject.prototype._subscribe = function (subscriber) {
  147. var source = this.source;
  148. if (source) {
  149. return this.source.subscribe(subscriber);
  150. }
  151. else {
  152. return Subscription.EMPTY;
  153. }
  154. };
  155. return AnonymousSubject;
  156. }(Subject));
  157. export { AnonymousSubject };
  158. //# sourceMappingURL=Subject.js.map