"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Primitive_1 = require("../../../../Primitive");
const _ids_1 = require("../../../_ids");
class Catch extends Primitive_1.Primitive {
    constructor(system) {
        super({
            i: ['unit'],
            o: ['err'],
        }, {
            input: {
                unit: {
                    ref: true,
                },
            },
        }, system, _ids_1.ID_CATCH);
        this.addListener('reset', () => {
            this._unlisten = undefined;
            this._done = undefined;
        });
    }
    onRefInputData(name, unit) {
        // if (name === 'unit') {
        const { unlisten, done } = unit.catch((err) => {
            if (err === null) {
                this._forward_all_empty();
            }
            else {
                this._output.err.push(err);
            }
        });
        this._unlisten = unlisten;
        this._done = done;
        const err = unit.caughtErr();
        if (err !== null) {
            this._output.err.push(err);
        }
        // }
    }
    onRefInputDrop(name) {
        // if (name === 'unit') {
        this._unlisten();
        this._unlisten = undefined;
        this._done = undefined;
        this._forward_all_empty();
        // }
    }
    onDataOutputDrop(name) {
        // if (name === 'err') {
        if (!this._backwarding && !this._forwarding_empty) {
            this._done();
        }
        // }
    }
}
exports.default = Catch;
//# sourceMappingURL=index.js.map