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.5KB

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