Subscription.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import { __read, __spreadArray, __values } from "tslib";
  2. import { isFunction } from './util/isFunction';
  3. import { UnsubscriptionError } from './util/UnsubscriptionError';
  4. import { arrRemove } from './util/arrRemove';
  5. var Subscription = (function () {
  6. function Subscription(initialTeardown) {
  7. this.initialTeardown = initialTeardown;
  8. this.closed = false;
  9. this._parentage = null;
  10. this._finalizers = null;
  11. }
  12. Subscription.prototype.unsubscribe = function () {
  13. var e_1, _a, e_2, _b;
  14. var errors;
  15. if (!this.closed) {
  16. this.closed = true;
  17. var _parentage = this._parentage;
  18. if (_parentage) {
  19. this._parentage = null;
  20. if (Array.isArray(_parentage)) {
  21. try {
  22. for (var _parentage_1 = __values(_parentage), _parentage_1_1 = _parentage_1.next(); !_parentage_1_1.done; _parentage_1_1 = _parentage_1.next()) {
  23. var parent_1 = _parentage_1_1.value;
  24. parent_1.remove(this);
  25. }
  26. }
  27. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  28. finally {
  29. try {
  30. if (_parentage_1_1 && !_parentage_1_1.done && (_a = _parentage_1.return)) _a.call(_parentage_1);
  31. }
  32. finally { if (e_1) throw e_1.error; }
  33. }
  34. }
  35. else {
  36. _parentage.remove(this);
  37. }
  38. }
  39. var initialFinalizer = this.initialTeardown;
  40. if (isFunction(initialFinalizer)) {
  41. try {
  42. initialFinalizer();
  43. }
  44. catch (e) {
  45. errors = e instanceof UnsubscriptionError ? e.errors : [e];
  46. }
  47. }
  48. var _finalizers = this._finalizers;
  49. if (_finalizers) {
  50. this._finalizers = null;
  51. try {
  52. for (var _finalizers_1 = __values(_finalizers), _finalizers_1_1 = _finalizers_1.next(); !_finalizers_1_1.done; _finalizers_1_1 = _finalizers_1.next()) {
  53. var finalizer = _finalizers_1_1.value;
  54. try {
  55. execFinalizer(finalizer);
  56. }
  57. catch (err) {
  58. errors = errors !== null && errors !== void 0 ? errors : [];
  59. if (err instanceof UnsubscriptionError) {
  60. errors = __spreadArray(__spreadArray([], __read(errors)), __read(err.errors));
  61. }
  62. else {
  63. errors.push(err);
  64. }
  65. }
  66. }
  67. }
  68. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  69. finally {
  70. try {
  71. if (_finalizers_1_1 && !_finalizers_1_1.done && (_b = _finalizers_1.return)) _b.call(_finalizers_1);
  72. }
  73. finally { if (e_2) throw e_2.error; }
  74. }
  75. }
  76. if (errors) {
  77. throw new UnsubscriptionError(errors);
  78. }
  79. }
  80. };
  81. Subscription.prototype.add = function (teardown) {
  82. var _a;
  83. if (teardown && teardown !== this) {
  84. if (this.closed) {
  85. execFinalizer(teardown);
  86. }
  87. else {
  88. if (teardown instanceof Subscription) {
  89. if (teardown.closed || teardown._hasParent(this)) {
  90. return;
  91. }
  92. teardown._addParent(this);
  93. }
  94. (this._finalizers = (_a = this._finalizers) !== null && _a !== void 0 ? _a : []).push(teardown);
  95. }
  96. }
  97. };
  98. Subscription.prototype._hasParent = function (parent) {
  99. var _parentage = this._parentage;
  100. return _parentage === parent || (Array.isArray(_parentage) && _parentage.includes(parent));
  101. };
  102. Subscription.prototype._addParent = function (parent) {
  103. var _parentage = this._parentage;
  104. this._parentage = Array.isArray(_parentage) ? (_parentage.push(parent), _parentage) : _parentage ? [_parentage, parent] : parent;
  105. };
  106. Subscription.prototype._removeParent = function (parent) {
  107. var _parentage = this._parentage;
  108. if (_parentage === parent) {
  109. this._parentage = null;
  110. }
  111. else if (Array.isArray(_parentage)) {
  112. arrRemove(_parentage, parent);
  113. }
  114. };
  115. Subscription.prototype.remove = function (teardown) {
  116. var _finalizers = this._finalizers;
  117. _finalizers && arrRemove(_finalizers, teardown);
  118. if (teardown instanceof Subscription) {
  119. teardown._removeParent(this);
  120. }
  121. };
  122. Subscription.EMPTY = (function () {
  123. var empty = new Subscription();
  124. empty.closed = true;
  125. return empty;
  126. })();
  127. return Subscription;
  128. }());
  129. export { Subscription };
  130. export var EMPTY_SUBSCRIPTION = Subscription.EMPTY;
  131. export function isSubscription(value) {
  132. return (value instanceof Subscription ||
  133. (value && 'closed' in value && isFunction(value.remove) && isFunction(value.add) && isFunction(value.unsubscribe)));
  134. }
  135. function execFinalizer(finalizer) {
  136. if (isFunction(finalizer)) {
  137. finalizer();
  138. }
  139. else {
  140. finalizer.unsubscribe();
  141. }
  142. }
  143. //# sourceMappingURL=Subscription.js.map