addTypedListener<T extends P> method

ListenerKiller addTypedListener<T extends P>(
  1. EventListener<T> listener, {
  2. bool useHistory = true,
  3. bool useRuntimeType = false,
  4. List<Type>? excludedTypes,
})

the same as addListener but will call listener only if payload type is T which is a subtype of P

check addListener for more info

set useRuntimeType to true to use runtime type checking, means only listen for T not its parent type P or any subclass of T

use excludedTypes to exclude a List of Type example: if C and D are subclasses for A then addTypedListener<A> while listen for A and C and D but we want to ignore D then use addTypedListener<A>(excludedTypes: [D])

to only listen for a type e.g. C then use addTypedListener<C>(useRuntimeType: true)

Implementation

ListenerKiller addTypedListener<T extends P>(
  EventListener<T> listener, {
  bool useHistory = true,
  bool useRuntimeType = false,
  List<Type>? excludedTypes,
}) {
  return addListener(
    (payload) {
      if (excludedTypes?.contains(payload.runtimeType) ?? false) return;

      if (useRuntimeType) {
        if (payload.runtimeType == T) listener(payload as T);
      } else {
        if (payload is T) listener(payload);
      }
    },
    useHistory: useHistory,
  );
}