mirror of
https://gitlab.com/wgp/dougal/software.git
synced 2025-12-06 08:17:09 +00:00
The events listener now uses a proper self-consuming queue and the event handlers have been rewritten accordingly. The way this works is that running init() on the handlers library instantiates the handlers and returns two higher-order functions, prepare() and despatch(). A call to the latter of these is appended to the queue with each new incoming event. The handlers have access to a context object (ctx) which may be used to persist data between calls and/or exchange data between handlers. This is used notably to give the handlers access to project configurations, which are themselves refreshed by a project configuration change handler (DetectProjectConfigurationChange).
45 lines
1.0 KiB
JavaScript
45 lines
1.0 KiB
JavaScript
const { ALERT, ERROR, WARNING, NOTICE, INFO, DEBUG } = require('DOUGAL_ROOT/debug')(__filename);
|
|
|
|
const Handlers = [
|
|
require('./detect-project-configuration-change'),
|
|
require('./detect-soleol'),
|
|
require('./detect-soft-start'),
|
|
require('./report-line-change-time'),
|
|
require('./detect-fdsp')
|
|
];
|
|
|
|
function init (ctx) {
|
|
|
|
const instances = Handlers.map(Handler => new Handler(ctx));
|
|
|
|
function prepare (data, ctx) {
|
|
const promises = [];
|
|
for (let instance of instances) {
|
|
const promise = new Promise(async (resolve, reject) => {
|
|
try {
|
|
DEBUG("Run", instance.author);
|
|
const result = await instance.run(data, ctx);
|
|
DEBUG("%s result: %O", instance.author, result);
|
|
resolve(result);
|
|
} catch (err) {
|
|
ERROR("%s error:\n%O", instance.author, err);
|
|
reject(err);
|
|
}
|
|
});
|
|
promises.push(promise);
|
|
}
|
|
return promises;
|
|
}
|
|
|
|
function despatch (data, ctx) {
|
|
return Promise.allSettled(prepare(data, ctx));
|
|
}
|
|
|
|
return { instances, prepare, despatch };
|
|
}
|
|
|
|
module.exports = {
|
|
Handlers,
|
|
init
|
|
};
|