import 'dart:async';

import 'package:event_bus/event_bus.dart';
import 'package:get/get.dart';

import '../app/modules/splittunneling/selectapp/controllers/splittunneling_selectapp_controller.dart';

final EventBus eventBus = EventBus();

mixin EventBusMixin on GetxController {
  final List<StreamSubscription> _subscriptions = [];

  /// 监听某种事件类型
  void listenEvent<T>(void Function(T event) handler) {
    final sub = eventBus.on<T>().listen(handler);
    _subscriptions.add(sub);
  }

  @override
  void onClose() {
    // 页面销毁时自动反注册
    for (final sub in _subscriptions) {
      sub.cancel();
    }
    _subscriptions.clear();
    super.onClose();
  }
}

class SplitTunnelingPageEvent {
  final SplitTunnelingMode mode;
  SplitTunnelingPageEvent({required this.mode});
}
