shareReplay.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { ReplaySubject } from '../ReplaySubject';
  2. /**
  3. * @method shareReplay
  4. * @owner Observable
  5. */
  6. export function shareReplay(bufferSize, windowTime, scheduler) {
  7. return (source) => source.lift(shareReplayOperator(bufferSize, windowTime, scheduler));
  8. }
  9. function shareReplayOperator(bufferSize, windowTime, scheduler) {
  10. let subject;
  11. let refCount = 0;
  12. let subscription;
  13. let hasError = false;
  14. let isComplete = false;
  15. return function shareReplayOperation(source) {
  16. refCount++;
  17. if (!subject || hasError) {
  18. hasError = false;
  19. subject = new ReplaySubject(bufferSize, windowTime, scheduler);
  20. subscription = source.subscribe({
  21. next(value) { subject.next(value); },
  22. error(err) {
  23. hasError = true;
  24. subject.error(err);
  25. },
  26. complete() {
  27. isComplete = true;
  28. subject.complete();
  29. },
  30. });
  31. }
  32. const innerSub = subject.subscribe(this);
  33. return () => {
  34. refCount--;
  35. innerSub.unsubscribe();
  36. if (subscription && refCount === 0 && isComplete) {
  37. subscription.unsubscribe();
  38. }
  39. };
  40. };
  41. }
  42. ;
  43. //# sourceMappingURL=shareReplay.js.map