shareReplay.js 1.4KB

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