"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const Holder_1 = require("../../../../../Class/Holder");
const Waiter_1 = require("../../../../../Waiter");
const _ids_1 = require("../../../../_ids");
class Server extends Holder_1.Holder {
    constructor(system) {
        super({
            fi: ['port'],
            fo: [],
            i: ['res'],
            o: ['req'],
        }, {}, system, _ids_1.ID_SERVER);
        this._waiter = new Waiter_1.Waiter();
    }
    async f({ port }, done) {
        const { api: { http: { listen }, }, cache: { servers }, } = this.__system;
        try {
            this._unlisten = listen(port, async (req) => {
                this._output.req.push(req);
                const res = await this._waiter.once();
                this._waiter.clear();
                return res;
            }, servers);
        }
        catch (err) {
            done(undefined, err.message);
            return;
        }
    }
    d() {
        if (this._unlisten) {
            this._unlisten();
            this._unlisten = undefined;
        }
    }
    onIterDataInputData(name, data) {
        super.onIterDataInputData(name, data);
        if (name === 'res') {
            this._waiter.set(data);
            this._input.res.pull();
        }
    }
}
exports.default = Server;
//# sourceMappingURL=index.js.map