Async ExpressionHandler
Hello! apparently I can extend the behaviour of an activity - but how to extend the behaviour of a SequenceFlow?
I try to use an ExpressionHandler with an asynchronous resolveExpression function, which is not allowed in the interface but could be solved easily by overwriting the ExpressionCondition.prototype.execute method as follows:
ExpressionCondition.prototype.execute = function execute(message, callback) {
const owner = this._owner;
try {
const result = owner.environment.resolveExpression(this.expression, owner.createMessage(message));
return Promise.resolve(result).then(r =>
callback ? callback(null, r) : result).catch(err =>
callback ? callback(err) : err)
//if (callback) return callback(null, result);
//return result;
} catch (err) {
if (callback) return callback(err);
throw err;
}
};
or is there any other mechanism to deal with an async ExpressionHandler?
Thank you! Florian
Since Expression- and ScriptCondition are not exposed in the api (at the moment), I would extend the SequenceFlow.prototype.getCondition function or make your own extended SequenceFlow.
import * as elements from 'bpmn-elements';
class FlorianSequenceFlow extends elements.SequenceFlow {
getCondition() {
const condition = super.getCondition();
if (condition?.type !== 'expression') return condition;
const execute = condition.execute;
condition.execute = asyncExecute.bind(condition);
return condition;
function asyncExecute(message, callback) {
execute.call(condition, message, (executeErr, result) => {
if (executeErr) return callback(executeErr);
return Promise.resolve(result).then(r =>
callback ? callback(null, r) : result).catch(err =>
callback ? callback(err) : err);
});
}
}
}
Thank you Pål - this solution sounds reasonable - i will try it out!
Can I close this issue or are you still struggling?