2020-04-18 11:48:21 +00:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
|
|
import 'package:event_bus/event_bus.dart';
|
|
|
|
|
|
|
|
/// Events helper
|
|
|
|
///
|
|
|
|
/// @author Pierre Hubert
|
|
|
|
|
|
|
|
/// Main WebSocket closed
|
|
|
|
class WSClosedEvent {}
|
|
|
|
|
2020-04-18 12:14:54 +00:00
|
|
|
/// New number of notifications
|
|
|
|
class NewNumberNotifsEvent {
|
|
|
|
final int newNum;
|
|
|
|
|
|
|
|
NewNumberNotifsEvent(this.newNum);
|
|
|
|
}
|
|
|
|
|
2020-04-18 11:48:21 +00:00
|
|
|
class EventsHelper {
|
|
|
|
static EventBus _mgr = EventBus();
|
|
|
|
|
|
|
|
/// Listen to event
|
|
|
|
///
|
|
|
|
/// Do not use this method directly. You should instead prefer to use
|
|
|
|
/// [SafeState.listen] to handle safely widgets lifecycle...
|
|
|
|
///
|
|
|
|
/// You can not register to global events
|
|
|
|
static StreamSubscription<T> on<T>(void onData(T event)) {
|
|
|
|
if (T == dynamic) throw Exception("Do not register to all events!");
|
|
|
|
|
|
|
|
final stream = _mgr.on<T>();
|
|
|
|
return stream.listen(onData);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Propagate an event
|
|
|
|
static void emit<T>(T event) {
|
|
|
|
_mgr.fire(event);
|
|
|
|
}
|
|
|
|
}
|