levit_dart_core 0.0.5
levit_dart_core: ^0.0.5 copied to clipboard
The core framework for pure Dart applications. Aggregates dependency injection and reactivity.
levit_dart_core #
The foundational composition layer for the Levit ecosystem. Explicit. Reactive. Deterministic.
levit_dart_core orchestrates the relationship between [levit_reactive] primitives and [levit_scope] dependency management into a structured framework for building pure Dart business logic.
Purpose & Scope #
levit_dart_core provides a predictable environment for managing application logic without UI dependencies. It is responsible for:
- Lifecycle Orchestration: Defining how components are initialized, attached to scopes, and disposed.
- Automated Resource Management: Ensuring all streams, timers, and reactive objects are cleaned up deterministically.
- Composition Conventions: Providing both class-based ([LevitController]) and functional ([LevitState]) abstractions for logic.
It deliberately avoids UI or platform-specific logic, making it suitable for CLI tools, servers, and background services. For Flutter integration, use levit_flutter_core.
Conceptual Overview #
Application Elements #
- [LevitController]: A managed component that encapsulates state and behavior. It participates in an explicit lifecycle and uses
autoDisposefor resource management. - [LevitState]: A functional provider that acts as a factory for state instances, offering a modern alternative to class-based controllers.
- [Levit]: The central registry and entry point for managing scopes and resolving dependencies.
Key Mechanisms #
- Ambient Scoping: Uses Zone-based propagation via
levit_scopeto resolve dependencies implicitly without manual passing. - Auto-Linking: Automatically tracks reactive state created within a controller or state builder to ensure zero-leak disposal.
Getting Started #
1. Define a Controller #
class CounterController extends LevitController {
// Logic is automatically registered for cleanup
late final count = autoDispose(0.lx);
void increment() => count.value++;
}
2. Register and Resolve #
void main() {
// Register the controller in the current scope
Levit.put(() => CounterController());
// Resolve and use
final controller = Levit.find<CounterController>();
controller.increment();
}
Design Principles #
Explicit over Implicit #
While [Levit] provides ambient scoping for ergonomics, every state transition and dependency link is trackable and deterministic.
Managed Lifecycle #
Resources are never left to the garbage collector alone. The onClose hook and autoDispose mechanism ensure that all side effects are terminated as soon as a component is removed.
Functional Composition #
The framework encourages composing logic via [LevitState] to reduce boilerplate and improve encapsulation, while providing [LevitController] for more complex or hierarchical logic.