shareReplay.js 1.4 KB

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