Basic Custom Component
At its core, a custom component in SmartGraph is a class that inherits fromReactiveComponent
. Here’s a basic template:
process
, which defines how your component handles input data.
State Management in Custom Components
You can add state to your custom components using the built-in state management methods:Error Handling
Implement robust error handling in your custom components:Asynchronous Components
For components that need to perform asynchronous operations:Components with External Dependencies
If your component relies on external libraries or services:Branching Components
Create components that can direct flow based on input:Components with Internal Buffers
For components that need to accumulate data:Components with Initialization and Cleanup
For components that need setup and teardown operations:Composable Components
Create higher-order components by composing existing ones:Best Practices for Custom Components
- Single Responsibility: Each component should have a single, well-defined purpose.
- Input Validation: Validate input data at the beginning of the
process
method. - Error Handling: Use try-except blocks to handle potential errors gracefully.
- State Management: Use the built-in state management methods for maintaining component state.
- Asynchronous Design: Utilize
async/await
for any I/O-bound operations. - Logging: Implement logging to aid in debugging and monitoring.
- Type Hinting: Use type hints to improve code readability and catch potential type-related errors.
- Documentation: Provide clear docstrings explaining the component’s purpose and usage.