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