Subject.js 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. "use strict";
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p];
  4. function __() { this.constructor = d; }
  5. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  6. };
  7. var Observable_1 = require('./Observable');
  8. var Subscriber_1 = require('./Subscriber');
  9. var Subscription_1 = require('./Subscription');
  10. var ObjectUnsubscribedError_1 = require('./util/ObjectUnsubscribedError');
  11. var SubjectSubscription_1 = require('./SubjectSubscription');
  12. var rxSubscriber_1 = require('./symbol/rxSubscriber');
  13. /**
  14. * @class SubjectSubscriber<T>
  15. */
  16. var SubjectSubscriber = (function (_super) {
  17. __extends(SubjectSubscriber, _super);
  18. function SubjectSubscriber(destination) {
  19. _super.call(this, destination);
  20. this.destination = destination;
  21. }
  22. return SubjectSubscriber;
  23. }(Subscriber_1.Subscriber));
  24. exports.SubjectSubscriber = SubjectSubscriber;
  25. /**
  26. * @class Subject<T>
  27. */
  28. var Subject = (function (_super) {
  29. __extends(Subject, _super);
  30. function Subject() {
  31. _super.call(this);
  32. this.observers = [];
  33. this.closed = false;
  34. this.isStopped = false;
  35. this.hasError = false;
  36. this.thrownError = null;
  37. }
  38. Subject.prototype[rxSubscriber_1.rxSubscriber] = function () {
  39. return new SubjectSubscriber(this);
  40. };
  41. Subject.prototype.lift = function (operator) {
  42. var subject = new AnonymousSubject(this, this);
  43. subject.operator = operator;
  44. return subject;
  45. };
  46. Subject.prototype.next = function (value) {
  47. if (this.closed) {
  48. throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
  49. }
  50. if (!this.isStopped) {
  51. var observers = this.observers;
  52. var len = observers.length;
  53. var copy = observers.slice();
  54. for (var i = 0; i < len; i++) {
  55. copy[i].next(value);
  56. }
  57. }
  58. };
  59. Subject.prototype.error = function (err) {
  60. if (this.closed) {
  61. throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
  62. }
  63. this.hasError = true;
  64. this.thrownError = err;
  65. this.isStopped = true;
  66. var observers = this.observers;
  67. var len = observers.length;
  68. var copy = observers.slice();
  69. for (var i = 0; i < len; i++) {
  70. copy[i].error(err);
  71. }
  72. this.observers.length = 0;
  73. };
  74. Subject.prototype.complete = function () {
  75. if (this.closed) {
  76. throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
  77. }
  78. this.isStopped = true;
  79. var observers = this.observers;
  80. var len = observers.length;
  81. var copy = observers.slice();
  82. for (var i = 0; i < len; i++) {
  83. copy[i].complete();
  84. }
  85. this.observers.length = 0;
  86. };
  87. Subject.prototype.unsubscribe = function () {
  88. this.isStopped = true;
  89. this.closed = true;
  90. this.observers = null;
  91. };
  92. Subject.prototype._trySubscribe = function (subscriber) {
  93. if (this.closed) {
  94. throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
  95. }
  96. else {
  97. return _super.prototype._trySubscribe.call(this, subscriber);
  98. }
  99. };
  100. /** @deprecated internal use only */ Subject.prototype._subscribe = function (subscriber) {
  101. if (this.closed) {
  102. throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
  103. }
  104. else if (this.hasError) {
  105. subscriber.error(this.thrownError);
  106. return Subscription_1.Subscription.EMPTY;
  107. }
  108. else if (this.isStopped) {
  109. subscriber.complete();
  110. return Subscription_1.Subscription.EMPTY;
  111. }
  112. else {
  113. this.observers.push(subscriber);
  114. return new SubjectSubscription_1.SubjectSubscription(this, subscriber);
  115. }
  116. };
  117. Subject.prototype.asObservable = function () {
  118. var observable = new Observable_1.Observable();
  119. observable.source = this;
  120. return observable;
  121. };
  122. Subject.create = function (destination, source) {
  123. return new AnonymousSubject(destination, source);
  124. };
  125. return Subject;
  126. }(Observable_1.Observable));
  127. exports.Subject = Subject;
  128. /**
  129. * @class AnonymousSubject<T>
  130. */
  131. var AnonymousSubject = (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_1.Subscription.EMPTY;
  163. }
  164. };
  165. return AnonymousSubject;
  166. }(Subject));
  167. exports.AnonymousSubject = AnonymousSubject;
  168. //# sourceMappingURL=Subject.js.map