workit icon indicating copy to clipboard operation
workit copied to clipboard

Allow writing typed variables during serialization

Open pvoisin opened this issue 5 years ago • 1 comments

🚀 Feature Proposal

Allow writing typed variables after a task is done.

Motivation

Have better control over what's exchanged with the Camunda engine.

Example

Here's the insertion point I found (quite a hack...). The logic could easily be moved to a more suitable place in the library. I guess that would be around the "success strategy".

import { cloneDeep, wrap } from 'lodash';
import { CamundaMessage, ICamundaService, IMessage, ISuccessStrategy, IVariables } from 'workit-camunda';

export class DefaultSuccessStrategy implements ISuccessStrategy {
  public async handle(message: IMessage, service: ICamundaService): Promise<void> {
    return service.ack(message);
  }
}

const utility = require('workit-camunda/lib/src/utils/utils');
utility.getVariablesWhenChanged = wrap(utility.getVariablesWhenChanged, (getVariablesWhenChanged, message, unwrap) => {
  return getVariablesWhenChanged(message, unwrapVariables);
});

function unwrapVariables(message: IMessage) {
  const taskContext = message.body;
  const types = taskContext['@types'] || {};
  delete taskContext['@types'];

  const variables: IVariables = CamundaMessage.unwrap(cloneDeep(message));
  for (const variableName of Object.keys(types)) {
    if (variableName in taskContext) {
      const variableType = types[variableName];
      if (variableType === 'collection') {
        variables.setTyped(variableName, {
          type: 'object',
          value: JSON.stringify(taskContext[variableName]),
          valueInfo: {
            objectTypeName: 'java.util.Collection',
            serializationDataFormat: 'application/json'
          }
        });
      }
    }
  }

  return variables;
}

Pitch

Power to the developers! ✊

pvoisin avatar Nov 01 '19 14:11 pvoisin

For documentation

In the Camunda Modeler, you can use:

${<variableName>.elements()}

In the Property panel > Multi instance > Collection

But giving the ability to add custom stuff during serialization is a good idea.

We should look here: https://github.com/VilledeMontreal/workit/blob/master/packages/workit-camunda/src/models/camunda/utils.ts#L117-L154

Developer could pass an additional option (a function) to serialize object otherwise it will use utils.serializeVariables<T> by default.

Feel free to make a PR if you have the bandwidth.

OlivierAlbertini avatar Nov 01 '19 14:11 OlivierAlbertini

closed reason: no activity since 2019

OlivierAlbertini avatar Dec 12 '23 20:12 OlivierAlbertini