Subject.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
  7. return extendStatics(d, b);
  8. };
  9. return function (d, b) {
  10. if (typeof b !== "function" && b !== null)
  11. throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  12. extendStatics(d, b);
  13. function __() { this.constructor = d; }
  14. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  15. };
  16. })();
  17. var __values = (this && this.__values) || function(o) {
  18. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  19. if (m) return m.call(o);
  20. if (o && typeof o.length === "number") return {
  21. next: function () {
  22. if (o && i >= o.length) o = void 0;
  23. return { value: o && o[i++], done: !o };
  24. }
  25. };
  26. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  27. };
  28. Object.defineProperty(exports, "__esModule", { value: true });
  29. exports.AnonymousSubject = exports.Subject = void 0;
  30. var Observable_1 = require("./Observable");
  31. var Subscription_1 = require("./Subscription");
  32. var ObjectUnsubscribedError_1 = require("./util/ObjectUnsubscribedError");
  33. var arrRemove_1 = require("./util/arrRemove");
  34. var errorContext_1 = require("./util/errorContext");
  35. var Subject = (function (_super) {
  36. __extends(Subject, _super);
  37. function Subject() {
  38. var _this = _super.call(this) || this;
  39. _this.closed = false;
  40. _this.currentObservers = null;
  41. _this.observers = [];
  42. _this.isStopped = false;
  43. _this.hasError = false;
  44. _this.thrownError = null;
  45. return _this;
  46. }
  47. Subject.prototype.lift = function (operator) {
  48. var subject = new AnonymousSubject(this, this);
  49. subject.operator = operator;
  50. return subject;
  51. };
  52. Subject.prototype._throwIfClosed = function () {
  53. if (this.closed) {
  54. throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
  55. }
  56. };
  57. Subject.prototype.next = function (value) {
  58. var _this = this;
  59. errorContext_1.errorContext(function () {
  60. var e_1, _a;
  61. _this._throwIfClosed();
  62. if (!_this.isStopped) {
  63. if (!_this.currentObservers) {
  64. _this.currentObservers = Array.from(_this.observers);
  65. }
  66. try {
  67. for (var _b = __values(_this.currentObservers), _c = _b.next(); !_c.done; _c = _b.next()) {
  68. var observer = _c.value;
  69. observer.next(value);
  70. }
  71. }
  72. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  73. finally {
  74. try {
  75. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  76. }
  77. finally { if (e_1) throw e_1.error; }
  78. }
  79. }
  80. });
  81. };
  82. Subject.prototype.error = function (err) {
  83. var _this = this;
  84. errorContext_1.errorContext(function () {
  85. _this._throwIfClosed();
  86. if (!_this.isStopped) {
  87. _this.hasError = _this.isStopped = true;
  88. _this.thrownError = err;
  89. var observers = _this.observers;
  90. while (observers.length) {
  91. observers.shift().error(err);
  92. }
  93. }
  94. });
  95. };
  96. Subject.prototype.complete = function () {
  97. var _this = this;
  98. errorContext_1.errorContext(function () {
  99. _this._throwIfClosed();
  100. if (!_this.isStopped) {
  101. _this.isStopped = true;
  102. var observers = _this.observers;
  103. while (observers.length) {
  104. observers.shift().complete();
  105. }
  106. }
  107. });
  108. };
  109. Subject.prototype.unsubscribe = function () {
  110. this.isStopped = this.closed = true;
  111. this.observers = this.currentObservers = null;
  112. };
  113. Object.defineProperty(Subject.prototype, "observed", {
  114. get: function () {
  115. var _a;
  116. return ((_a = this.observers) === null || _a === void 0 ? void 0 : _a.length) > 0;
  117. },
  118. enumerable: false,
  119. configurable: true
  120. });
  121. Subject.prototype._trySubscribe = function (subscriber) {
  122. this._throwIfClosed();
  123. return _super.prototype._trySubscribe.call(this, subscriber);
  124. };
  125. Subject.prototype._subscribe = function (subscriber) {
  126. this._throwIfClosed();
  127. this._checkFinalizedStatuses(subscriber);
  128. return this._innerSubscribe(subscriber);
  129. };
  130. Subject.prototype._innerSubscribe = function (subscriber) {
  131. var _this = this;
  132. var _a = this, hasError = _a.hasError, isStopped = _a.isStopped, observers = _a.observers;
  133. if (hasError || isStopped) {
  134. return Subscription_1.EMPTY_SUBSCRIPTION;
  135. }
  136. this.currentObservers = null;
  137. observers.push(subscriber);
  138. return new Subscription_1.Subscription(function () {
  139. _this.currentObservers = null;
  140. arrRemove_1.arrRemove(observers, subscriber);
  141. });
  142. };
  143. Subject.prototype._checkFinalizedStatuses = function (subscriber) {
  144. var _a = this, hasError = _a.hasError, thrownError = _a.thrownError, isStopped = _a.isStopped;
  145. if (hasError) {
  146. subscriber.error(thrownError);
  147. }
  148. else if (isStopped) {
  149. subscriber.complete();
  150. }
  151. };
  152. Subject.prototype.asObservable = function () {
  153. var observable = new Observable_1.Observable();
  154. observable.source = this;
  155. return observable;
  156. };
  157. Subject.create = function (destination, source) {
  158. return new AnonymousSubject(destination, source);
  159. };
  160. return Subject;
  161. }(Observable_1.Observable));
  162. exports.Subject = Subject;
  163. var AnonymousSubject = (function (_super) {
  164. __extends(AnonymousSubject, _super);
  165. function AnonymousSubject(destination, source) {
  166. var _this = _super.call(this) || this;
  167. _this.destination = destination;
  168. _this.source = source;
  169. return _this;
  170. }
  171. AnonymousSubject.prototype.next = function (value) {
  172. var _a, _b;
  173. (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.next) === null || _b === void 0 ? void 0 : _b.call(_a, value);
  174. };
  175. AnonymousSubject.prototype.error = function (err) {
  176. var _a, _b;
  177. (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.error) === null || _b === void 0 ? void 0 : _b.call(_a, err);
  178. };
  179. AnonymousSubject.prototype.complete = function () {
  180. var _a, _b;
  181. (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.complete) === null || _b === void 0 ? void 0 : _b.call(_a);
  182. };
  183. AnonymousSubject.prototype._subscribe = function (subscriber) {
  184. var _a, _b;
  185. return (_b = (_a = this.source) === null || _a === void 0 ? void 0 : _a.subscribe(subscriber)) !== null && _b !== void 0 ? _b : Subscription_1.EMPTY_SUBSCRIPTION;
  186. };
  187. return AnonymousSubject;
  188. }(Subject));
  189. exports.AnonymousSubject = AnonymousSubject;
  190. //# sourceMappingURL=Subject.js.map