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 4.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import { SubjectSubscriber } from '../Subject';
  2. import { Observable } from '../Observable';
  3. import { Subscriber } from '../Subscriber';
  4. import { Subscription } from '../Subscription';
  5. import { refCount as higherOrderRefCount } from '../operators/refCount';
  6. export class ConnectableObservable extends Observable {
  7. constructor(source, subjectFactory) {
  8. super();
  9. this.source = source;
  10. this.subjectFactory = subjectFactory;
  11. this._refCount = 0;
  12. this._isComplete = false;
  13. }
  14. _subscribe(subscriber) {
  15. return this.getSubject().subscribe(subscriber);
  16. }
  17. getSubject() {
  18. const subject = this._subject;
  19. if (!subject || subject.isStopped) {
  20. this._subject = this.subjectFactory();
  21. }
  22. return this._subject;
  23. }
  24. connect() {
  25. let connection = this._connection;
  26. if (!connection) {
  27. this._isComplete = false;
  28. connection = this._connection = new Subscription();
  29. connection.add(this.source
  30. .subscribe(new ConnectableSubscriber(this.getSubject(), this)));
  31. if (connection.closed) {
  32. this._connection = null;
  33. connection = Subscription.EMPTY;
  34. }
  35. }
  36. return connection;
  37. }
  38. refCount() {
  39. return higherOrderRefCount()(this);
  40. }
  41. }
  42. const connectableProto = ConnectableObservable.prototype;
  43. export const connectableObservableDescriptor = {
  44. operator: { value: null },
  45. _refCount: { value: 0, writable: true },
  46. _subject: { value: null, writable: true },
  47. _connection: { value: null, writable: true },
  48. _subscribe: { value: connectableProto._subscribe },
  49. _isComplete: { value: connectableProto._isComplete, writable: true },
  50. getSubject: { value: connectableProto.getSubject },
  51. connect: { value: connectableProto.connect },
  52. refCount: { value: connectableProto.refCount }
  53. };
  54. class ConnectableSubscriber extends SubjectSubscriber {
  55. constructor(destination, connectable) {
  56. super(destination);
  57. this.connectable = connectable;
  58. }
  59. _error(err) {
  60. this._unsubscribe();
  61. super._error(err);
  62. }
  63. _complete() {
  64. this.connectable._isComplete = true;
  65. this._unsubscribe();
  66. super._complete();
  67. }
  68. _unsubscribe() {
  69. const connectable = this.connectable;
  70. if (connectable) {
  71. this.connectable = null;
  72. const connection = connectable._connection;
  73. connectable._refCount = 0;
  74. connectable._subject = null;
  75. connectable._connection = null;
  76. if (connection) {
  77. connection.unsubscribe();
  78. }
  79. }
  80. }
  81. }
  82. class RefCountOperator {
  83. constructor(connectable) {
  84. this.connectable = connectable;
  85. }
  86. call(subscriber, source) {
  87. const { connectable } = this;
  88. connectable._refCount++;
  89. const refCounter = new RefCountSubscriber(subscriber, connectable);
  90. const subscription = source.subscribe(refCounter);
  91. if (!refCounter.closed) {
  92. refCounter.connection = connectable.connect();
  93. }
  94. return subscription;
  95. }
  96. }
  97. class RefCountSubscriber extends Subscriber {
  98. constructor(destination, connectable) {
  99. super(destination);
  100. this.connectable = connectable;
  101. }
  102. _unsubscribe() {
  103. const { connectable } = this;
  104. if (!connectable) {
  105. this.connection = null;
  106. return;
  107. }
  108. this.connectable = null;
  109. const refCount = connectable._refCount;
  110. if (refCount <= 0) {
  111. this.connection = null;
  112. return;
  113. }
  114. connectable._refCount = refCount - 1;
  115. if (refCount > 1) {
  116. this.connection = null;
  117. return;
  118. }
  119. const { connection } = this;
  120. const sharedConnection = connectable._connection;
  121. this.connection = null;
  122. if (sharedConnection && (!connection || sharedConnection === connection)) {
  123. sharedConnection.unsubscribe();
  124. }
  125. }
  126. }
  127. //# sourceMappingURL=ConnectableObservable.js.map