Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

refCount.js 1.6KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { Subscriber } from '../Subscriber';
  2. export function refCount() {
  3. return function refCountOperatorFunction(source) {
  4. return source.lift(new RefCountOperator(source));
  5. };
  6. }
  7. class RefCountOperator {
  8. constructor(connectable) {
  9. this.connectable = connectable;
  10. }
  11. call(subscriber, source) {
  12. const { connectable } = this;
  13. connectable._refCount++;
  14. const refCounter = new RefCountSubscriber(subscriber, connectable);
  15. const subscription = source.subscribe(refCounter);
  16. if (!refCounter.closed) {
  17. refCounter.connection = connectable.connect();
  18. }
  19. return subscription;
  20. }
  21. }
  22. class RefCountSubscriber extends Subscriber {
  23. constructor(destination, connectable) {
  24. super(destination);
  25. this.connectable = connectable;
  26. }
  27. _unsubscribe() {
  28. const { connectable } = this;
  29. if (!connectable) {
  30. this.connection = null;
  31. return;
  32. }
  33. this.connectable = null;
  34. const refCount = connectable._refCount;
  35. if (refCount <= 0) {
  36. this.connection = null;
  37. return;
  38. }
  39. connectable._refCount = refCount - 1;
  40. if (refCount > 1) {
  41. this.connection = null;
  42. return;
  43. }
  44. const { connection } = this;
  45. const sharedConnection = connectable._connection;
  46. this.connection = null;
  47. if (sharedConnection && (!connection || sharedConnection === connection)) {
  48. sharedConnection.unsubscribe();
  49. }
  50. }
  51. }
  52. //# sourceMappingURL=refCount.js.map