mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Update hooks api to support promise api
This commit is contained in:
@@ -8,7 +8,12 @@ const VALID_HOOKS = [
|
||||
"postDeliver",
|
||||
"onReceive",
|
||||
"postReceive",
|
||||
"onComplete"
|
||||
"onComplete",
|
||||
// Module install hooks
|
||||
"preInstall",
|
||||
"postInstall",
|
||||
"preUninstall",
|
||||
"postUninstall"
|
||||
]
|
||||
|
||||
|
||||
@@ -35,12 +40,12 @@ let labelledHooks = { }
|
||||
* - `postReceive` - passed a `ReceiveEvent` when the message has been given to the node's `input` handler(s)
|
||||
* - `onComplete` - passed a `CompleteEvent` when the node has completed with a message or logged an error
|
||||
*
|
||||
* @mixin @node-red/runtime_hooks
|
||||
* @mixin @node-red/util_hooks
|
||||
*/
|
||||
|
||||
/**
|
||||
* Register a handler to a named hook
|
||||
* @memberof @node-red/runtime_hooks
|
||||
* @memberof @node-red/util_hooks
|
||||
* @param {String} hookId - the name of the hook to attach to
|
||||
* @param {Function} callback - the callback function for the hook
|
||||
*/
|
||||
@@ -68,7 +73,7 @@ function add(hookId, callback) {
|
||||
|
||||
/**
|
||||
* Remove a handled from a named hook
|
||||
* @memberof @node-red/runtime_hooks
|
||||
* @memberof @node-red/util_hooks
|
||||
* @param {String} hookId - the name of the hook event to remove - must be `name.label`
|
||||
*/
|
||||
function remove(hookId) {
|
||||
@@ -110,11 +115,33 @@ function removeHook(id,callback) {
|
||||
function trigger(hookId, payload, done) {
|
||||
const hookStack = hooks[hookId];
|
||||
if (!hookStack || hookStack.length === 0) {
|
||||
done();
|
||||
return;
|
||||
if (done) {
|
||||
done();
|
||||
return;
|
||||
} else {
|
||||
return Promise.resolve();
|
||||
}
|
||||
}
|
||||
if (!done) {
|
||||
return new Promise((resolve,reject) => {
|
||||
invokeStack(hookStack,payload,function(err) {
|
||||
if (err !== undefined && err !== false) {
|
||||
if (!(err instanceof Error)) {
|
||||
err = new Error(err);
|
||||
}
|
||||
err.hook = hookId
|
||||
reject(err);
|
||||
} else {
|
||||
resolve(err);
|
||||
}
|
||||
})
|
||||
});
|
||||
} else {
|
||||
invokeStack(hookStack,payload,done)
|
||||
}
|
||||
}
|
||||
function invokeStack(hookStack,payload,done) {
|
||||
let i = 0;
|
||||
|
||||
function callNextHook(err) {
|
||||
if (i === hookStack.length || err) {
|
||||
done(err);
|
||||
@@ -148,8 +175,6 @@ function trigger(hookId, payload, done) {
|
||||
}
|
||||
}
|
||||
}
|
||||
callNextHook();
|
||||
|
||||
function handleResolve(result) {
|
||||
if (result === undefined) {
|
||||
callNextHook();
|
||||
@@ -157,6 +182,7 @@ function trigger(hookId, payload, done) {
|
||||
done(result);
|
||||
}
|
||||
}
|
||||
callNextHook();
|
||||
}
|
||||
|
||||
function clear() {
|
||||
|
Reference in New Issue
Block a user