/// Created by Haoyi on 2022/2/23

typedef EventListener<T extends Event> = bool Function(T event);
typedef EventProcessor<T extends Event> = void Function(T event);

class EventObserver<T extends Event> {
  final Type eventType;
  final EventListener<T> listener;

  EventObserver(this.eventType, this.listener);

  void process(T data) {
    listener.call(data);
  }
}

abstract class Event {
  static EventListener listen<T extends Event>(EventProcessor<T> processor) {
    return (event) {
      if (event is T) {
        processor.call(event);
        return true;
      }
      return false;
    };
  }
}
