Skip to content

Commit

Permalink
chore: adjusted expressionLoopPreventer naming and docs
Browse files Browse the repository at this point in the history
Related to #1151
  • Loading branch information
Skaiir committed Apr 16, 2024
1 parent 4bebb52 commit 6ee5c44
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand All @@ -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 = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 () {
Expand Down Expand Up @@ -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 () {
Expand Down Expand Up @@ -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;
});
});

Expand Down

0 comments on commit 6ee5c44

Please sign in to comment.