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 4.9KB

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