nyEvent<T> function

dynamic nyEvent<T>({
  1. Map? params,
  2. Map<Type, NyEvent> events = const {},
})

Event helper

Implementation

nyEvent<T>({
  Map? params,
  Map<Type, NyEvent> events = const {},
}) async {
  assert(T.toString() != 'dynamic',
      'You must provide an Event type for this method.\nE.g. event<LoginEvent>({"User": "#1 User"});');

  Map<Type, NyEvent> appEvents = events;

  if (events.isEmpty) {
    appEvents = Backpack.instance.read('nylo').getEvents();
  }
  assert(appEvents.containsKey(T),
      'Your config/events.dart is missing this class ${T.toString()}');

  NyEvent nyEvent = appEvents[T]!;
  Map<dynamic, NyListener> listeners = nyEvent.listeners;

  if (listeners.isEmpty) {
    return;
  }
  for (NyListener listener in listeners.values.toList()) {
    listener.setEvent(nyEvent);
    dynamic result = await listener.handle(params);
    if (result != null && result == false) {
      break;
    }
  }
}