Ohm-Management - Projektarbeit B-ME
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

Subscription.js 5.0KB

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