Subscription.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /** PURE_IMPORTS_START _util_isArray,_util_isObject,_util_isFunction,_util_UnsubscriptionError PURE_IMPORTS_END */
  2. import { isArray } from './util/isArray';
  3. import { isObject } from './util/isObject';
  4. import { isFunction } from './util/isFunction';
  5. import { UnsubscriptionError } from './util/UnsubscriptionError';
  6. var Subscription = /*@__PURE__*/ (function () {
  7. function Subscription(unsubscribe) {
  8. this.closed = false;
  9. this._parent = null;
  10. this._parents = null;
  11. this._subscriptions = null;
  12. if (unsubscribe) {
  13. this._unsubscribe = unsubscribe;
  14. }
  15. }
  16. Subscription.prototype.unsubscribe = function () {
  17. var hasErrors = false;
  18. var errors;
  19. if (this.closed) {
  20. return;
  21. }
  22. var _a = this, _parent = _a._parent, _parents = _a._parents, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions;
  23. this.closed = true;
  24. this._parent = null;
  25. this._parents = null;
  26. this._subscriptions = null;
  27. var index = -1;
  28. var len = _parents ? _parents.length : 0;
  29. while (_parent) {
  30. _parent.remove(this);
  31. _parent = ++index < len && _parents[index] || null;
  32. }
  33. if (isFunction(_unsubscribe)) {
  34. try {
  35. _unsubscribe.call(this);
  36. }
  37. catch (e) {
  38. hasErrors = true;
  39. errors = e instanceof UnsubscriptionError ? flattenUnsubscriptionErrors(e.errors) : [e];
  40. }
  41. }
  42. if (isArray(_subscriptions)) {
  43. index = -1;
  44. len = _subscriptions.length;
  45. while (++index < len) {
  46. var sub = _subscriptions[index];
  47. if (isObject(sub)) {
  48. try {
  49. sub.unsubscribe();
  50. }
  51. catch (e) {
  52. hasErrors = true;
  53. errors = errors || [];
  54. if (e instanceof UnsubscriptionError) {
  55. errors = errors.concat(flattenUnsubscriptionErrors(e.errors));
  56. }
  57. else {
  58. errors.push(e);
  59. }
  60. }
  61. }
  62. }
  63. }
  64. if (hasErrors) {
  65. throw new UnsubscriptionError(errors);
  66. }
  67. };
  68. Subscription.prototype.add = function (teardown) {
  69. var subscription = teardown;
  70. switch (typeof teardown) {
  71. case 'function':
  72. subscription = new Subscription(teardown);
  73. case 'object':
  74. if (subscription === this || subscription.closed || typeof subscription.unsubscribe !== 'function') {
  75. return subscription;
  76. }
  77. else if (this.closed) {
  78. subscription.unsubscribe();
  79. return subscription;
  80. }
  81. else if (!(subscription instanceof Subscription)) {
  82. var tmp = subscription;
  83. subscription = new Subscription();
  84. subscription._subscriptions = [tmp];
  85. }
  86. break;
  87. default: {
  88. if (!teardown) {
  89. return Subscription.EMPTY;
  90. }
  91. throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.');
  92. }
  93. }
  94. if (subscription._addParent(this)) {
  95. var subscriptions = this._subscriptions;
  96. if (subscriptions) {
  97. subscriptions.push(subscription);
  98. }
  99. else {
  100. this._subscriptions = [subscription];
  101. }
  102. }
  103. return subscription;
  104. };
  105. Subscription.prototype.remove = function (subscription) {
  106. var subscriptions = this._subscriptions;
  107. if (subscriptions) {
  108. var subscriptionIndex = subscriptions.indexOf(subscription);
  109. if (subscriptionIndex !== -1) {
  110. subscriptions.splice(subscriptionIndex, 1);
  111. }
  112. }
  113. };
  114. Subscription.prototype._addParent = function (parent) {
  115. var _a = this, _parent = _a._parent, _parents = _a._parents;
  116. if (_parent === parent) {
  117. return false;
  118. }
  119. else if (!_parent) {
  120. this._parent = parent;
  121. return true;
  122. }
  123. else if (!_parents) {
  124. this._parents = [parent];
  125. return true;
  126. }
  127. else if (_parents.indexOf(parent) === -1) {
  128. _parents.push(parent);
  129. return true;
  130. }
  131. return false;
  132. };
  133. Subscription.EMPTY = (function (empty) {
  134. empty.closed = true;
  135. return empty;
  136. }(new Subscription()));
  137. return Subscription;
  138. }());
  139. export { Subscription };
  140. function flattenUnsubscriptionErrors(errors) {
  141. return errors.reduce(function (errs, err) { return errs.concat((err instanceof UnsubscriptionError) ? err.errors : err); }, []);
  142. }
  143. //# sourceMappingURL=Subscription.js.map