Core Engine Internals
This document covers the "hidden" technical details and internal logic discovered during a deep audit of the @dfbe/core source code.
1. Logic Evaluation Depth Guard
To prevent infinite recursion or performance degradation in extremely complex forms, the ConditionEvaluator implements a strict MAX_DEPTH limit.
- Limit: 5 levels of nested logic groups (
and/or). - Behavior: If the limit is exceeded, the evaluation returns
falseand logs an error to the console.
2. Rule Evaluation & Precedence
Rules are not just a simple list; they follow a specific precedence and default behavior:
- Precedence: If multiple rules match and affect the same property (e.g., two rules trying to set
label), the last matching rule in therulesarray takes precedence. - Smart Visibility: If a field has one or more rules with a
visible: trueeffect, but no rules with avisible: falseeffect, the engine automatically assumes the field should be hidden by default. - Batch Updates: All field effects are calculated in a single pass using
evaluateFieldEffectsbefore the UI re-renders, ensuring no flickering of intermediate states.
3. The Calculation Loop Guard
The FieldRenderer implements a sophisticated protection mechanism for computedValue side-effects.
- The Problem: A field
Ahas a computed value based on fieldB, andBhas a computed value based onA. This creates an infinite loop. - The Solution: A
tickCounterReftracks how many times a field value is updated in a single execution tick. - Limit: The engine checks
tickCounterRef.current > 5. This means if a value is updated 6 or more times in one tick, the engine halts the update for that field and logs an error.
4. Dot-Notation Resolution
The engine uses a recursive resolver for field paths. This allows you to bind components to deep data structures without flattening your state.
- Function:
getNestedValue(obj, path) - Example:
profile.education.university.idwill correctly traverse the RHF state object to find the nested value. - Safety: If any part of the path is
nullorundefined, the resolver returnsundefinedimmediately rather than throwing.
5. Dependency Tracking System
Re-renders are highly optimized via the extractFieldDependencies system. The engine only watches the fields that are actually mentioned in:
- The
visibleIfcondition. - Any
rule.condition. - Any
rule.effectsof typelabel,helperText, orcomputedValuethat use string interpolation (e.g.,label: "Hello {{name}}"). - The
dataSource.dependsOnproperty.
This ensures that typing in a "Phone Number" field doesn't trigger logic recalculations for an unrelated "Address" section.
6. Computed Value Engine
The engine supports more than just string interpolation; it includes a safe Math Evaluator.
- Syntax:
computedValue: "{{price}} * {{quantity}}" - Security: The evaluator uses a strict regex to only allow numbers and basic math operators (
+,-,*,/,.,()). It will NOT evaluate any string containing letters or potentially malicious code. - Fail-safe: If the evaluation fails or the syntax is incomplete (during typing), it returns the raw interpolated string.
7. Dataset Compatibility
While most fields can be defined in the schema, the External Dataset feature is strictly restricted to high-performance selection components:
- Supported Types:
combobox,searchable-select. - Behavior: If a dataset is assigned to a field of any other type, the engine throws a runtime error — it will not silently pass.
- Reasoning: These components are designed to handle the ID → Object transformation and O(1) Map lookups without performance bottlenecks.