UNPKG

@asyncapi/react-component

Version:

A React component for AsyncAPI specification.

113 lines 4.47 kB
var PluginManager = (function () { function PluginManager(initialContext) { this.plugins = new Map(); this.slotComponents = new Map(); this.eventListeners = new Map(); this.context = initialContext; } PluginManager.prototype.register = function (plugin) { if (this.plugins.has(plugin.name)) { console.warn("Plugin ".concat(plugin.name, " is already registered")); return; } var api = this.createPluginAPI(plugin); plugin.install(api); this.plugins.set(plugin.name, plugin); }; PluginManager.prototype.unregister = function (pluginName) { var plugin = this.plugins.get(pluginName); if (!plugin) { console.warn("Plugin \"".concat(pluginName, "\" not found")); return; } this.plugins.delete(pluginName); this.slotComponents.forEach(function (components) { var index = components.findIndex(function (c) { return c.pluginName === pluginName; }); if (index > -1) { components.splice(index, 1); } }); }; PluginManager.prototype.createPluginAPI = function (plugin) { var _this = this; return { registerComponent: function (slot, component, options) { var _a; if (options === void 0) { options = {}; } if (!_this.slotComponents.has(slot)) { _this.slotComponents.set(slot, []); } var priority = (_a = options.priority) !== null && _a !== void 0 ? _a : 100; _this.slotComponents.get(slot).push({ component: component, priority: priority, label: options.label, pluginName: plugin === null || plugin === void 0 ? void 0 : plugin.name, }); _this.slotComponents.get(slot).sort(function (a, b) { return b.priority - a.priority; }); }, onSpecLoaded: function (callback) { _this.on('specLoaded', callback); }, getContext: function () { return _this.context; }, on: function (eventName, callback) { _this.on(eventName, callback); }, off: function (eventName, callback) { _this.off(eventName, callback); }, emit: function (eventName, data) { _this.emit(eventName, data); }, }; }; PluginManager.prototype.on = function (eventName, callback) { if (!this.eventListeners.has(eventName)) { this.eventListeners.set(eventName, new Set()); } this.eventListeners.get(eventName).add(callback); }; PluginManager.prototype.off = function (eventName, callback) { var listeners = this.eventListeners.get(eventName); if (listeners) { listeners.delete(callback); if (listeners.size === 0) { this.eventListeners.delete(eventName); } } }; PluginManager.prototype.emit = function (eventName, data) { var eventListeners = this.eventListeners.get(eventName); if (eventListeners) { eventListeners.forEach(function (callback) { return callback(data); }); } }; PluginManager.prototype.listeners = function (eventName) { var listeners = this.eventListeners.get(eventName); return listeners ? Array.from(listeners) : []; }; PluginManager.prototype.eventNames = function () { return Array.from(this.eventListeners.keys()); }; PluginManager.prototype.getComponentsForSlot = function (slot) { var _a; return ((_a = this.slotComponents.get(slot)) !== null && _a !== void 0 ? _a : []).map(function (c) { return c.component; }); }; PluginManager.prototype.updateContext = function (updates) { this.context = updates; }; PluginManager.prototype.getPlugin = function (name) { return this.plugins.get(name); }; PluginManager.prototype.listPlugins = function () { return Array.from(this.plugins.values()).map(function (p) { return ({ name: p.name, version: p.version, }); }); }; return PluginManager; }()); export { PluginManager }; //# sourceMappingURL=pluginManager.js.map