philiprehberger_event_bus 0.1.0 copy "philiprehberger_event_bus: ^0.1.0" to clipboard
philiprehberger_event_bus: ^0.1.0 copied to clipboard

Typed event bus with Stream subscriptions, sticky events, and scoped lifecycle

philiprehberger_event_bus #

Tests pub package Last updated

Typed event bus with Stream subscriptions, sticky events, and scoped lifecycle

Requirements #

  • Dart >= 3.6

Installation #

Add to your pubspec.yaml:

dependencies:
  philiprehberger_event_bus: ^0.1.0

Then run:

dart pub get

Usage #

import 'package:philiprehberger_event_bus/event_bus.dart';

final bus = EventBus();

Fire and Listen #

bus.on<String>().listen((event) {
  print('Received: $event');
});

bus.fire('hello');

Typed Events #

class UserLoggedIn {
  final String userId;
  UserLoggedIn(this.userId);
}

bus.on<UserLoggedIn>().listen((event) {
  print('User logged in: ${event.userId}');
});

bus.fire(UserLoggedIn('user-123'));

Sticky Events #

Sticky events are stored and replayed to new subscribers:

bus.fireSticky(UserLoggedIn('user-456'));

// New listener immediately receives the last sticky event
bus.on<UserLoggedIn>().listen((event) {
  print('Got sticky: ${event.userId}');
});

// Retrieve the last sticky event directly
final last = bus.lastSticky<UserLoggedIn>();

// Clear when no longer needed
bus.clearSticky<UserLoggedIn>();

Dispose #

bus.dispose();

After calling dispose(), no more events can be fired or received.

API #

Method Description
fire<T>(T event) Dispatch an event to all listeners of type T
on<T>() Subscribe to events of type T, returns Stream<T>
fireSticky<T>(T event) Dispatch a sticky event that replays to new subscribers
lastSticky<T>() Get the last sticky event of type T, or null
clearSticky<T>() Remove the stored sticky event of type T
dispose() Close the bus and release all resources

Development #

dart pub get
dart analyze --fatal-infos
dart test

Support #

If you find this project useful:

License #

MIT

1
likes
160
points
0
downloads

Documentation

API reference

Publisher

verified publisherphiliprehberger.com

Weekly Downloads

Typed event bus with Stream subscriptions, sticky events, and scoped lifecycle

Homepage
Repository (GitHub)
View/report issues

License

MIT (license)

More

Packages that depend on philiprehberger_event_bus