Subscription.js 4.6 KB

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