"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const tslib_1 = require("tslib");
const routing_controllers_1 = require("@gem-mine/routing-controllers");
const HEARTBEAT = 5000;
const clients = {};
let clientId = 0;
function createEventStream(heartbeat) {
    function everyClient(fn) {
        Object.keys(clients).forEach(function (id) {
            fn(clients[id]);
        });
    }
    const interval = setInterval(function heartbeatTick() {
        everyClient(function (client) {
            client.write('data: \uD83D\uDC93\n\n');
        });
    }, heartbeat);
    return {
        handler: function (req, res) {
            const PassThrough = require('stream').PassThrough;
            const stream = new PassThrough();
            res.body = stream;
            res.write = stream.write.bind(stream);
            res.write('\n');
            var isHttp1 = !(parseInt(req.httpVersion) >= 2);
            if (isHttp1) {
                req.socket.setKeepAlive(true);
                res.set('Connection', 'keep-alive');
            }
            res.set('X-Accel-Buffering', 'no');
            res.set('Access-Control-Allow-Origin', '*');
            res.set('Cache-Control', 'no-cache, no-transform');
            res.set('Content-Type', 'text/event-stream;charset=utf-8');
            res.status = 200;
            stream.on('error', () => { });
            let id = clientId++;
            clients[id] = res;
            stream.on('close', function () {
                clearInterval(interval);
                delete clients[id];
            });
        }
    };
}
let default_1 = class default_1 {
    get(request, response) {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            const eventStream = createEventStream(HEARTBEAT);
            eventStream.handler(request, response);
            return response;
        });
    }
    count() {
        return tslib_1.__awaiter(this, void 0, void 0, function* () {
            return {
                data: {
                    client: Object.keys(clients).length
                }
            };
        });
    }
};
tslib_1.__decorate([
    routing_controllers_1.Get('/'),
    tslib_1.__param(0, routing_controllers_1.Req()), tslib_1.__param(1, routing_controllers_1.Res()),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", [Object, Object]),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "get", null);
tslib_1.__decorate([
    routing_controllers_1.Get('/count'),
    tslib_1.__metadata("design:type", Function),
    tslib_1.__metadata("design:paramtypes", []),
    tslib_1.__metadata("design:returntype", Promise)
], default_1.prototype, "count", null);
default_1 = tslib_1.__decorate([
    routing_controllers_1.Controller('/heartbeat')
], default_1);
exports.default = default_1;
//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaGVhcnRiZWF0LmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiLi4vLi4vc3JjL2NvbnRyb2xsZXIvaGVhcnRiZWF0LnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7OztBQUNBLHVFQUF3RTtBQUV4RSxNQUFNLFNBQVMsR0FBRyxJQUFJLENBQUE7QUFDdEIsTUFBTSxPQUFPLEdBQUcsRUFBRSxDQUFBO0FBQ2xCLElBQUksUUFBUSxHQUFHLENBQUMsQ0FBQTtBQUVoQixTQUFTLGlCQUFpQixDQUFDLFNBQVM7SUFDbEMsU0FBUyxXQUFXLENBQUMsRUFBRTtRQUNyQixNQUFNLENBQUMsSUFBSSxDQUFDLE9BQU8sQ0FBQyxDQUFDLE9BQU8sQ0FBQyxVQUFTLEVBQUU7WUFDdEMsRUFBRSxDQUFDLE9BQU8sQ0FBQyxFQUFFLENBQUMsQ0FBQyxDQUFBO1FBQ2pCLENBQUMsQ0FBQyxDQUFBO0lBQ0osQ0FBQztJQUNELE1BQU0sUUFBUSxHQUFHLFdBQVcsQ0FBQyxTQUFTLGFBQWE7UUFDakQsV0FBVyxDQUFDLFVBQVMsTUFBTTtZQUN6QixNQUFNLENBQUMsS0FBSyxDQUFDLHdCQUF3QixDQUFDLENBQUE7UUFDeEMsQ0FBQyxDQUFDLENBQUE7SUFDSixDQUFDLEVBQUUsU0FBUyxDQUFDLENBQUE7SUFDYixPQUFPO1FBQ0wsT0FBTyxFQUFFLFVBQVMsR0FBRyxFQUFFLEdBQUc7WUFDeEIsTUFBTSxXQUFXLEdBQUcsT0FBTyxDQUFDLFFBQVEsQ0FBQyxDQUFDLFdBQVcsQ0FBQTtZQUNqRCxNQUFNLE1BQU0sR0FBRyxJQUFJLFdBQVcsRUF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