Subject.js 5.9 KB

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