share.js 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { __read, __spreadArray } from "tslib";
  2. import { innerFrom } from '../observable/innerFrom';
  3. import { Subject } from '../Subject';
  4. import { SafeSubscriber } from '../Subscriber';
  5. import { operate } from '../util/lift';
  6. export function share(options) {
  7. if (options === void 0) { options = {}; }
  8. var _a = options.connector, connector = _a === void 0 ? function () { return new Subject(); } : _a, _b = options.resetOnError, resetOnError = _b === void 0 ? true : _b, _c = options.resetOnComplete, resetOnComplete = _c === void 0 ? true : _c, _d = options.resetOnRefCountZero, resetOnRefCountZero = _d === void 0 ? true : _d;
  9. return function (wrapperSource) {
  10. var connection;
  11. var resetConnection;
  12. var subject;
  13. var refCount = 0;
  14. var hasCompleted = false;
  15. var hasErrored = false;
  16. var cancelReset = function () {
  17. resetConnection === null || resetConnection === void 0 ? void 0 : resetConnection.unsubscribe();
  18. resetConnection = undefined;
  19. };
  20. var reset = function () {
  21. cancelReset();
  22. connection = subject = undefined;
  23. hasCompleted = hasErrored = false;
  24. };
  25. var resetAndUnsubscribe = function () {
  26. var conn = connection;
  27. reset();
  28. conn === null || conn === void 0 ? void 0 : conn.unsubscribe();
  29. };
  30. return operate(function (source, subscriber) {
  31. refCount++;
  32. if (!hasErrored && !hasCompleted) {
  33. cancelReset();
  34. }
  35. var dest = (subject = subject !== null && subject !== void 0 ? subject : connector());
  36. subscriber.add(function () {
  37. refCount--;
  38. if (refCount === 0 && !hasErrored && !hasCompleted) {
  39. resetConnection = handleReset(resetAndUnsubscribe, resetOnRefCountZero);
  40. }
  41. });
  42. dest.subscribe(subscriber);
  43. if (!connection &&
  44. refCount > 0) {
  45. connection = new SafeSubscriber({
  46. next: function (value) { return dest.next(value); },
  47. error: function (err) {
  48. hasErrored = true;
  49. cancelReset();
  50. resetConnection = handleReset(reset, resetOnError, err);
  51. dest.error(err);
  52. },
  53. complete: function () {
  54. hasCompleted = true;
  55. cancelReset();
  56. resetConnection = handleReset(reset, resetOnComplete);
  57. dest.complete();
  58. },
  59. });
  60. innerFrom(source).subscribe(connection);
  61. }
  62. })(wrapperSource);
  63. };
  64. }
  65. function handleReset(reset, on) {
  66. var args = [];
  67. for (var _i = 2; _i < arguments.length; _i++) {
  68. args[_i - 2] = arguments[_i];
  69. }
  70. if (on === true) {
  71. reset();
  72. return;
  73. }
  74. if (on === false) {
  75. return;
  76. }
  77. var onSubscriber = new SafeSubscriber({
  78. next: function () {
  79. onSubscriber.unsubscribe();
  80. reset();
  81. },
  82. });
  83. return innerFrom(on.apply(void 0, __spreadArray([], __read(args)))).subscribe(onSubscriber);
  84. }
  85. //# sourceMappingURL=share.js.map