ConnectableObservable.js 6.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /** PURE_IMPORTS_START .._Subject,.._Observable,.._Subscriber,.._Subscription,.._operators_refCount PURE_IMPORTS_END */
  2. var __extends = (this && this.__extends) || function (d, b) {
  3. for (var p in b)
  4. if (b.hasOwnProperty(p))
  5. d[p] = b[p];
  6. function __() { this.constructor = d; }
  7. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  8. };
  9. import { SubjectSubscriber } from '../Subject';
  10. import { Observable } from '../Observable';
  11. import { Subscriber } from '../Subscriber';
  12. import { Subscription } from '../Subscription';
  13. import { refCount as higherOrderRefCount } from '../operators/refCount';
  14. /**
  15. * @class ConnectableObservable<T>
  16. */
  17. export var ConnectableObservable = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  18. __extends(ConnectableObservable, _super);
  19. function ConnectableObservable(/** @deprecated internal use only */ source,
  20. /** @deprecated internal use only */ subjectFactory) {
  21. _super.call(this);
  22. this.source = source;
  23. this.subjectFactory = subjectFactory;
  24. /** @deprecated internal use only */ this._refCount = 0;
  25. this._isComplete = false;
  26. }
  27. /** @deprecated internal use only */ ConnectableObservable.prototype._subscribe = function (subscriber) {
  28. return this.getSubject().subscribe(subscriber);
  29. };
  30. /** @deprecated internal use only */ ConnectableObservable.prototype.getSubject = function () {
  31. var subject = this._subject;
  32. if (!subject || subject.isStopped) {
  33. this._subject = this.subjectFactory();
  34. }
  35. return this._subject;
  36. };
  37. ConnectableObservable.prototype.connect = function () {
  38. var connection = this._connection;
  39. if (!connection) {
  40. this._isComplete = false;
  41. connection = this._connection = new Subscription();
  42. connection.add(this.source
  43. .subscribe(new ConnectableSubscriber(this.getSubject(), this)));
  44. if (connection.closed) {
  45. this._connection = null;
  46. connection = Subscription.EMPTY;
  47. }
  48. else {
  49. this._connection = connection;
  50. }
  51. }
  52. return connection;
  53. };
  54. ConnectableObservable.prototype.refCount = function () {
  55. return higherOrderRefCount()(this);
  56. };
  57. return ConnectableObservable;
  58. }(Observable));
  59. var connectableProto = ConnectableObservable.prototype;
  60. export var connectableObservableDescriptor = {
  61. operator: { value: null },
  62. _refCount: { value: 0, writable: true },
  63. _subject: { value: null, writable: true },
  64. _connection: { value: null, writable: true },
  65. _subscribe: { value: connectableProto._subscribe },
  66. _isComplete: { value: connectableProto._isComplete, writable: true },
  67. getSubject: { value: connectableProto.getSubject },
  68. connect: { value: connectableProto.connect },
  69. refCount: { value: connectableProto.refCount }
  70. };
  71. var ConnectableSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  72. __extends(ConnectableSubscriber, _super);
  73. function ConnectableSubscriber(destination, connectable) {
  74. _super.call(this, destination);
  75. this.connectable = connectable;
  76. }
  77. ConnectableSubscriber.prototype._error = function (err) {
  78. this._unsubscribe();
  79. _super.prototype._error.call(this, err);
  80. };
  81. ConnectableSubscriber.prototype._complete = function () {
  82. this.connectable._isComplete = true;
  83. this._unsubscribe();
  84. _super.prototype._complete.call(this);
  85. };
  86. /** @deprecated internal use only */ ConnectableSubscriber.prototype._unsubscribe = function () {
  87. var connectable = this.connectable;
  88. if (connectable) {
  89. this.connectable = null;
  90. var connection = connectable._connection;
  91. connectable._refCount = 0;
  92. connectable._subject = null;
  93. connectable._connection = null;
  94. if (connection) {
  95. connection.unsubscribe();
  96. }
  97. }
  98. };
  99. return ConnectableSubscriber;
  100. }(SubjectSubscriber));
  101. var RefCountOperator = /*@__PURE__*/ (/*@__PURE__*/ function () {
  102. function RefCountOperator(connectable) {
  103. this.connectable = connectable;
  104. }
  105. RefCountOperator.prototype.call = function (subscriber, source) {
  106. var connectable = this.connectable;
  107. connectable._refCount++;
  108. var refCounter = new RefCountSubscriber(subscriber, connectable);
  109. var subscription = source.subscribe(refCounter);
  110. if (!refCounter.closed) {
  111. refCounter.connection = connectable.connect();
  112. }
  113. return subscription;
  114. };
  115. return RefCountOperator;
  116. }());
  117. var RefCountSubscriber = /*@__PURE__*/ (/*@__PURE__*/ function (_super) {
  118. __extends(RefCountSubscriber, _super);
  119. function RefCountSubscriber(destination, connectable) {
  120. _super.call(this, destination);
  121. this.connectable = connectable;
  122. }
  123. /** @deprecated internal use only */ RefCountSubscriber.prototype._unsubscribe = function () {
  124. var connectable = this.connectable;
  125. if (!connectable) {
  126. this.connection = null;
  127. return;
  128. }
  129. this.connectable = null;
  130. var refCount = connectable._refCount;
  131. if (refCount <= 0) {
  132. this.connection = null;
  133. return;
  134. }
  135. connectable._refCount = refCount - 1;
  136. if (refCount > 1) {
  137. this.connection = null;
  138. return;
  139. }
  140. ///
  141. // Compare the local RefCountSubscriber's connection Subscription to the
  142. // connection Subscription on the shared ConnectableObservable. In cases
  143. // where the ConnectableObservable source synchronously emits values, and
  144. // the RefCountSubscriber's downstream Observers synchronously unsubscribe,
  145. // execution continues to here before the RefCountOperator has a chance to
  146. // supply the RefCountSubscriber with the shared connection Subscription.
  147. // For example:
  148. // ```
  149. // Observable.range(0, 10)
  150. // .publish()
  151. // .refCount()
  152. // .take(5)
  153. // .subscribe();
  154. // ```
  155. // In order to account for this case, RefCountSubscriber should only dispose
  156. // the ConnectableObservable's shared connection Subscription if the
  157. // connection Subscription exists, *and* either:
  158. // a. RefCountSubscriber doesn't have a reference to the shared connection
  159. // Subscription yet, or,
  160. // b. RefCountSubscriber's connection Subscription reference is identical
  161. // to the shared connection Subscription
  162. ///
  163. var connection = this.connection;
  164. var sharedConnection = connectable._connection;
  165. this.connection = null;
  166. if (sharedConnection && (!connection || sharedConnection === connection)) {
  167. sharedConnection.unsubscribe();
  168. }
  169. };
  170. return RefCountSubscriber;
  171. }(Subscriber));
  172. //# sourceMappingURL=ConnectableObservable.js.map