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

import 'package:event_bus/event_bus.dart';
import 'package:marking_app/utils/event_bus_utils.dart';

mixin EventBusMixin<T> {
  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() {
    _subscription?.cancel();
  }
}

mixin EventBusMixinSub<T> {
  StreamSubscription? _subscriptionSub;
  static final EventBusUtils _exampleUtilSub = EventBusUtils();
  static final EventBus _eBusSub = _exampleUtilSub.getEventBus();

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

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

  // 关闭监听总线事件
  void eventCancelSub() {
    _subscriptionSub?.cancel();
  }
}

mixin EventBusMixinSubLast<T> {
  StreamSubscription? _subscriptionSubLast;
  static final EventBusUtils _exampleUtilSubLast = EventBusUtils();
  static final EventBus _eBusSubLast = _exampleUtilSubLast.getEventBus();

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

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

  // 关闭监听总线事件
  void eventCancelSubLast() {
    _subscriptionSubLast?.cancel();
  }
}
