shareReplay.js 2.3 KB

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