Skip to content

Commit 35f315f

Browse files
committed
WIP optimization
1 parent 33a167c commit 35f315f

File tree

1 file changed

+31
-3
lines changed

1 file changed

+31
-3
lines changed

lib/TaskExecution.js

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@ export default class TaskExecution extends EventEmitter {
140140
// Collect IDs of direct children of the tested element
141141
const childElementIds = getChildElementIds(this._elementRegistry, element.id);
142142

143+
// Track last emitted items to avoid re-emitting unchanged entries
144+
let lastWaitingItemsKey = null;
145+
let lastChildrenItemsKey = null;
146+
143147
const intervalCallback = async () => {
144148
const getProcessInstanceResult = await this._api.getProcessInstance(processInstanceKey);
145149

@@ -162,7 +166,10 @@ export default class TaskExecution extends EventEmitter {
162166
const waitingItems = buildWaitingItems(jobsResult, userTasksResult, messageSubscriptionsResult, elementInstancesResult);
163167
enrichEventSubProcessChildren(waitingItems, elementInstancesResult, this._elementRegistry);
164168

165-
if (waitingItems.length) {
169+
const waitingItemsKey = getItemsKey(waitingItems);
170+
171+
if (waitingItems.length && waitingItemsKey !== lastWaitingItemsKey) {
172+
lastWaitingItemsKey = waitingItemsKey;
166173

167174
/** @type {import('./types').ExecutionLogEntry} */
168175
const logEntry = {
@@ -171,6 +178,8 @@ export default class TaskExecution extends EventEmitter {
171178
timestamp: Date.now()
172179
};
173180
this.emit('taskExecution.log', logEntry);
181+
} else if (!waitingItems.length) {
182+
lastWaitingItemsKey = null;
174183
}
175184

176185
// Poll for current variables
@@ -189,12 +198,20 @@ export default class TaskExecution extends EventEmitter {
189198

190199
// Emit child elements (sub-process internals) enriched with variables
191200
const childElements = buildChildElements(elementInstancesResult, element.id, childElementIds);
192-
if (childElements.length) {
201+
const enrichedChildren = childElements.length
202+
? enrichChildrenWithVariables(childElements, elementInstancesResult, getVariablesResult)
203+
: [];
204+
const childrenItemsKey = getItemsKey(enrichedChildren);
205+
206+
if (enrichedChildren.length && childrenItemsKey !== lastChildrenItemsKey) {
207+
lastChildrenItemsKey = childrenItemsKey;
193208
this.emit('taskExecution.log', {
194209
type: 'children',
195-
items: enrichChildrenWithVariables(childElements, elementInstancesResult, getVariablesResult),
210+
items: enrichedChildren,
196211
timestamp: Date.now()
197212
});
213+
} else if (!enrichedChildren.length) {
214+
lastChildrenItemsKey = null;
198215
}
199216

200217
if (!getProcessInstanceResult.success) {
@@ -752,4 +769,15 @@ function enrichChildrenWithVariables(children, elementInstancesResult, variables
752769

753770
return { ...child, variables: childVars };
754771
});
772+
}
773+
774+
/**
775+
* Create a stable key from items for change detection.
776+
* Only emits a new log entry when this key differs from the previous one.
777+
*
778+
* @param {Array} items
779+
* @returns {string}
780+
*/
781+
function getItemsKey(items) {
782+
return JSON.stringify(items);
755783
}

0 commit comments

Comments
 (0)