"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const cli_core_1 = require("@bluebase/cli-core");
const logger = cli_core_1.Utils.logger;
class ListenerManager {
    constructor(listener, name) {
        this.name = name || 'listener';
        this.lastConnectionKey = 0;
        this.connectionMap = {};
        this.listener = listener;
        // Track all connections to our server so that we can close them when needed.
        this.listener.on('connection', (connection) => {
            // Increment the connection key.
            this.lastConnectionKey += 1;
            // Generate a new key to represent the connection
            const connectionKey = this.lastConnectionKey;
            // Add the connection to our map.
            this.connectionMap[connectionKey] = connection;
            // Remove the connection from our map when it closes.
            connection.on('close', () => {
                delete this.connectionMap[connectionKey];
            });
        });
    }
    killAllConnections() {
        Object.keys(this.connectionMap).forEach((connectionKey) => {
            this.connectionMap[connectionKey].destroy();
        });
    }
    dispose() {
        return new Promise((resolve) => {
            if (this.listener) {
                this.killAllConnections();
                logger.log({
                    label: `BlueBase CLI: Engine Web`,
                    level: 'info',
                    message: 'Destroyed all existing connections.',
                });
                this.listener.close(() => {
                    logger.log({
                        label: `BlueBase CLI: Engine Web`,
                        level: 'info',
                        message: 'Closed listener.',
                    });
                    resolve();
                });
            }
            else {
                resolve();
            }
        });
    }
}
exports.default = ListenerManager;
//# sourceMappingURL=ListenerManager.js.map