import { __extends, __values } from "tslib"; import { Observable } from './Observable'; import { Subscription, EMPTY_SUBSCRIPTION } from './Subscription'; import { ObjectUnsubscribedError } from './util/ObjectUnsubscribedError'; import { arrRemove } from './util/arrRemove'; import { errorContext } from './util/errorContext'; var Subject = (function (_super) { __extends(Subject, _super); function Subject() { var _this = _super.call(this) || this; _this.closed = false; _this.observers = []; _this.isStopped = false; _this.hasError = false; _this.thrownError = null; return _this; } Subject.prototype.lift = function (operator) { var subject = new AnonymousSubject(this, this); subject.operator = operator; return subject; }; Subject.prototype._throwIfClosed = function () { if (this.closed) { throw new ObjectUnsubscribedError(); } }; Subject.prototype.next = function (value) { var _this = this; errorContext(function () { var e_1, _a; _this._throwIfClosed(); if (!_this.isStopped) { var copy = _this.observers.slice(); try { for (var copy_1 = __values(copy), copy_1_1 = copy_1.next(); !copy_1_1.done; copy_1_1 = copy_1.next()) { var observer = copy_1_1.value; observer.next(value); } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (copy_1_1 && !copy_1_1.done && (_a = copy_1.return)) _a.call(copy_1); } finally { if (e_1) throw e_1.error; } } } }); }; Subject.prototype.error = function (err) { var _this = this; errorContext(function () { _this._throwIfClosed(); if (!_this.isStopped) { _this.hasError = _this.isStopped = true; _this.thrownError = err; var observers = _this.observers; while (observers.length) { observers.shift().error(err); } } }); }; Subject.prototype.complete = function () { var _this = this; errorContext(function () { _this._throwIfClosed(); if (!_this.isStopped) { _this.isStopped = true; var observers = _this.observers; while (observers.length) { observers.shift().complete(); } } }); }; Subject.prototype.unsubscribe = function () { this.isStopped = this.closed = true; this.observers = null; }; Object.defineProperty(Subject.prototype, "observed", { get: function () { var _a; return ((_a = this.observers) === null || _a === void 0 ? void 0 : _a.length) > 0; }, enumerable: false, configurable: true }); Subject.prototype._trySubscribe = function (subscriber) { this._throwIfClosed(); return _super.prototype._trySubscribe.call(this, subscriber); }; Subject.prototype._subscribe = function (subscriber) { this._throwIfClosed(); this._checkFinalizedStatuses(subscriber); return this._innerSubscribe(subscriber); }; Subject.prototype._innerSubscribe = function (subscriber) { var _a = this, hasError = _a.hasError, isStopped = _a.isStopped, observers = _a.observers; return hasError || isStopped ? EMPTY_SUBSCRIPTION : (observers.push(subscriber), new Subscription(function () { return arrRemove(observers, subscriber); })); }; Subject.prototype._checkFinalizedStatuses = function (subscriber) { var _a = this, hasError = _a.hasError, thrownError = _a.thrownError, isStopped = _a.isStopped; if (hasError) { subscriber.error(thrownError); } else if (isStopped) { subscriber.complete(); } }; Subject.prototype.asObservable = function () { var observable = new Observable(); observable.source = this; return observable; }; Subject.create = function (destination, source) { return new AnonymousSubject(destination, source); }; return Subject; }(Observable)); export { Subject }; var AnonymousSubject = (function (_super) { __extends(AnonymousSubject, _super); function AnonymousSubject(destination, source) { var _this = _super.call(this) || this; _this.destination = destination; _this.source = source; return _this; } AnonymousSubject.prototype.next = function (value) { var _a, _b; (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.next) === null || _b === void 0 ? void 0 : _b.call(_a, value); }; AnonymousSubject.prototype.error = function (err) { var _a, _b; (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.error) === null || _b === void 0 ? void 0 : _b.call(_a, err); }; AnonymousSubject.prototype.complete = function () { var _a, _b; (_b = (_a = this.destination) === null || _a === void 0 ? void 0 : _a.complete) === null || _b === void 0 ? void 0 : _b.call(_a); }; AnonymousSubject.prototype._subscribe = function (subscriber) { var _a, _b; return (_b = (_a = this.source) === null || _a === void 0 ? void 0 : _a.subscribe(subscriber)) !== null && _b !== void 0 ? _b : EMPTY_SUBSCRIPTION; }; return AnonymousSubject; }(Subject)); export { AnonymousSubject }; //# sourceMappingURL=Subject.js.map