Browse Source

Cache compiled component field rules instead of recomputing them on every form update

pull/1368/head
github-actions[bot] 3 weeks ago
committed by GitHub
parent
commit
6dacc158e6
No known key found for this signature in database GPG Key ID: B5690EEEBB952194
  1. 70
      frontend/src/app/shared/state/contents.form-rules.spec.ts
  2. 6
      frontend/src/app/shared/state/contents.form-rules.ts

70
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);
});
});

6
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<CompiledRule> = [];
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;

Loading…
Cancel
Save