"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.Completable = void 0;
class Completable {
    startTime = Date.now();
    _completeInfo;
    _onCompleteFns = [];
    constructor(control) {
        if (!control) {
            return;
        }
        control.complete = this.complete.bind(this);
        control.error = this.error.bind(this);
    }
    get isComplete() {
        return this._completeInfo !== undefined;
    }
    onComplete(cb) {
        if (this._completeInfo) {
            cb(this._completeInfo);
            return;
        }
        this._onCompleteFns.push(cb);
    }
    _complete(result, err) {
        const endTime = Date.now();
        this._completeInfo = {
            startTime: this.startTime,
            endTime,
            duration: endTime - this.startTime,
            error: err,
            success: err === undefined,
            result: result
        };
        this._onCompleteFns.forEach(cb => cb(this._completeInfo));
    }
    complete(res) {
        this._complete(res);
    }
    error(err) {
        this._complete(undefined, err);
    }
}
exports.Completable = Completable;
