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

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