Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Async ExpressionHandler #39

Closed
Florian79 opened this issue Feb 20, 2024 · 3 comments
Closed

Async ExpressionHandler #39

Florian79 opened this issue Feb 20, 2024 · 3 comments

Comments

@Florian79
Copy link

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

@paed01
Copy link
Owner

paed01 commented Feb 21, 2024

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 added a commit that referenced this issue Feb 21, 2024
@Florian79
Copy link
Author

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

@paed01
Copy link
Owner

paed01 commented Mar 26, 2024

Can I close this issue or are you still struggling?

@paed01 paed01 closed this as completed May 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants