"use strict"; var __extends = (this && this.__extends) || function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; function __() { this.constructor = d; } d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __()); }; var isArray_1 = require('./util/isArray'); var isObject_1 = require('./util/isObject'); var isFunction_1 = require('./util/isFunction'); var tryCatch_1 = require('./util/tryCatch'); var errorObject_1 = require('./util/errorObject'); var Subscription = (function () { function Subscription(_unsubscribe) { this.isUnsubscribed = false; if (_unsubscribe) { this._unsubscribe = _unsubscribe; } } Subscription.prototype.unsubscribe = function () { var hasErrors = false; var errors; if (this.isUnsubscribed) { return; } this.isUnsubscribed = true; var _a = this, _unsubscribe = _a._unsubscribe, _subscriptions = _a._subscriptions; this._subscriptions = null; if (isFunction_1.isFunction(_unsubscribe)) { var trial = tryCatch_1.tryCatch(_unsubscribe).call(this); if (trial === errorObject_1.errorObject) { hasErrors = true; (errors = errors || []).push(errorObject_1.errorObject.e); } } if (isArray_1.isArray(_subscriptions)) { var index = -1; var len = _subscriptions.length; while (++index < len) { var sub = _subscriptions[index]; if (isObject_1.isObject(sub)) { var trial = tryCatch_1.tryCatch(sub.unsubscribe).call(sub); if (trial === errorObject_1.errorObject) { hasErrors = true; errors = errors || []; var err = errorObject_1.errorObject.e; if (err instanceof UnsubscriptionError) { errors = errors.concat(err.errors); } else { errors.push(err); } } } } } if (hasErrors) { throw new UnsubscriptionError(errors); } }; Subscription.prototype.add = function (subscription) { // return early if: // 1. the subscription is null // 2. we're attempting to add our this // 3. we're attempting to add the static `empty` Subscription if (!subscription || (subscription === this) || (subscription === Subscription.EMPTY)) { return; } var sub = subscription; switch (typeof subscription) { case 'function': sub = new Subscription(subscription); case 'object': if (sub.isUnsubscribed || typeof sub.unsubscribe !== 'function') { break; } else if (this.isUnsubscribed) { sub.unsubscribe(); } else { (this._subscriptions || (this._subscriptions = [])).push(sub); } break; default: throw new Error('Unrecognized subscription ' + subscription + ' added to Subscription.'); } }; Subscription.prototype.remove = function (subscription) { // return early if: // 1. the subscription is null // 2. we're attempting to remove ourthis // 3. we're attempting to remove the static `empty` Subscription if (subscription == null || (subscription === this) || (subscription === Subscription.EMPTY)) { return; } var subscriptions = this._subscriptions; if (subscriptions) { var subscriptionIndex = subscriptions.indexOf(subscription); if (subscriptionIndex !== -1) { subscriptions.splice(subscriptionIndex, 1); } } }; Subscription.EMPTY = (function (empty) { empty.isUnsubscribed = true; return empty; }(new Subscription())); return Subscription; }()); exports.Subscription = Subscription; var UnsubscriptionError = (function (_super) { __extends(UnsubscriptionError, _super); function UnsubscriptionError(errors) { _super.call(this, 'unsubscriptoin error(s)'); this.errors = errors; this.name = 'UnsubscriptionError'; } return UnsubscriptionError; }(Error)); exports.UnsubscriptionError = UnsubscriptionError; //# sourceMappingURL=Subscription.js.map