Overview
Custom models allow you to extend Stremio Core with your own stateful components. By implementing theModel trait, you can integrate seamlessly with the runtime system and benefit from automatic effect handling, state propagation, and UI updates.
The Model Trait
TheModel trait is the foundation for all stateful components:
Manual Implementation
Step 1: Define Your Model
Step 2: Define Field Enum
TheField enum represents all mutable fields in your model:
Step 3: Implement the Model Trait
Using the Model Derive Macro
For complex models with multiple fields, use the#[derive(Model)] macro:
Step 1: Enable the Derive Feature
Step 2: Structure Your Model
The model must have actx field:
Generated Code
The derive macro generates:- Field enum with variants for each field
- Model implementation that coordinates updates across fields
- Automatic effect composition combining effects from all fields
Context-Aware Models
Many models need access to user context. Use theUpdateWithCtx trait:
Complete Example: Custom Favorites Model
Here’s a complete example of a custom favorites model:Integrating with Runtime
Once your model is defined, integrate it with the runtime:Testing Custom Models
Test your models with the test environment:Best Practices
Keep Models Simple
Keep Models Simple
Each model should have a clear, single responsibility. Compose complex models from simpler ones.
Use Type-Safe Actions
Use Type-Safe Actions
Define custom action enums for your model instead of using strings or generic types.
Handle All Message Types
Handle All Message Types
Always include a catch-all pattern that returns empty effects and fields for unhandled messages.
Minimize Clones
Minimize Clones
Use
Arc<T> for large data structures that are frequently cloned across messages.Separate Concerns
Separate Concerns
Keep business logic in helper functions. The
update method should focus on state transitions.Common Patterns
Resource Loading Pattern
Pagination Pattern
Next Steps
State Management
Deep dive into state management patterns
Environment Trait
Learn about the Env trait for side effects
