Skip to content

Commit

Permalink
fix: run task listener cleanup on properties change
Browse files Browse the repository at this point in the history
  • Loading branch information
misiekhardcore committed Nov 15, 2024
1 parent d012a2f commit 14ef3ff
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 11 deletions.
34 changes: 25 additions & 9 deletions lib/camunda-cloud/CleanUpTaskListenersBehavior.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,22 @@ export default class CleanUpTaskListenersBehavior extends CommandInterceptor {
constructor(eventBus, modeling) {
super(eventBus);

// remove task listeners of disallowed type
// remove task listeners of disallowed type on shape replace
this.postExecuted('shape.replace', function(event) {
const element = event.context.newShape;
const taskListenersContainer = getTaskListenersContainer(element);
if (!taskListenersContainer) {
return;
}

const listeners = taskListenersContainer.get('listeners');
const newListeners = withoutDisallowedListeners(element, listeners);
updateListeners(element, modeling);
});

if (newListeners.length !== listeners.length) {
modeling.updateModdleProperties(element, taskListenersContainer, { listeners: newListeners });
// remove task listeners of disallowed type on user task properties update
this.postExecuted('element.updateModdleProperties', function(event) {
const element = event.context.element;

if (!is(element, 'bpmn:UserTask')) {
return;
}

updateListeners(element, modeling);
});

// remove empty task listener container
Expand Down Expand Up @@ -54,6 +56,20 @@ CleanUpTaskListenersBehavior.$inject = [
];

// helpers //////////
function updateListeners(element, modeling) {
const taskListenersContainer = getTaskListenersContainer(element);
if (!taskListenersContainer) {
return;
}

const listeners = taskListenersContainer.get('listeners');
const newListeners = withoutDisallowedListeners(element, listeners);

if (newListeners.length !== listeners.length) {
modeling.updateModdleProperties(element, taskListenersContainer, { listeners: newListeners });
}
}

function withoutDisallowedListeners(element, listeners) {
return listeners.filter(listener => {
if (
Expand Down
22 changes: 20 additions & 2 deletions test/camunda-cloud/CleanUpTaskListenersBehaviorSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {

import { getExtensionElementsList } from 'lib/util/ExtensionElementsUtil';

import { getBusinessObject } from 'bpmn-js/lib/util/ModelUtil';
import { getBusinessObject, is } from 'bpmn-js/lib/util/ModelUtil';

import diagramXML from './task-listeners.bpmn';

Expand Down Expand Up @@ -168,7 +168,7 @@ describe('camunda-cloud/features/modeling - CleanUpTaskListenersBehavior', funct
}));


it('should remove zeebe:TaskListeners', inject(function(elementRegistry, modeling) {
it('should remove zeebe:TaskListeners container when empty', inject(function(elementRegistry, modeling) {

// given
const el = elementRegistry.get('UserTask');
Expand All @@ -182,9 +182,27 @@ describe('camunda-cloud/features/modeling - CleanUpTaskListenersBehavior', funct

expect(extensionElements.get('values')).to.have.lengthOf(1);
}));

it('should remove zeebe:TaskListeners when user task is not zeebe user task', inject(function(elementRegistry, modeling) {

// given
const el = elementRegistry.get('UserTask');

// when
const extensionElements = getBusinessObject(el).get('extensionElements');
modeling.updateModdleProperties(el, extensionElements, { values: removeZeebeUserTask(extensionElements) });

// then
const listenersContainer = getTaskListenersContainer(el);
expect(listenersContainer).not.to.exist;
}));
});
});

function getTaskListenersContainer(element) {
return getExtensionElementsList(getBusinessObject(element), 'zeebe:TaskListeners')[0];
}

function removeZeebeUserTask(extensionElements) {
return extensionElements.get('values').filter(extensionElement => !is(extensionElement, 'zeebe:UserTask'));
}

0 comments on commit 14ef3ff

Please sign in to comment.