refCount.js 978 B

1234567891011121314151617181920212223242526
  1. import { operate } from '../util/lift';
  2. import { createOperatorSubscriber } from './OperatorSubscriber';
  3. export function refCount() {
  4. return operate((source, subscriber) => {
  5. let connection = null;
  6. source._refCount++;
  7. const refCounter = createOperatorSubscriber(subscriber, undefined, undefined, undefined, () => {
  8. if (!source || source._refCount <= 0 || 0 < --source._refCount) {
  9. connection = null;
  10. return;
  11. }
  12. const sharedConnection = source._connection;
  13. const conn = connection;
  14. connection = null;
  15. if (sharedConnection && (!conn || sharedConnection === conn)) {
  16. sharedConnection.unsubscribe();
  17. }
  18. subscriber.unsubscribe();
  19. });
  20. source.subscribe(refCounter);
  21. if (!refCounter.closed) {
  22. connection = source.connect();
  23. }
  24. });
  25. }
  26. //# sourceMappingURL=refCount.js.map