// event_bus 混入类
import 'dart:async';

import 'package:event_bus/event_bus.dart';
import 'package:making_school_asignment_app/common/utils/event_bus_utils.dart';

mixin EventBusMixin<T> {
  late final StreamSubscription? _subscription;
  static final EventBusUtils _exampleUtil = EventBusUtils();
  static final EventBus _eBus = _exampleUtil.getEventBus();

  /*
   * 发起事件总线监听
   * @param {Function} callback 回调函数
  */
  /// 发起事件总线监听 @param {Function} callback 回调函数
  StreamSubscription eventOn<T>({required void Function(T) callback}) {
    _subscription = _eBus.on<T>().listen(callback);
    return _subscription!;
  }

  // 触发事件总线
  void eventFire({required T model}) {
    _exampleUtil.toFire<T>(model);
  }

  // 关闭监听总线事件
  void eventCancel() {
    try{
      _subscription?.cancel();
    }catch(_){}
  }
}
