Subject.js 5.5KB

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