AxFlowDependencyAnalyzer
Analyzes mapping functions to extract state dependencies.
This class is crucial for the automatic parallelization feature of AxFlow. It determines which fields in the state object a mapping function accesses, which allows the execution planner to understand dependencies between steps and optimize execution by running independent steps in parallel.
The analyzer uses two complementary approaches:
- Static analysis of the function source code
- Dynamic proxy-based tracking as a fallback
This dual approach ensures robust dependency detection even for complex mapping functions that might use destructuring, computed property access, or other advanced JavaScript patterns.
Constructors
Constructor
new AxFlowDependencyAnalyzer(): AxFlowDependencyAnalyzer;
Returns
AxFlowDependencyAnalyzer
Methods
analyzeMappingDependencies()
analyzeMappingDependencies(mapping: (state: any) => any, _nodeName: string): string[];
Analyzes a mapping function to determine which state fields it depends on.
This method is called for every execute step to understand what data the step needs from the current state. This information is used to:
- Build the dependency graph for parallel execution
- Ensure steps execute in the correct order
- Optimize performance by identifying independent operations
The analysis process:
- First tries static analysis by parsing the function source
- Falls back to proxy-based tracking for complex cases
- Returns a list of field names that the mapping function accesses
Example
// For a mapping like: state => ({ query: state.userInput, context: state.previousResult })
// This would return: ['userInput', 'previousResult']
Parameters
Parameter | Type | Description |
---|---|---|
mapping | (state : any ) => any | The mapping function that transforms state to node inputs |
_nodeName | string | The name of the node (currently unused but kept for future use) |
Returns
string
[]
Array of field names that the mapping function depends on
createTrackingProxy()
createTrackingProxy(target: any, accessed: string[]): any;
Creates a tracking proxy for dependency analysis.
This is a public method that creates a proxy to track property access patterns. It’s used for testing and advanced dependency analysis scenarios.
Parameters
Parameter | Type | Description |
---|---|---|
target | any | The target object to wrap with a proxy |
accessed | string [] | Array to collect accessed property names |
Returns
any
Proxy object that tracks property access
parseStaticDependencies()
parseStaticDependencies(functionSource: string): string[];
Parses function source code to extract state dependencies using static analysis.
This method analyzes the source code of a function to find patterns like
state.fieldName
and extracts the field names as dependencies.
Parameters
Parameter | Type | Description |
---|---|---|
functionSource | string | The source code of the function to analyze |
Returns
string
[]
Array of field names found in the source code