From 6ee5c442b7109de4397deafc2cfbe35dc4a037fb Mon Sep 17 00:00:00 2001 From: Valentin Serra Date: Tue, 16 Apr 2024 19:02:39 +0200 Subject: [PATCH] chore: adjusted expressionLoopPreventer naming and docs Related to #1151 --- .../ExpressionLoopPreventer.js | 19 ++++++++++++++----- .../components/form-fields/ExpressionField.js | 8 ++++++-- .../form-fields/ExpressionField.spec.js | 6 +++--- 3 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/form-js-viewer/src/features/expressionField/ExpressionLoopPreventer.js b/packages/form-js-viewer/src/features/expressionField/ExpressionLoopPreventer.js index f480fd821..af0f00840 100644 --- a/packages/form-js-viewer/src/features/expressionField/ExpressionLoopPreventer.js +++ b/packages/form-js-viewer/src/features/expressionField/ExpressionLoopPreventer.js @@ -2,8 +2,8 @@ export class ExpressionLoopPreventer { constructor(eventBus) { this._computedExpressions = []; - eventBus.on('field.updated', ({ doNotRecompute }) => { - if (doNotRecompute) { + eventBus.on('field.updated', ({ shouldNotRecompute }) => { + if (shouldNotRecompute) { return; } @@ -14,16 +14,25 @@ export class ExpressionLoopPreventer { eventBus.on('reset', this.reset.bind(this)); } - requestOnce(expression) { - if (this._computedExpressions.includes(expression)) { + /** + * Checks if the expression field has already been computed, and registers it if not. + * + * @param {any} expressionField + * @returns {boolean} - whether the expression field has already been computed within the current cycle + */ + registerExpressionExecution(expressionField) { + if (this._computedExpressions.includes(expressionField)) { return false; } - this._computedExpressions.push(expression); + this._computedExpressions.push(expressionField); return true; } + /** + * Resets the list of computed expressions. + */ reset() { this._computedExpressions = []; } diff --git a/packages/form-js-viewer/src/render/components/form-fields/ExpressionField.js b/packages/form-js-viewer/src/render/components/form-fields/ExpressionField.js index cb133df72..5ac067847 100644 --- a/packages/form-js-viewer/src/render/components/form-fields/ExpressionField.js +++ b/packages/form-js-viewer/src/render/components/form-fields/ExpressionField.js @@ -16,11 +16,15 @@ export function ExpressionField(props) { const expressionLoopPreventer = useService('expressionLoopPreventer'); const sendValue = useCallback(() => { - onChange && onChange({ field, value: evaluationMemo, doNotRecompute: true }); + onChange && onChange({ field, value: evaluationMemo, shouldNotRecompute: true }); }, [field, evaluationMemo, onChange]); useEffect(() => { - if (computeOn !== 'change' || isEqual(evaluationMemo, value) || !expressionLoopPreventer.requestOnce(this)) { + if ( + computeOn !== 'change' || + isEqual(evaluationMemo, value) || + !expressionLoopPreventer.registerExpressionExecution(this) + ) { return; } sendValue(); diff --git a/packages/form-js-viewer/test/spec/render/components/form-fields/ExpressionField.spec.js b/packages/form-js-viewer/test/spec/render/components/form-fields/ExpressionField.spec.js index cd669874a..b16399a5a 100644 --- a/packages/form-js-viewer/test/spec/render/components/form-fields/ExpressionField.spec.js +++ b/packages/form-js-viewer/test/spec/render/components/form-fields/ExpressionField.spec.js @@ -43,7 +43,7 @@ describe('ExpressionField', function () { }); // then - expect(onChangeSpy.calledWith({ field, value: 2, doNotRecompute: true })).to.be.true; + expect(onChangeSpy.calledWith({ field, value: 2, shouldNotRecompute: true })).to.be.true; }); it('should re-evaluate when the expression result changes', function () { @@ -88,7 +88,7 @@ describe('ExpressionField', function () { }); // then - expect(onChangeSpy.calledWith({ field, value: 3, doNotRecompute: true })).to.be.true; + expect(onChangeSpy.calledWith({ field, value: 3, shouldNotRecompute: true })).to.be.true; }); it('should not evaluate on intialization if computeOn presubmit', function () { @@ -148,7 +148,7 @@ describe('ExpressionField', function () { }); // then - expect(onChangeSpy.calledWith({ field, value: 2, doNotRecompute: true })).to.be.true; + expect(onChangeSpy.calledWith({ field, value: 2, shouldNotRecompute: true })).to.be.true; }); });