ConnectableObservable.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /** PURE_IMPORTS_START tslib,_Subject,_Observable,_Subscriber,_Subscription,_operators_refCount PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { SubjectSubscriber } from '../Subject';
  4. import { Observable } from '../Observable';
  5. import { Subscriber } from '../Subscriber';
  6. import { Subscription } from '../Subscription';
  7. import { refCount as higherOrderRefCount } from '../operators/refCount';
  8. var ConnectableObservable = /*@__PURE__*/ (function (_super) {
  9. tslib_1.__extends(ConnectableObservable, _super);
  10. function ConnectableObservable(source, subjectFactory) {
  11. var _this = _super.call(this) || this;
  12. _this.source = source;
  13. _this.subjectFactory = subjectFactory;
  14. _this._refCount = 0;
  15. _this._isComplete = false;
  16. return _this;
  17. }
  18. ConnectableObservable.prototype._subscribe = function (subscriber) {
  19. return this.getSubject().subscribe(subscriber);
  20. };
  21. ConnectableObservable.prototype.getSubject = function () {
  22. var subject = this._subject;
  23. if (!subject || subject.isStopped) {
  24. this._subject = this.subjectFactory();
  25. }
  26. return this._subject;
  27. };
  28. ConnectableObservable.prototype.connect = function () {
  29. var connection = this._connection;
  30. if (!connection) {
  31. this._isComplete = false;
  32. connection = this._connection = new Subscription();
  33. connection.add(this.source
  34. .subscribe(new ConnectableSubscriber(this.getSubject(), this)));
  35. if (connection.closed) {
  36. this._connection = null;
  37. connection = Subscription.EMPTY;
  38. }
  39. }
  40. return connection;
  41. };
  42. ConnectableObservable.prototype.refCount = function () {
  43. return higherOrderRefCount()(this);
  44. };
  45. return ConnectableObservable;
  46. }(Observable));
  47. export { ConnectableObservable };
  48. export var connectableObservableDescriptor = /*@__PURE__*/ (function () {
  49. var connectableProto = ConnectableObservable.prototype;
  50. return {
  51. operator: { value: null },
  52. _refCount: { value: 0, writable: true },
  53. _subject: { value: null, writable: true },
  54. _connection: { value: null, writable: true },
  55. _subscribe: { value: connectableProto._subscribe },
  56. _isComplete: { value: connectableProto._isComplete, writable: true },
  57. getSubject: { value: connectableProto.getSubject },
  58. connect: { value: connectableProto.connect },
  59. refCount: { value: connectableProto.refCount }
  60. };
  61. })();
  62. var ConnectableSubscriber = /*@__PURE__*/ (function (_super) {
  63. tslib_1.__extends(ConnectableSubscriber, _super);
  64. function ConnectableSubscriber(destination, connectable) {
  65. var _this = _super.call(this, destination) || this;
  66. _this.connectable = connectable;
  67. return _this;
  68. }
  69. ConnectableSubscriber.prototype._error = function (err) {
  70. this._unsubscribe();
  71. _super.prototype._error.call(this, err);
  72. };
  73. ConnectableSubscriber.prototype._complete = function () {
  74. this.connectable._isComplete = true;
  75. this._unsubscribe();
  76. _super.prototype._complete.call(this);
  77. };
  78. ConnectableSubscriber.prototype._unsubscribe = function () {
  79. var connectable = this.connectable;
  80. if (connectable) {
  81. this.connectable = null;
  82. var connection = connectable._connection;
  83. connectable._refCount = 0;
  84. connectable._subject = null;
  85. connectable._connection = null;
  86. if (connection) {
  87. connection.unsubscribe();
  88. }
  89. }
  90. };
  91. return ConnectableSubscriber;
  92. }(SubjectSubscriber));
  93. var RefCountOperator = /*@__PURE__*/ (function () {
  94. function RefCountOperator(connectable) {
  95. this.connectable = connectable;
  96. }
  97. RefCountOperator.prototype.call = function (subscriber, source) {
  98. var connectable = this.connectable;
  99. connectable._refCount++;
  100. var refCounter = new RefCountSubscriber(subscriber, connectable);
  101. var subscription = source.subscribe(refCounter);
  102. if (!refCounter.closed) {
  103. refCounter.connection = connectable.connect();
  104. }
  105. return subscription;
  106. };
  107. return RefCountOperator;
  108. }());
  109. var RefCountSubscriber = /*@__PURE__*/ (function (_super) {
  110. tslib_1.__extends(RefCountSubscriber, _super);
  111. function RefCountSubscriber(destination, connectable) {
  112. var _this = _super.call(this, destination) || this;
  113. _this.connectable = connectable;
  114. return _this;
  115. }
  116. RefCountSubscriber.prototype._unsubscribe = function () {
  117. var connectable = this.connectable;
  118. if (!connectable) {
  119. this.connection = null;
  120. return;
  121. }
  122. this.connectable = null;
  123. var refCount = connectable._refCount;
  124. if (refCount <= 0) {
  125. this.connection = null;
  126. return;
  127. }
  128. connectable._refCount = refCount - 1;
  129. if (refCount > 1) {
  130. this.connection = null;
  131. return;
  132. }
  133. var connection = this.connection;
  134. var sharedConnection = connectable._connection;
  135. this.connection = null;
  136. if (sharedConnection && (!connection || sharedConnection === connection)) {
  137. sharedConnection.unsubscribe();
  138. }
  139. };
  140. return RefCountSubscriber;
  141. }(Subscriber));
  142. //# sourceMappingURL=ConnectableObservable.js.map