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.3KB

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