34 lines
700 B
Dart
34 lines
700 B
Dart
import 'dart:async';
|
||
|
||
/// 全局事件总线
|
||
class EventBus {
|
||
EventBus._();
|
||
|
||
static final _instance = EventBus._();
|
||
|
||
factory EventBus() => _instance;
|
||
|
||
|
||
// StreamController,广播模式可以让多个地方监听
|
||
final StreamController<GlobalEvent> _controller = StreamController.broadcast();
|
||
|
||
/// 发送事件
|
||
void fire(GlobalEvent event) {
|
||
_controller.add(event);
|
||
}
|
||
|
||
/// 监听事件
|
||
Stream<GlobalEvent> get stream => _controller.stream;
|
||
|
||
/// 关闭流(一般应用生命周期结束时调用)
|
||
void dispose() {
|
||
_controller.close();
|
||
}
|
||
}
|
||
|
||
/// 事件类型枚举
|
||
enum GlobalEvent {
|
||
unauthorized, // 401 需要重新登录
|
||
// 以后可以扩展其他事件
|
||
}
|