"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Subject_1 = require("rxjs/Subject");
const ObjectUnsubscribedError_1 = require("rxjs/util/ObjectUnsubscribedError");
/**
 * MemorizingSubject acts like a BehaviorSubject, except that it is seeded
 * with no value, calls to getValue() will fail before the first `next` call,
 * and it does not push a value to subscribers if `next` has not been called.
 */
class MemorizingSubject extends Subject_1.Subject {
    constructor() {
        super(...arguments);
        this.hasSet = false;
    }
    hasValue() {
        return this.hasSet;
    }
    next(value) {
        this.hasSet = true;
        super.next((this.value = value));
    }
    getValue() {
        if (this.hasError) {
            throw this.thrownError;
        }
        if (this.closed) {
            throw new ObjectUnsubscribedError_1.ObjectUnsubscribedError();
        }
        if (!this.hasSet) {
            throw new Error('Attempted to call `getValue` on a MemorizingSubject' +
                'which did not previous have its value set.');
        }
        return this.value;
    }
    // tslint:disable-next-line
    _subscribe(subscriber) {
        const subscription = super._subscribe(subscriber);
        if (subscription && this.hasSet && !subscription.closed) {
            subscriber.next(this.value);
        }
        return subscription;
    }
}
exports.MemorizingSubject = MemorizingSubject;
