shareReplay.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. /** PURE_IMPORTS_START _ReplaySubject PURE_IMPORTS_END */
  2. import { ReplaySubject } from '../ReplaySubject';
  3. export function shareReplay(configOrBufferSize, windowTime, scheduler) {
  4. var config;
  5. if (configOrBufferSize && typeof configOrBufferSize === 'object') {
  6. config = configOrBufferSize;
  7. }
  8. else {
  9. config = {
  10. bufferSize: configOrBufferSize,
  11. windowTime: windowTime,
  12. refCount: false,
  13. scheduler: scheduler,
  14. };
  15. }
  16. return function (source) { return source.lift(shareReplayOperator(config)); };
  17. }
  18. function shareReplayOperator(_a) {
  19. var _b = _a.bufferSize, bufferSize = _b === void 0 ? Number.POSITIVE_INFINITY : _b, _c = _a.windowTime, windowTime = _c === void 0 ? Number.POSITIVE_INFINITY : _c, useRefCount = _a.refCount, scheduler = _a.scheduler;
  20. var subject;
  21. var refCount = 0;
  22. var subscription;
  23. var hasError = false;
  24. var isComplete = false;
  25. return function shareReplayOperation(source) {
  26. refCount++;
  27. var innerSub;
  28. if (!subject || hasError) {
  29. hasError = false;
  30. subject = new ReplaySubject(bufferSize, windowTime, scheduler);
  31. innerSub = subject.subscribe(this);
  32. subscription = source.subscribe({
  33. next: function (value) {
  34. subject.next(value);
  35. },
  36. error: function (err) {
  37. hasError = true;
  38. subject.error(err);
  39. },
  40. complete: function () {
  41. isComplete = true;
  42. subscription = undefined;
  43. subject.complete();
  44. },
  45. });
  46. if (isComplete) {
  47. subscription = undefined;
  48. }
  49. }
  50. else {
  51. innerSub = subject.subscribe(this);
  52. }
  53. this.add(function () {
  54. refCount--;
  55. innerSub.unsubscribe();
  56. innerSub = undefined;
  57. if (subscription && !isComplete && useRefCount && refCount === 0) {
  58. subscription.unsubscribe();
  59. subscription = undefined;
  60. subject = undefined;
  61. }
  62. });
  63. };
  64. }
  65. //# sourceMappingURL=shareReplay.js.map