| 1 |
import { isFunction } from './util/isFunction'; |
| 2 |
import { UnsubscriptionError } from './util/UnsubscriptionError'; |
| 3 |
import { arrRemove } from './util/arrRemove'; |
| 4 |
export class Subscription { |
| 5 |
constructor(initialTeardown) { |
| 6 |
this.initialTeardown = initialTeardown; |
| 7 |
this.closed = false; |
| 8 |
this._parentage = null; |
| 9 |
this._finalizers = null; |
| 10 |
} |
| 11 |
unsubscribe() { |
| 12 |
let errors; |
| 13 |
if (!this.closed) { |
| 14 |
this.closed = true; |
| 15 |
const { _parentage } = this; |
| 16 |
if (_parentage) { |
| 17 |
this._parentage = null; |
| 18 |
if (Array.isArray(_parentage)) { |
| 19 |
for (const parent of _parentage) { |
| 20 |
parent.remove(this); |
| 21 |
} |
| 22 |
} |
| 23 |
else { |
| 24 |
_parentage.remove(this); |
| 25 |
} |
| 26 |
} |
| 27 |
const { initialTeardown: initialFinalizer } = this; |
| 28 |
if (isFunction(initialFinalizer)) { |
| 29 |
try { |
| 30 |
initialFinalizer(); |
| 31 |
} |
| 32 |
catch (e) { |
| 33 |
errors = e instanceof UnsubscriptionError ? e.errors : [e]; |
| 34 |
} |
| 35 |
} |
| 36 |
const { _finalizers } = this; |
| 37 |
if (_finalizers) { |
| 38 |
this._finalizers = null; |
| 39 |
for (const finalizer of _finalizers) { |
| 40 |
try { |
| 41 |
execFinalizer(finalizer); |
| 42 |
} |
| 43 |
catch (err) { |
| 44 |
errors = errors !== null && errors !== void 0 ? errors : []; |
| 45 |
if (err instanceof UnsubscriptionError) { |
| 46 |
errors = [...errors, ...err.errors]; |
| 47 |
} |
| 48 |
else { |
| 49 |
errors.push(err); |
| 50 |
} |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
if (errors) { |
| 55 |
throw new UnsubscriptionError(errors); |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
add(teardown) { |
| 60 |
var _a; |
| 61 |
if (teardown && teardown !== this) { |
| 62 |
if (this.closed) { |
| 63 |
execFinalizer(teardown); |
| 64 |
} |
| 65 |
else { |
| 66 |
if (teardown instanceof Subscription) { |
| 67 |
if (teardown.closed || teardown._hasParent(this)) { |
| 68 |
return; |
| 69 |
} |
| 70 |
teardown._addParent(this); |
| 71 |
} |
| 72 |
(this._finalizers = (_a = this._finalizers) !== null && _a !== void 0 ? _a : []).push(teardown); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
_hasParent(parent) { |
| 77 |
const { _parentage } = this; |
| 78 |
return _parentage === parent || (Array.isArray(_parentage) && _parentage.includes(parent)); |
| 79 |
} |
| 80 |
_addParent(parent) { |
| 81 |
const { _parentage } = this; |
| 82 |
this._parentage = Array.isArray(_parentage) ? (_parentage.push(parent), _parentage) : _parentage ? [_parentage, parent] : parent; |
| 83 |
} |
| 84 |
_removeParent(parent) { |
| 85 |
const { _parentage } = this; |
| 86 |
if (_parentage === parent) { |
| 87 |
this._parentage = null; |
| 88 |
} |
| 89 |
else if (Array.isArray(_parentage)) { |
| 90 |
arrRemove(_parentage, parent); |
| 91 |
} |
| 92 |
} |
| 93 |
remove(teardown) { |
| 94 |
const { _finalizers } = this; |
| 95 |
_finalizers && arrRemove(_finalizers, teardown); |
| 96 |
if (teardown instanceof Subscription) { |
| 97 |
teardown._removeParent(this); |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
Subscription.EMPTY = (() => { |
| 102 |
const empty = new Subscription(); |
| 103 |
empty.closed = true; |
| 104 |
return empty; |
| 105 |
})(); |
| 106 |
export const EMPTY_SUBSCRIPTION = Subscription.EMPTY; |
| 107 |
export function isSubscription(value) { |
| 108 |
return (value instanceof Subscription || |
| 109 |
(value && 'closed' in value && isFunction(value.remove) && isFunction(value.add) && isFunction(value.unsubscribe))); |
| 110 |
} |
| 111 |
function execFinalizer(finalizer) { |
| 112 |
if (isFunction(finalizer)) { |
| 113 |
finalizer(); |
| 114 |
} |
| 115 |
else { |
| 116 |
finalizer.unsubscribe(); |
| 117 |
} |
| 118 |
} |
| 119 |
//# sourceMappingURL=Subscription.js.map |