ConnectableObservable.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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 Subject_1 = require("../Subject");
  17. var Observable_1 = require("../Observable");
  18. var Subscriber_1 = require("../Subscriber");
  19. var Subscription_1 = require("../Subscription");
  20. var refCount_1 = require("../operators/refCount");
  21. var ConnectableObservable = (function (_super) {
  22. __extends(ConnectableObservable, _super);
  23. function ConnectableObservable(source, subjectFactory) {
  24. var _this = _super.call(this) || this;
  25. _this.source = source;
  26. _this.subjectFactory = subjectFactory;
  27. _this._refCount = 0;
  28. _this._isComplete = false;
  29. return _this;
  30. }
  31. ConnectableObservable.prototype._subscribe = function (subscriber) {
  32. return this.getSubject().subscribe(subscriber);
  33. };
  34. ConnectableObservable.prototype.getSubject = function () {
  35. var subject = this._subject;
  36. if (!subject || subject.isStopped) {
  37. this._subject = this.subjectFactory();
  38. }
  39. return this._subject;
  40. };
  41. ConnectableObservable.prototype.connect = function () {
  42. var connection = this._connection;
  43. if (!connection) {
  44. this._isComplete = false;
  45. connection = this._connection = new Subscription_1.Subscription();
  46. connection.add(this.source
  47. .subscribe(new ConnectableSubscriber(this.getSubject(), this)));
  48. if (connection.closed) {
  49. this._connection = null;
  50. connection = Subscription_1.Subscription.EMPTY;
  51. }
  52. }
  53. return connection;
  54. };
  55. ConnectableObservable.prototype.refCount = function () {
  56. return refCount_1.refCount()(this);
  57. };
  58. return ConnectableObservable;
  59. }(Observable_1.Observable));
  60. exports.ConnectableObservable = ConnectableObservable;
  61. exports.connectableObservableDescriptor = (function () {
  62. var connectableProto = ConnectableObservable.prototype;
  63. return {
  64. operator: { value: null },
  65. _refCount: { value: 0, writable: true },
  66. _subject: { value: null, writable: true },
  67. _connection: { value: null, writable: true },
  68. _subscribe: { value: connectableProto._subscribe },
  69. _isComplete: { value: connectableProto._isComplete, writable: true },
  70. getSubject: { value: connectableProto.getSubject },
  71. connect: { value: connectableProto.connect },
  72. refCount: { value: connectableProto.refCount }
  73. };
  74. })();
  75. var ConnectableSubscriber = (function (_super) {
  76. __extends(ConnectableSubscriber, _super);
  77. function ConnectableSubscriber(destination, connectable) {
  78. var _this = _super.call(this, destination) || this;
  79. _this.connectable = connectable;
  80. return _this;
  81. }
  82. ConnectableSubscriber.prototype._error = function (err) {
  83. this._unsubscribe();
  84. _super.prototype._error.call(this, err);
  85. };
  86. ConnectableSubscriber.prototype._complete = function () {
  87. this.connectable._isComplete = true;
  88. this._unsubscribe();
  89. _super.prototype._complete.call(this);
  90. };
  91. ConnectableSubscriber.prototype._unsubscribe = function () {
  92. var connectable = this.connectable;
  93. if (connectable) {
  94. this.connectable = null;
  95. var connection = connectable._connection;
  96. connectable._refCount = 0;
  97. connectable._subject = null;
  98. connectable._connection = null;
  99. if (connection) {
  100. connection.unsubscribe();
  101. }
  102. }
  103. };
  104. return ConnectableSubscriber;
  105. }(Subject_1.SubjectSubscriber));
  106. var RefCountOperator = (function () {
  107. function RefCountOperator(connectable) {
  108. this.connectable = connectable;
  109. }
  110. RefCountOperator.prototype.call = function (subscriber, source) {
  111. var connectable = this.connectable;
  112. connectable._refCount++;
  113. var refCounter = new RefCountSubscriber(subscriber, connectable);
  114. var subscription = source.subscribe(refCounter);
  115. if (!refCounter.closed) {
  116. refCounter.connection = connectable.connect();
  117. }
  118. return subscription;
  119. };
  120. return RefCountOperator;
  121. }());
  122. var RefCountSubscriber = (function (_super) {
  123. __extends(RefCountSubscriber, _super);
  124. function RefCountSubscriber(destination, connectable) {
  125. var _this = _super.call(this, destination) || this;
  126. _this.connectable = connectable;
  127. return _this;
  128. }
  129. RefCountSubscriber.prototype._unsubscribe = function () {
  130. var connectable = this.connectable;
  131. if (!connectable) {
  132. this.connection = null;
  133. return;
  134. }
  135. this.connectable = null;
  136. var refCount = connectable._refCount;
  137. if (refCount <= 0) {
  138. this.connection = null;
  139. return;
  140. }
  141. connectable._refCount = refCount - 1;
  142. if (refCount > 1) {
  143. this.connection = null;
  144. return;
  145. }
  146. var connection = this.connection;
  147. var sharedConnection = connectable._connection;
  148. this.connection = null;
  149. if (sharedConnection && (!connection || sharedConnection === connection)) {
  150. sharedConnection.unsubscribe();
  151. }
  152. };
  153. return RefCountSubscriber;
  154. }(Subscriber_1.Subscriber));
  155. //# sourceMappingURL=ConnectableObservable.js.map