jozz_events 1.0.1
jozz_events: ^1.0.1 copied to clipboard
Domain-first, framework-agnostic event system built for Clean Architecture
๐ฆ jozz_events #
Domain-first, framework-agnostic event system built for Clean Architecture
jozz_events
is a lightweight, strongly-typed event management system designed to enable clean, decoupled communication between features in large-scale applications.
Inspired by domain-driven design and separation of concerns, it enables features to react to domain events without knowing about each other, making it a perfect fit for Clean Architecture, modular design, and scalable systems.
๐ Why jozz_events
? #
โ Clean Architecture Friendly #
- Events are fully decoupled from emitters and listeners.
- Works across layers: UI, Application, Domain, Infrastructure.
- Perfect for feature-based modular projects.
โ Typed, Predictable, and Testable #
- No string-based events or untyped channels.
- Built with Dart's type system using generics and sealed base classes.
โ Framework-Agnostic #
- No dependency on Flutter or any UI toolkit.
- Can be used in Dart CLIs, server apps, or Flutter mobile/web/desktop.
โ Lifecycle Aware #
- Optional lifecycle management for Bloc, Cubit, State, or any component.
- Auto-dispose subscriptions on destruction.
โ Simpler and Safer Than Alternatives #
Package | Clean Arch Friendly | Strongly Typed | Lifecycle Support | Global Singleton | DI-Friendly |
---|---|---|---|---|---|
jozz_events |
โ Yes | โ Yes | โ Yes | โ Opt-in only | โ Yes |
event_bus |
โ No | โ No | โ No | โ Always | โ No |
Bloc-to-Bloc Comm. | โ Tight Coupling | โ Yes | โ Yes | โ | โ Yes |
Signals | โ UI-focused | โ Yes | โ (via hooks) | โ | โ ๏ธ UI-tied |
๐งฑ Use Case Example #
You have two features:
- A
TodoService
that emitsTodoCreatedEvent
- A
NotificationModule
that listens for this event and displays a notification
๐ง These two features must be completely unaware of each other โ and jozz_events
makes that easy.
๐ฆ Installation #
dependencies:
jozz_events: ^latest
Then import it:
import 'package:jozz_events/jozz_events.dart';
๐ ๏ธ Getting Started #
1. Define a Domain Event #
class TodoCreatedEvent extends JozzEvent {
final String todoId;
final String title;
const TodoCreatedEvent({required this.todoId, required this.title});
}
2. Create a Shared Event Bus (or inject it) #
final JozzBus eventBus = JozzBusService();
3. Emit the Event (e.g., inside a service) #
eventBus.emit(TodoCreatedEvent(todoId: '1', title: 'Buy milk'));
Or using fluent API:
eventBus
.emitEvent(TodoCreatedEvent(todoId: '1', title: 'Buy milk'))
.emitEvent(TodoCreatedEvent(todoId: '2', title: 'Call mom'));
4. Listen for the Event (e.g., in a notification module) #
eventBus.on<TodoCreatedEvent>().listen((event) {
print('NOTIFICATION: New todo created: ${event.title}');
});
5. Manage Subscriptions #
final sub = eventBus.listen<TodoCreatedEvent>((event) {
print('Handling todo: ${event.todoId}');
});
// later
sub.cancel();
6. Use Lifecycle Mixins (Optional, Recommended for BLoC/Cubit) #
class MyBloc with JozzLifecycleMixin {
final JozzBus _eventBus;
MyBloc(this._eventBus) {
_eventBus.autoListen<TodoCreatedEvent>(this, _onTodoCreated);
}
void _onTodoCreated(TodoCreatedEvent event) {
// handle logic
}
@override
Future<void> close() {
disposeJozzSubscriptions();
return super.close();
}
}
๐งช Testing #
Mock JozzBus
and verify behavior easily:
final mockBus = MockJozzBus();
when(() => mockBus.on<TodoCreatedEvent>()).thenAnswer((_) => Stream.value(TodoCreatedEvent(...)));
โ Features #
- โ Strongly typed events
- โ No tight coupling between features
- โ Clean architecture ready
- โ Pure Dart โ no Flutter dependency
- โ Lifecycle support for widgets, blocs, cubits, etc.
- โ Fluent event emission
- โ Easy testing & mocking
๐ Clean Architecture Example Structure #
features/
โโโ todo/
โ โโโ domain/events/todo_created_event.dart
โ โโโ application/todo_service.dart
โโโ notifications/
โโโ presentation/notification_listener.dart
Both features are fully decoupled. Communication happens through JozzBus
.
Created by developers who love Clean Architecture and hate spaghetti.
๐ข Coming Soon #
- โ
Singleton opt-in with
JozzBus.instance
- โ Flutter integration subpackage
- ๐งช Built-in testing utilities
- ๐งฉ Middleware or interceptors (event logging)
- ๐ก Namespaced topics or channels
๐ License #
MIT ยฉ Jozz