ConnectableObservable.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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. var connectableProto = ConnectableObservable.prototype;
  62. exports.connectableObservableDescriptor = {
  63. operator: { value: null },
  64. _refCount: { value: 0, writable: true },
  65. _subject: { value: null, writable: true },
  66. _connection: { value: null, writable: true },
  67. _subscribe: { value: connectableProto._subscribe },
  68. _isComplete: { value: connectableProto._isComplete, writable: true },
  69. getSubject: { value: connectableProto.getSubject },
  70. connect: { value: connectableProto.connect },
  71. refCount: { value: connectableProto.refCount }
  72. };
  73. var ConnectableSubscriber = (function (_super) {
  74. __extends(ConnectableSubscriber, _super);
  75. function ConnectableSubscriber(destination, connectable) {
  76. var _this = _super.call(this, destination) || this;
  77. _this.connectable = connectable;
  78. return _this;
  79. }
  80. ConnectableSubscriber.prototype._error = function (err) {
  81. this._unsubscribe();
  82. _super.prototype._error.call(this, err);
  83. };
  84. ConnectableSubscriber.prototype._complete = function () {
  85. this.connectable._isComplete = true;
  86. this._unsubscribe();
  87. _super.prototype._complete.call(this);
  88. };
  89. ConnectableSubscriber.prototype._unsubscribe = function () {
  90. var connectable = this.connectable;
  91. if (connectable) {
  92. this.connectable = null;
  93. var connection = connectable._connection;
  94. connectable._refCount = 0;
  95. connectable._subject = null;
  96. connectable._connection = null;
  97. if (connection) {
  98. connection.unsubscribe();
  99. }
  100. }
  101. };
  102. return ConnectableSubscriber;
  103. }(Subject_1.SubjectSubscriber));
  104. var RefCountOperator = (function () {
  105. function RefCountOperator(connectable) {
  106. this.connectable = connectable;
  107. }
  108. RefCountOperator.prototype.call = function (subscriber, source) {
  109. var connectable = this.connectable;
  110. connectable._refCount++;
  111. var refCounter = new RefCountSubscriber(subscriber, connectable);
  112. var subscription = source.subscribe(refCounter);
  113. if (!refCounter.closed) {
  114. refCounter.connection = connectable.connect();
  115. }
  116. return subscription;
  117. };
  118. return RefCountOperator;
  119. }());
  120. var RefCountSubscriber = (function (_super) {
  121. __extends(RefCountSubscriber, _super);
  122. function RefCountSubscriber(destination, connectable) {
  123. var _this = _super.call(this, destination) || this;
  124. _this.connectable = connectable;
  125. return _this;
  126. }
  127. RefCountSubscriber.prototype._unsubscribe = function () {
  128. var connectable = this.connectable;
  129. if (!connectable) {
  130. this.connection = null;
  131. return;
  132. }
  133. this.connectable = null;
  134. var refCount = connectable._refCount;
  135. if (refCount <= 0) {
  136. this.connection = null;
  137. return;
  138. }
  139. connectable._refCount = refCount - 1;
  140. if (refCount > 1) {
  141. this.connection = null;
  142. return;
  143. }
  144. var connection = this.connection;
  145. var sharedConnection = connectable._connection;
  146. this.connection = null;
  147. if (sharedConnection && (!connection || sharedConnection === connection)) {
  148. sharedConnection.unsubscribe();
  149. }
  150. };
  151. return RefCountSubscriber;
  152. }(Subscriber_1.Subscriber));
  153. //# sourceMappingURL=ConnectableObservable.js.map