一个listener定义

src/module/test-flow/backend/src/config/static/flowDef/listener/set00_simple.spec.js

const require3 = require('require3');
const assert = require3('assert');

module.exports = class Listener {
  constructor(context) {
    this.context = context;
  }

  async onFlowStart(options) {
    console.log('onFlowStart: ', options.startEventId);
    // flowVars
    const xyz = this.context.vars.get('x.y.z');
    assert.equal(xyz, undefined);
    this.context.vars.set('x.y.z', 'flow');
  }

  async onFlowEnd(options) {
    console.log('onFlowEnd: ', options && options.flowRemark);
    // flowVars
    const xyz = this.context.vars.get('x.y.z');
    assert.equal(xyz, 'flow');
  }

  async onNodeEnter(contextNode) {
    console.log('onNodeEnter: ', contextNode._nodeDef.id);
    // nodeVars
    const xyz = contextNode.vars.get('x.y.z');
    assert.equal(xyz, undefined);
    contextNode.vars.set('x.y.z', contextNode._nodeDef.id);
  }

  async onNodeBegin(contextNode) {
    console.log('onNodeBegin: ', contextNode._nodeDef.id);
  }

  async onNodeDoing(contextNode) {
    console.log('onNodeDoing: ', contextNode._nodeDef.id);
  }

  async onNodeEnd(contextNode) {
    console.log('onNodeEnd: ', contextNode._nodeDef.id);
  }

  async onNodeLeave(contextNode) {
    console.log('onNodeLeave: ', contextNode._nodeDef.id);
    // nodeVars
    const xyz = contextNode.vars.get('x.y.z');
    assert.equal(xyz, contextNode._nodeDef.id);
  }

  async onEdgeEnter(contextEdge, contextNode) {
    console.log('onEdgeEnter: ', contextEdge._edgeDef.id, ' from node: ', contextNode._nodeDef.id);
  }

  async onEdgeTake(contextEdge, contextNode) {
    console.log('onEdgeTake: ', contextEdge._edgeDef.id, ' from node: ', contextNode._nodeDef.id);
  }

  async onEdgeLeave(contextEdge, contextNode) {
    console.log('onEdgeLeave: ', contextEdge._edgeDef.id, ' from node: ', contextNode._nodeDef.id);
  }

  getNodeDefOptions(contextNode /* { options }*/) {
    console.log('getNodeDefOptions: ', contextNode._nodeDef.id);
  }

};

1. 环境对象

  • this.context:流程实例环境对象
  • contextNode:活动节点环境对象
  • contextEdge:转移线环境对象

2. 流程实例事件

  • onFlowStart:流程实例启动事件
async onFlowStart(options)
  • onFlowEnd:流程实例结束事件
async onFlowEnd(options)

3. 活动节点事件

  • onNodeEnter:活动节点进入事件
async onNodeEnter(contextNode)
  • onNodeBegin:活动节点开始事件
async onNodeBegin(contextNode)
  • onNodeDoing:活动节点执行事件
async onNodeDoing(contextNode)
  • onNodeEnd:活动节点结束事件
async onNodeEnd(contextNode)
  • onNodeLeave:活动节点离开事件
async onNodeLeave(contextNode)

4. 转移线事件

  • onEdgeEnter:转移线进入事件
async onEdgeEnter(contextEdge, contextNode)
  • onEdgeEnter:转移线进入事件

如果转移线设置了条件表达式,那么当onEdgeEnter事件发生时,会自动执行条件表达式。如果结果为true,则会继续触发onEdgeTake事件;反之,则中止该转移线的执行

async onEdgeEnter(contextEdge, contextNode)
  • onEdgeTake:转移线有效事件
async onEdgeTake(contextEdge, contextNode)
  • onEdgeLeave:转移线离开事件
async onEdgeLeave(contextEdge, contextNode)

5. 工具方法

  • getNodeDefOptions

通过重载此方法,可以提供动态修改之后的活动节点的配置参数

getNodeDefOptions(contextNode, { options })