Invoke plugin hooks via common function

Use a common function to iterate through and invoke the given hook in
every plugin, and ignore errors thrown by plugins from these hooks.
This commit is contained in:
Hornwitser committed 2020-05-30 14:27:59 +02:00
1 parent 01ea301a8c
commit fc3686b865
4 files changed
+71 -50

No files matched your search

+25
View File
@@ -334,6 +334,30 @@ function attachPluginMessages(link, pluginInfo, plugin) {
}
}
/**
* Invokes the given hook on all plugins
*
* @param {Map<string, Object} plugins -
* Mapping of plugin names to plugins to invoke the hook on.
* @param {string} hook - Name of hook to invoke.
* @param {...*} args - Arguments to pass on to the hook.
* @returns {Array} Non-empty return values from the hooks.
*/
async function invokeHook(plugins, hook, ...args) {
let results = [];
for (let [name, plugin] of plugins) {
try {
let result = await plugin[hook](...args);
if (result !== undefined) {
results.push(result);
}
} catch (err) {
console.error(`Ignoring error from plugin ${name} in ${hook}:`, err);
}
}
return results;
}
module.exports = {
BaseInstancePlugin,
@@ -341,4 +365,5 @@ module.exports = {
loadPluginInfos,
attachPluginMessages,
invokeHook,
};