Subscription.js 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. "use strict";
  2. var __values = (this && this.__values) || function(o) {
  3. var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
  4. if (m) return m.call(o);
  5. if (o && typeof o.length === "number") return {
  6. next: function () {
  7. if (o && i >= o.length) o = void 0;
  8. return { value: o && o[i++], done: !o };
  9. }
  10. };
  11. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  12. };
  13. var __read = (this && this.__read) || function (o, n) {
  14. var m = typeof Symbol === "function" && o[Symbol.iterator];
  15. if (!m) return o;
  16. var i = m.call(o), r, ar = [], e;
  17. try {
  18. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  19. }
  20. catch (error) { e = { error: error }; }
  21. finally {
  22. try {
  23. if (r && !r.done && (m = i["return"])) m.call(i);
  24. }
  25. finally { if (e) throw e.error; }
  26. }
  27. return ar;
  28. };
  29. var __spreadArray = (this && this.__spreadArray) || function (to, from) {
  30. for (var i = 0, il = from.length, j = to.length; i < il; i++, j++)
  31. to[j] = from[i];
  32. return to;
  33. };
  34. Object.defineProperty(exports, "__esModule", { value: true });
  35. exports.isSubscription = exports.EMPTY_SUBSCRIPTION = exports.Subscription = void 0;
  36. var isFunction_1 = require("./util/isFunction");
  37. var UnsubscriptionError_1 = require("./util/UnsubscriptionError");
  38. var arrRemove_1 = require("./util/arrRemove");
  39. var Subscription = (function () {
  40. function Subscription(initialTeardown) {
  41. this.initialTeardown = initialTeardown;
  42. this.closed = false;
  43. this._parentage = null;
  44. this._finalizers = null;
  45. }
  46. Subscription.prototype.unsubscribe = function () {
  47. var e_1, _a, e_2, _b;
  48. var errors;
  49. if (!this.closed) {
  50. this.closed = true;
  51. var _parentage = this._parentage;
  52. if (_parentage) {
  53. this._parentage = null;
  54. if (Array.isArray(_parentage)) {
  55. try {
  56. for (var _parentage_1 = __values(_parentage), _parentage_1_1 = _parentage_1.next(); !_parentage_1_1.done; _parentage_1_1 = _parentage_1.next()) {
  57. var parent_1 = _parentage_1_1.value;
  58. parent_1.remove(this);
  59. }
  60. }
  61. catch (e_1_1) { e_1 = { error: e_1_1 }; }
  62. finally {
  63. try {
  64. if (_parentage_1_1 && !_parentage_1_1.done && (_a = _parentage_1.return)) _a.call(_parentage_1);
  65. }
  66. finally { if (e_1) throw e_1.error; }
  67. }
  68. }
  69. else {
  70. _parentage.remove(this);
  71. }
  72. }
  73. var initialFinalizer = this.initialTeardown;
  74. if (isFunction_1.isFunction(initialFinalizer)) {
  75. try {
  76. initialFinalizer();
  77. }
  78. catch (e) {
  79. errors = e instanceof UnsubscriptionError_1.UnsubscriptionError ? e.errors : [e];
  80. }
  81. }
  82. var _finalizers = this._finalizers;
  83. if (_finalizers) {
  84. this._finalizers = null;
  85. try {
  86. for (var _finalizers_1 = __values(_finalizers), _finalizers_1_1 = _finalizers_1.next(); !_finalizers_1_1.done; _finalizers_1_1 = _finalizers_1.next()) {
  87. var finalizer = _finalizers_1_1.value;
  88. try {
  89. execFinalizer(finalizer);
  90. }
  91. catch (err) {
  92. errors = errors !== null && errors !== void 0 ? errors : [];
  93. if (err instanceof UnsubscriptionError_1.UnsubscriptionError) {
  94. errors = __spreadArray(__spreadArray([], __read(errors)), __read(err.errors));
  95. }
  96. else {
  97. errors.push(err);
  98. }
  99. }
  100. }
  101. }
  102. catch (e_2_1) { e_2 = { error: e_2_1 }; }
  103. finally {
  104. try {
  105. if (_finalizers_1_1 && !_finalizers_1_1.done && (_b = _finalizers_1.return)) _b.call(_finalizers_1);
  106. }
  107. finally { if (e_2) throw e_2.error; }
  108. }
  109. }
  110. if (errors) {
  111. throw new UnsubscriptionError_1.UnsubscriptionError(errors);
  112. }
  113. }
  114. };
  115. Subscription.prototype.add = function (teardown) {
  116. var _a;
  117. if (teardown && teardown !== this) {
  118. if (this.closed) {
  119. execFinalizer(teardown);
  120. }
  121. else {
  122. if (teardown instanceof Subscription) {
  123. if (teardown.closed || teardown._hasParent(this)) {
  124. return;
  125. }
  126. teardown._addParent(this);
  127. }
  128. (this._finalizers = (_a = this._finalizers) !== null && _a !== void 0 ? _a : []).push(teardown);
  129. }
  130. }
  131. };
  132. Subscription.prototype._hasParent = function (parent) {
  133. var _parentage = this._parentage;
  134. return _parentage === parent || (Array.isArray(_parentage) && _parentage.includes(parent));
  135. };
  136. Subscription.prototype._addParent = function (parent) {
  137. var _parentage = this._parentage;
  138. this._parentage = Array.isArray(_parentage) ? (_parentage.push(parent), _parentage) : _parentage ? [_parentage, parent] : parent;
  139. };
  140. Subscription.prototype._removeParent = function (parent) {
  141. var _parentage = this._parentage;
  142. if (_parentage === parent) {
  143. this._parentage = null;
  144. }
  145. else if (Array.isArray(_parentage)) {
  146. arrRemove_1.arrRemove(_parentage, parent);
  147. }
  148. };
  149. Subscription.prototype.remove = function (teardown) {
  150. var _finalizers = this._finalizers;
  151. _finalizers && arrRemove_1.arrRemove(_finalizers, teardown);
  152. if (teardown instanceof Subscription) {
  153. teardown._removeParent(this);
  154. }
  155. };
  156. Subscription.EMPTY = (function () {
  157. var empty = new Subscription();
  158. empty.closed = true;
  159. return empty;
  160. })();
  161. return Subscription;
  162. }());
  163. exports.Subscription = Subscription;
  164. exports.EMPTY_SUBSCRIPTION = Subscription.EMPTY;
  165. function isSubscription(value) {
  166. return (value instanceof Subscription ||
  167. (value && 'closed' in value && isFunction_1.isFunction(value.remove) && isFunction_1.isFunction(value.add) && isFunction_1.isFunction(value.unsubscribe)));
  168. }
  169. exports.isSubscription = isSubscription;
  170. function execFinalizer(finalizer) {
  171. if (isFunction_1.isFunction(finalizer)) {
  172. finalizer();
  173. }
  174. else {
  175. finalizer.unsubscribe();
  176. }
  177. }
  178. //# sourceMappingURL=Subscription.js.map