Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ConnectableObservable.js 5.8KB

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