bpmn-elements icon indicating copy to clipboard operation
bpmn-elements copied to clipboard

Async ExpressionHandler

Open Florian79 opened this issue 1 year ago • 3 comments

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

Florian79 avatar Feb 20 '24 16:02 Florian79

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);
      });
    }
  }
}

paed01 avatar Feb 21 '24 05:02 paed01

Thank you Pål - this solution sounds reasonable - i will try it out!

Florian79 avatar Feb 21 '24 08:02 Florian79

Can I close this issue or are you still struggling?

paed01 avatar Mar 26 '24 14:03 paed01