From 6dacc158e6a4d9e9512e4d06a3736f2088eac232 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 09:29:06 +0100 Subject: [PATCH] Cache compiled component field rules instead of recomputing them on every form update --- .../shared/state/contents.form-rules.spec.ts | 70 +++++++++++++++++++ .../app/shared/state/contents.form-rules.ts | 6 +- 2 files changed, 74 insertions(+), 2 deletions(-) create mode 100644 frontend/src/app/shared/state/contents.form-rules.spec.ts diff --git a/frontend/src/app/shared/state/contents.form-rules.spec.ts b/frontend/src/app/shared/state/contents.form-rules.spec.ts new file mode 100644 index 000000000..c232e2aad --- /dev/null +++ b/frontend/src/app/shared/state/contents.form-rules.spec.ts @@ -0,0 +1,70 @@ +/* + * Squidex Headless CMS + * + * @license + * Copyright (c) Squidex UG (haftungsbeschränkt). All rights reserved. + */ + +import { createProperties, FieldRuleDto, SchemaDto } from '@app/shared/internal'; +import { TestValues } from './_test-helpers'; +import { ComponentRulesProvider, RootRulesProvider, RuleForm } from './contents.form-rules'; + +const { createField, createSchema } = TestValues; + +describe('ComponentRulesProvider', () => { + const field = createField({ id: 1, properties: createProperties('String') }); + + const componentSchema = createSchema({ + id: 2, + fieldRules: [ + new FieldRuleDto({ field: 'field1', action: 'Disable', condition: 'data.field1 > 1' }), + ], + }); + + const rootSchema = createSchema({ id: 1 }); + + it('should return relative rules of the component schema', () => { + const form: RuleForm = { path: 'component.field1', field }; + + const rules = new ComponentRulesProvider('component', new RootRulesProvider(rootSchema), () => componentSchema).getRules(form); + + expect(rules.rules.length).toEqual(1); + expect(rules.rules[0].action).toEqual('Disable'); + }); + + it('should not recompute rules if schema has not changed', () => { + const form: RuleForm = { path: 'component.field1', field }; + + const rootRules = new RootRulesProvider(rootSchema); + const rootGetRules = rootRules.getRules.bind(rootRules); + + let rootCalls = 0; + + rootRules.getRules = (x: RuleForm) => { + rootCalls++; + return rootGetRules(x); + }; + + const rules = new ComponentRulesProvider('component', rootRules, () => componentSchema).getRules(form); + + const rules1 = rules.rules; + const rules2 = rules.rules; + + expect(rules1).toBe(rules2); + expect(rootCalls).toEqual(1); + }); + + it('should recompute rules if schema has changed', () => { + const form: RuleForm = { path: 'component.field1', field }; + + let schema: SchemaDto | undefined = undefined; + + const rules = new ComponentRulesProvider('component', new RootRulesProvider(rootSchema), () => schema).getRules(form); + + expect(rules.rules.length).toEqual(0); + + schema = componentSchema; + + expect(rules.rules.length).toEqual(1); + }); +}); diff --git a/frontend/src/app/shared/state/contents.form-rules.ts b/frontend/src/app/shared/state/contents.form-rules.ts index ca1df3d14..5fb937425 100644 --- a/frontend/src/app/shared/state/contents.form-rules.ts +++ b/frontend/src/app/shared/state/contents.form-rules.ts @@ -81,18 +81,20 @@ export class CompiledRule { } class ComponentRules implements CompiledRules { - private previouSchema?: SchemaDto; + private previousSchema?: SchemaDto; private compiledRules: ReadonlyArray = []; public get rules() { const schema = this.schema(); - if (schema !== this.previouSchema) { + if (schema !== this.previousSchema) { if (schema) { this.compiledRules = fastMerge(this.parentRules.getRules(this.form).rules, this.getRelativeRules(this.form, schema)); } else { this.compiledRules = EMPTY_RULES_ARRAY; } + + this.previousSchema = schema; } return this.compiledRules;