Repositorio del curso CCOM4030 el semestre B91 del proyecto Artesanías con el Instituto de Cultura

Subscription.js 4.7KB

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