ConnectableObservable.js 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import { __extends } from "tslib";
  2. import { Observable } from '../Observable';
  3. import { Subscription } from '../Subscription';
  4. import { refCount as higherOrderRefCount } from '../operators/refCount';
  5. import { createOperatorSubscriber } from '../operators/OperatorSubscriber';
  6. import { hasLift } from '../util/lift';
  7. var ConnectableObservable = (function (_super) {
  8. __extends(ConnectableObservable, _super);
  9. function ConnectableObservable(source, subjectFactory) {
  10. var _this = _super.call(this) || this;
  11. _this.source = source;
  12. _this.subjectFactory = subjectFactory;
  13. _this._subject = null;
  14. _this._refCount = 0;
  15. _this._connection = null;
  16. if (hasLift(source)) {
  17. _this.lift = source.lift;
  18. }
  19. return _this;
  20. }
  21. ConnectableObservable.prototype._subscribe = function (subscriber) {
  22. return this.getSubject().subscribe(subscriber);
  23. };
  24. ConnectableObservable.prototype.getSubject = function () {
  25. var subject = this._subject;
  26. if (!subject || subject.isStopped) {
  27. this._subject = this.subjectFactory();
  28. }
  29. return this._subject;
  30. };
  31. ConnectableObservable.prototype._teardown = function () {
  32. this._refCount = 0;
  33. var _connection = this._connection;
  34. this._subject = this._connection = null;
  35. _connection === null || _connection === void 0 ? void 0 : _connection.unsubscribe();
  36. };
  37. ConnectableObservable.prototype.connect = function () {
  38. var _this = this;
  39. var connection = this._connection;
  40. if (!connection) {
  41. connection = this._connection = new Subscription();
  42. var subject_1 = this.getSubject();
  43. connection.add(this.source.subscribe(createOperatorSubscriber(subject_1, undefined, function () {
  44. _this._teardown();
  45. subject_1.complete();
  46. }, function (err) {
  47. _this._teardown();
  48. subject_1.error(err);
  49. }, function () { return _this._teardown(); })));
  50. if (connection.closed) {
  51. this._connection = null;
  52. connection = Subscription.EMPTY;
  53. }
  54. }
  55. return connection;
  56. };
  57. ConnectableObservable.prototype.refCount = function () {
  58. return higherOrderRefCount()(this);
  59. };
  60. return ConnectableObservable;
  61. }(Observable));
  62. export { ConnectableObservable };
  63. //# sourceMappingURL=ConnectableObservable.js.map