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

Subscription.ts 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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. import { SubscriptionLike, TeardownLogic } from './types';
  6. /**
  7. * Represents a disposable resource, such as the execution of an Observable. A
  8. * Subscription has one important method, `unsubscribe`, that takes no argument
  9. * and just disposes the resource held by the subscription.
  10. *
  11. * Additionally, subscriptions may be grouped together through the `add()`
  12. * method, which will attach a child Subscription to the current Subscription.
  13. * When a Subscription is unsubscribed, all its children (and its grandchildren)
  14. * will be unsubscribed as well.
  15. *
  16. * @class Subscription
  17. */
  18. export class Subscription implements SubscriptionLike {
  19. /** @nocollapse */
  20. public static EMPTY: Subscription = (function(empty: any) {
  21. empty.closed = true;
  22. return empty;
  23. }(new Subscription()));
  24. /**
  25. * A flag to indicate whether this Subscription has already been unsubscribed.
  26. * @type {boolean}
  27. */
  28. public closed: boolean = false;
  29. /** @internal */
  30. protected _parentOrParents: Subscription | Subscription[] = null;
  31. /** @internal */
  32. private _subscriptions: SubscriptionLike[] = null;
  33. /**
  34. * @param {function(): void} [unsubscribe] A function describing how to
  35. * perform the disposal of resources when the `unsubscribe` method is called.
  36. */
  37. constructor(unsubscribe?: () => void) {
  38. if (unsubscribe) {
  39. (this as any)._ctorUnsubscribe = true;
  40. (this as any)._unsubscribe = unsubscribe;
  41. }
  42. }
  43. /**
  44. * Disposes the resources held by the subscription. May, for instance, cancel
  45. * an ongoing Observable execution or cancel any other type of work that
  46. * started when the Subscription was created.
  47. * @return {void}
  48. */
  49. unsubscribe(): void {
  50. let errors: any[];
  51. if (this.closed) {
  52. return;
  53. }
  54. let { _parentOrParents, _ctorUnsubscribe, _unsubscribe, _subscriptions } = (this as any);
  55. this.closed = true;
  56. this._parentOrParents = null;
  57. // null out _subscriptions first so any child subscriptions that attempt
  58. // to remove themselves from this subscription will noop
  59. this._subscriptions = null;
  60. if (_parentOrParents instanceof Subscription) {
  61. _parentOrParents.remove(this);
  62. } else if (_parentOrParents !== null) {
  63. for (let index = 0; index < _parentOrParents.length; ++index) {
  64. const parent = _parentOrParents[index];
  65. parent.remove(this);
  66. }
  67. }
  68. if (isFunction(_unsubscribe)) {
  69. // It's only possible to null _unsubscribe - to release the reference to
  70. // any teardown function passed in the constructor - if the property was
  71. // actually assigned in the constructor, as there are some classes that
  72. // are derived from Subscriber (which derives from Subscription) that
  73. // implement an _unsubscribe method as a mechanism for obtaining
  74. // unsubscription notifications and some of those subscribers are
  75. // recycled. Also, in some of those subscribers, _unsubscribe switches
  76. // from a prototype method to an instance property - see notifyNext in
  77. // RetryWhenSubscriber.
  78. if (_ctorUnsubscribe) {
  79. (this as any)._unsubscribe = undefined;
  80. }
  81. try {
  82. _unsubscribe.call(this);
  83. } catch (e) {
  84. errors = e instanceof UnsubscriptionError ? flattenUnsubscriptionErrors(e.errors) : [e];
  85. }
  86. }
  87. if (isArray(_subscriptions)) {
  88. let index = -1;
  89. let len = _subscriptions.length;
  90. while (++index < len) {
  91. const sub = _subscriptions[index];
  92. if (isObject(sub)) {
  93. try {
  94. sub.unsubscribe();
  95. } catch (e) {
  96. errors = errors || [];
  97. if (e instanceof UnsubscriptionError) {
  98. errors = errors.concat(flattenUnsubscriptionErrors(e.errors));
  99. } else {
  100. errors.push(e);
  101. }
  102. }
  103. }
  104. }
  105. }
  106. if (errors) {
  107. throw new UnsubscriptionError(errors);
  108. }
  109. }
  110. /**
  111. * Adds a tear down to be called during the unsubscribe() of this
  112. * Subscription. Can also be used to add a child subscription.
  113. *
  114. * If the tear down being added is a subscription that is already
  115. * unsubscribed, is the same reference `add` is being called on, or is
  116. * `Subscription.EMPTY`, it will not be added.
  117. *
  118. * If this subscription is already in an `closed` state, the passed
  119. * tear down logic will be executed immediately.
  120. *
  121. * When a parent subscription is unsubscribed, any child subscriptions that were added to it are also unsubscribed.
  122. *
  123. * @param {TeardownLogic} teardown The additional logic to execute on
  124. * teardown.
  125. * @return {Subscription} Returns the Subscription used or created to be
  126. * added to the inner subscriptions list. This Subscription can be used with
  127. * `remove()` to remove the passed teardown logic from the inner subscriptions
  128. * list.
  129. */
  130. add(teardown: TeardownLogic): Subscription {
  131. let subscription = (<Subscription>teardown);
  132. if (!teardown) {
  133. return Subscription.EMPTY;
  134. }
  135. switch (typeof teardown) {
  136. case 'function':
  137. subscription = new Subscription(<(() => void)>teardown);
  138. case 'object':
  139. if (subscription === this || subscription.closed || typeof subscription.unsubscribe !== 'function') {
  140. // This also covers the case where `subscription` is `Subscription.EMPTY`, which is always in `closed` state.
  141. return subscription;
  142. } else if (this.closed) {
  143. subscription.unsubscribe();
  144. return subscription;
  145. } else if (!(subscription instanceof Subscription)) {
  146. const tmp = subscription;
  147. subscription = new Subscription();
  148. subscription._subscriptions = [tmp];
  149. }
  150. break;
  151. default: {
  152. throw new Error('unrecognized teardown ' + teardown + ' added to Subscription.');
  153. }
  154. }
  155. // Add `this` as parent of `subscription` if that's not already the case.
  156. let { _parentOrParents } = subscription;
  157. if (_parentOrParents === null) {
  158. // If we don't have a parent, then set `subscription._parents` to
  159. // the `this`, which is the common case that we optimize for.
  160. subscription._parentOrParents = this;
  161. } else if (_parentOrParents instanceof Subscription) {
  162. if (_parentOrParents === this) {
  163. // The `subscription` already has `this` as a parent.
  164. return subscription;
  165. }
  166. // If there's already one parent, but not multiple, allocate an
  167. // Array to store the rest of the parent Subscriptions.
  168. subscription._parentOrParents = [_parentOrParents, this];
  169. } else if (_parentOrParents.indexOf(this) === -1) {
  170. // Only add `this` to the _parentOrParents list if it's not already there.
  171. _parentOrParents.push(this);
  172. } else {
  173. // The `subscription` already has `this` as a parent.
  174. return subscription;
  175. }
  176. // Optimize for the common case when adding the first subscription.
  177. const subscriptions = this._subscriptions;
  178. if (subscriptions === null) {
  179. this._subscriptions = [subscription];
  180. } else {
  181. subscriptions.push(subscription);
  182. }
  183. return subscription;
  184. }
  185. /**
  186. * Removes a Subscription from the internal list of subscriptions that will
  187. * unsubscribe during the unsubscribe process of this Subscription.
  188. * @param {Subscription} subscription The subscription to remove.
  189. * @return {void}
  190. */
  191. remove(subscription: Subscription): void {
  192. const subscriptions = this._subscriptions;
  193. if (subscriptions) {
  194. const subscriptionIndex = subscriptions.indexOf(subscription);
  195. if (subscriptionIndex !== -1) {
  196. subscriptions.splice(subscriptionIndex, 1);
  197. }
  198. }
  199. }
  200. }
  201. function flattenUnsubscriptionErrors(errors: any[]) {
  202. return errors.reduce((errs, err) => errs.concat((err instanceof UnsubscriptionError) ? err.errors : err), []);
  203. }