d6dd64a44b61020c48c625076d9a9f9675d34128be9d98ef12874636be003544dd0eca1ac74a28816b553a3aea3d94de17c230fb629125fc9cd315763d6c0b 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import { isFunction } from './util/isFunction';
  2. import { UnsubscriptionError } from './util/UnsubscriptionError';
  3. import { SubscriptionLike, TeardownLogic, Unsubscribable } from './types';
  4. import { arrRemove } from './util/arrRemove';
  5. /**
  6. * Represents a disposable resource, such as the execution of an Observable. A
  7. * Subscription has one important method, `unsubscribe`, that takes no argument
  8. * and just disposes the resource held by the subscription.
  9. *
  10. * Additionally, subscriptions may be grouped together through the `add()`
  11. * method, which will attach a child Subscription to the current Subscription.
  12. * When a Subscription is unsubscribed, all its children (and its grandchildren)
  13. * will be unsubscribed as well.
  14. */
  15. export class Subscription implements SubscriptionLike {
  16. public static EMPTY = (() => {
  17. const empty = new Subscription();
  18. empty.closed = true;
  19. return empty;
  20. })();
  21. /**
  22. * A flag to indicate whether this Subscription has already been unsubscribed.
  23. */
  24. public closed = false;
  25. private _parentage: Subscription[] | Subscription | null = null;
  26. /**
  27. * The list of registered finalizers to execute upon unsubscription. Adding and removing from this
  28. * list occurs in the {@link #add} and {@link #remove} methods.
  29. */
  30. private _finalizers: Exclude<TeardownLogic, void>[] | null = null;
  31. /**
  32. * @param initialTeardown A function executed first as part of the finalization
  33. * process that is kicked off when {@link #unsubscribe} is called.
  34. */
  35. constructor(private initialTeardown?: () => void) {}
  36. /**
  37. * Disposes the resources held by the subscription. May, for instance, cancel
  38. * an ongoing Observable execution or cancel any other type of work that
  39. * started when the Subscription was created.
  40. */
  41. unsubscribe(): void {
  42. let errors: any[] | undefined;
  43. if (!this.closed) {
  44. this.closed = true;
  45. // Remove this from it's parents.
  46. const { _parentage } = this;
  47. if (_parentage) {
  48. this._parentage = null;
  49. if (Array.isArray(_parentage)) {
  50. for (const parent of _parentage) {
  51. parent.remove(this);
  52. }
  53. } else {
  54. _parentage.remove(this);
  55. }
  56. }
  57. const { initialTeardown: initialFinalizer } = this;
  58. if (isFunction(initialFinalizer)) {
  59. try {
  60. initialFinalizer();
  61. } catch (e) {
  62. errors = e instanceof UnsubscriptionError ? e.errors : [e];
  63. }
  64. }
  65. const { _finalizers } = this;
  66. if (_finalizers) {
  67. this._finalizers = null;
  68. for (const finalizer of _finalizers) {
  69. try {
  70. execFinalizer(finalizer);
  71. } catch (err) {
  72. errors = errors ?? [];
  73. if (err instanceof UnsubscriptionError) {
  74. errors = [...errors, ...err.errors];
  75. } else {
  76. errors.push(err);
  77. }
  78. }
  79. }
  80. }
  81. if (errors) {
  82. throw new UnsubscriptionError(errors);
  83. }
  84. }
  85. }
  86. /**
  87. * Adds a finalizer to this subscription, so that finalization will be unsubscribed/called
  88. * when this subscription is unsubscribed. If this subscription is already {@link #closed},
  89. * because it has already been unsubscribed, then whatever finalizer is passed to it
  90. * will automatically be executed (unless the finalizer itself is also a closed subscription).
  91. *
  92. * Closed Subscriptions cannot be added as finalizers to any subscription. Adding a closed
  93. * subscription to a any subscription will result in no operation. (A noop).
  94. *
  95. * Adding a subscription to itself, or adding `null` or `undefined` will not perform any
  96. * operation at all. (A noop).
  97. *
  98. * `Subscription` instances that are added to this instance will automatically remove themselves
  99. * if they are unsubscribed. Functions and {@link Unsubscribable} objects that you wish to remove
  100. * will need to be removed manually with {@link #remove}
  101. *
  102. * @param teardown The finalization logic to add to this subscription.
  103. */
  104. add(teardown: TeardownLogic): void {
  105. // Only add the finalizer if it's not undefined
  106. // and don't add a subscription to itself.
  107. if (teardown && teardown !== this) {
  108. if (this.closed) {
  109. // If this subscription is already closed,
  110. // execute whatever finalizer is handed to it automatically.
  111. execFinalizer(teardown);
  112. } else {
  113. if (teardown instanceof Subscription) {
  114. // We don't add closed subscriptions, and we don't add the same subscription
  115. // twice. Subscription unsubscribe is idempotent.
  116. if (teardown.closed || teardown._hasParent(this)) {
  117. return;
  118. }
  119. teardown._addParent(this);
  120. }
  121. (this._finalizers = this._finalizers ?? []).push(teardown);
  122. }
  123. }
  124. }
  125. /**
  126. * Checks to see if a this subscription already has a particular parent.
  127. * This will signal that this subscription has already been added to the parent in question.
  128. * @param parent the parent to check for
  129. */
  130. private _hasParent(parent: Subscription) {
  131. const { _parentage } = this;
  132. return _parentage === parent || (Array.isArray(_parentage) && _parentage.includes(parent));
  133. }
  134. /**
  135. * Adds a parent to this subscription so it can be removed from the parent if it
  136. * unsubscribes on it's own.
  137. *
  138. * NOTE: THIS ASSUMES THAT {@link _hasParent} HAS ALREADY BEEN CHECKED.
  139. * @param parent The parent subscription to add
  140. */
  141. private _addParent(parent: Subscription) {
  142. const { _parentage } = this;
  143. this._parentage = Array.isArray(_parentage) ? (_parentage.push(parent), _parentage) : _parentage ? [_parentage, parent] : parent;
  144. }
  145. /**
  146. * Called on a child when it is removed via {@link #remove}.
  147. * @param parent The parent to remove
  148. */
  149. private _removeParent(parent: Subscription) {
  150. const { _parentage } = this;
  151. if (_parentage === parent) {
  152. this._parentage = null;
  153. } else if (Array.isArray(_parentage)) {
  154. arrRemove(_parentage, parent);
  155. }
  156. }
  157. /**
  158. * Removes a finalizer from this subscription that was previously added with the {@link #add} method.
  159. *
  160. * Note that `Subscription` instances, when unsubscribed, will automatically remove themselves
  161. * from every other `Subscription` they have been added to. This means that using the `remove` method
  162. * is not a common thing and should be used thoughtfully.
  163. *
  164. * If you add the same finalizer instance of a function or an unsubscribable object to a `Subscription` instance
  165. * more than once, you will need to call `remove` the same number of times to remove all instances.
  166. *
  167. * All finalizer instances are removed to free up memory upon unsubscription.
  168. *
  169. * @param teardown The finalizer to remove from this subscription
  170. */
  171. remove(teardown: Exclude<TeardownLogic, void>): void {
  172. const { _finalizers } = this;
  173. _finalizers && arrRemove(_finalizers, teardown);
  174. if (teardown instanceof Subscription) {
  175. teardown._removeParent(this);
  176. }
  177. }
  178. }
  179. export const EMPTY_SUBSCRIPTION = Subscription.EMPTY;
  180. export function isSubscription(value: any): value is Subscription {
  181. return (
  182. value instanceof Subscription ||
  183. (value && 'closed' in value && isFunction(value.remove) && isFunction(value.add) && isFunction(value.unsubscribe))
  184. );
  185. }
  186. function execFinalizer(finalizer: Unsubscribable | (() => void)) {
  187. if (isFunction(finalizer)) {
  188. finalizer();
  189. } else {
  190. finalizer.unsubscribe();
  191. }
  192. }