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

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