"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
const types_1 = require("@deepstream/types");
const events_1 = require("events");
/**
 * This class provides a generic mechanism that allows to maintain
 * a distributed state amongst the nodes of a cluster.
 */
class SingleStateRegistry extends types_1.DeepstreamPlugin {
    constructor() {
        super(...arguments);
        this.description = 'Single State Registry';
        this.data = new Map();
        this.emitter = new events_1.EventEmitter();
    }
    /**
    * Checks if a given entry exists within the registry
    */
    has(name) {
        return this.data.has(name);
    }
    onAdd(callback) {
        this.emitter.on('add', callback);
    }
    onRemove(callback) {
        this.emitter.on('remove', callback);
    }
    /**
    * Add a name/entry to the registry. If the entry doesn't exist yet,
    * this will notify the other nodes within the cluster
    */
    add(name) {
        const current = this.data.get(name);
        if (!current) {
            this.data.set(name, 1);
            this.emitter.emit('add', name);
        }
        else {
            this.data.set(name, current + 1);
        }
    }
    /**
    * Removes a name/entry from the registry. If the entry doesn't exist,
    * this will exit silently
    */
    remove(name) {
        const current = this.data.get(name) - 1;
        if (current === 0) {
            this.data.delete(name);
            this.emitter.emit('remove', name);
        }
        else {
            this.data.set(name, current);
        }
    }
    /**
    * Returns all currently registered entries
    */
    getAll() {
        return [...this.data.keys()];
    }
    /**
     * Returns all the servers that hold a given state
     */
    getAllServers(subscriptionName) {
        return [];
    }
    /**
     * Removes all entries for a given serverName. This is intended to be called
     * whenever a node leaves the cluster
     */
    removeAll(serverName) {
    }
}
exports.SingleStateRegistry = SingleStateRegistry;
//# sourceMappingURL=single-state-registry.js.map