refCount.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /** PURE_IMPORTS_START tslib,_Subscriber PURE_IMPORTS_END */
  2. import * as tslib_1 from "tslib";
  3. import { Subscriber } from '../Subscriber';
  4. export function refCount() {
  5. return function refCountOperatorFunction(source) {
  6. return source.lift(new RefCountOperator(source));
  7. };
  8. }
  9. var RefCountOperator = /*@__PURE__*/ (function () {
  10. function RefCountOperator(connectable) {
  11. this.connectable = connectable;
  12. }
  13. RefCountOperator.prototype.call = function (subscriber, source) {
  14. var connectable = this.connectable;
  15. connectable._refCount++;
  16. var refCounter = new RefCountSubscriber(subscriber, connectable);
  17. var subscription = source.subscribe(refCounter);
  18. if (!refCounter.closed) {
  19. refCounter.connection = connectable.connect();
  20. }
  21. return subscription;
  22. };
  23. return RefCountOperator;
  24. }());
  25. var RefCountSubscriber = /*@__PURE__*/ (function (_super) {
  26. tslib_1.__extends(RefCountSubscriber, _super);
  27. function RefCountSubscriber(destination, connectable) {
  28. var _this = _super.call(this, destination) || this;
  29. _this.connectable = connectable;
  30. return _this;
  31. }
  32. RefCountSubscriber.prototype._unsubscribe = function () {
  33. var connectable = this.connectable;
  34. if (!connectable) {
  35. this.connection = null;
  36. return;
  37. }
  38. this.connectable = null;
  39. var refCount = connectable._refCount;
  40. if (refCount <= 0) {
  41. this.connection = null;
  42. return;
  43. }
  44. connectable._refCount = refCount - 1;
  45. if (refCount > 1) {
  46. this.connection = null;
  47. return;
  48. }
  49. var connection = this.connection;
  50. var sharedConnection = connectable._connection;
  51. this.connection = null;
  52. if (sharedConnection && (!connection || sharedConnection === connection)) {
  53. sharedConnection.unsubscribe();
  54. }
  55. };
  56. return RefCountSubscriber;
  57. }(Subscriber));
  58. //# sourceMappingURL=refCount.js.map