EventTruck is a lightweight, extensible, and type-safe event bus system for managing event-driven architecture in Dart/Flutter applications. It provides a streamlined API for broadcasting and listening to events, with optional support for event observation, debugging, and logging.
EventTruck is a lightweight, extensible, and type-safe event bus system for managing event-driven architecture in Dart/Flutter applications. It provides a streamlined API for broadcasting and listening to events, with optional support for event observation, debugging, and logging.
Add EventTruck
to your Dart or Flutter project by including the following in your pubspec.yaml
file:
dependencies: event_truck: <latest_version>
Then, run:
flutter pub get
Create an instance of the EventTruck
to handle events:
final eventTruck = EventTruck();
Use the on<T>()
method to subscribe to specific event types:
// Sealed class sealed class Event {} class ClickEvent extends Event { final String buttonId; ClickEvent(this.buttonId); } class TextEvent extends Event { final String text; TextEvent(this.text); } // Listen to all events of type `Event` (base class or subclasses) eventTruck.on<Event>((event) => switch(event) { ClickEvent() => print(event.buttonId), TextEvent() => print(event.text), }); // or eventTruck.on<ClickEvent>((event) => print(event.buttonId)); eventTruck.on<TextEvent>((event) => print(event.text));
Use the add()
method to emit events:
eventTruck.add(ClickEvent('submit_button')); eventTruck.add(TextEvent('Hello World!'));
To monitor all events for logging or debugging purposes, implement the EventTrackObserver
interface:
class DebugObserver implements EventTrackObserver { void onEvent<T>(T event) { print('Observed event: $event'); } } void main() { EventTruck.observer = DebugObserver(); final eventTruck = EventTruck(); eventTruck.add('Tracking this event'); }
Dispose of the EventTruck
instance when it's no longer needed:
eventTruck.dispose();
Subscribe to events of a specific type.
Parameters:
callback
: A function that handles the specific event type T
.
Returns: A stream controller of events filtered by type T
.
Example:
StreamController<ClickEvent> _streamController = eventTruck.on<ClickEvent>((event) { print('Received click event: $event'); }); _streamController.close();
Emit a new event to all listeners.
Parameters:
event
: The event to be added to the event stream.
Example:
eventTruck.add('New message event');
Clean up resources by closing the underlying StreamController
.
Example:
eventTruck.dispose();
An interface to track emitted events.
Handle and log emitted events.
Parameters:
event
: The event emitted through the EventTruck
.
Example:
class MyObserver implements EventTrackObserver { void onEvent<E, T>(EventTruck<E> truck, T event) { print('Logged event: $event'); } }
dispose()
to avoid memory leaks in long-running applications.EventTruck
for different parts of your app to reduce unnecessary event propagation.Hereβs a simple example demonstrating the usage of EventTruck
:
void main() { final eventTruck = EventTruck(); // Listening for string events eventTruck.on<String>((event) { print('String Event: $event'); }); // Emitting different events eventTruck.add('Hello, EventTruck!'); eventTruck.add(123); // Using an observer EventTruck.observer = DebugObserver(); // Clean up resources eventTruck.dispose(); } class DebugObserver implements EventTrackObserver { void onEvent<T>(T event) { print('Debug Observer: $event'); } }