UNPKG

@pandorajs/hub

Version:

pandora.js messenge hub

58 lines 1.62 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ConfigManager = void 0; const const_1 = require("../const"); class ConfigManager { constructor() { this.config = {}; this.callbackStore = new Map(); } static async create(providerManager) { const configManager = new ConfigManager(); await providerManager.publish(configManager, { name: const_1.PANDORA_HUB_CONFIG_MANAGER, }); return configManager; } getConfig(topic) { if (topic) { return this.config[topic]; } return this.config; } subscribe(topic, cb) { this.callbackStore.set(topic, cb); } unsubscribe(topic, cb) { this.callbackStore.delete(topic); } async publish(topic, config) { this.config[topic] = config; const cb = this.callbackStore.get(topic); if (cb) { await cb(config); } } getAllTopics(prefix) { const topics = Object.keys(this.config); const ret = []; for (const topic of topics) { if (!prefix || topic.startsWith(prefix)) { ret.push(topic); } } return ret; } getAllSubscribedTopics(prefix) { const topics = this.callbackStore.keys(); const ret = []; for (const topic of topics) { if (!prefix || topic.startsWith(prefix)) { ret.push(topic); } } return ret; } } exports.ConfigManager = ConfigManager; //# sourceMappingURL=ConfigManager.js.map