on<T> method

Stream<T>? on<T>(
  1. String widgetName
)

Listens for events of Type T and its subtypes.

The method is called like this: myEventBus.on

If the method is called without a type parameter, the Stream contains every event of this EventBus.

The returned Stream is a broadcast stream so multiple subscriptions are allowed.

Each listener is handled independently, and if they pause, only the pausing listener is affected. A paused listener will buffer events internally until unpaused or canceled. So it's usually better to just cancel and later subscribe again (avoids memory leak).

Implementation

Stream<T>? on<T>(String widgetName) {
  if (allowList.contains(_formatToComponentName(widgetName))) {
    Stream<T> steam = _streamController.stream.where((event) => event is T).cast<T>();

    _subscriptions.addAll({
      widgetName: steam,
    });
    if (_cachedEvent.containsKey(widgetName)) {
      fire(_cachedEvent[widgetName] as T, widgetName);
    }
    return steam;
  } else {
    TencentCloudChat.instance.logInstance.console(
      componentName: runtimeType.toString(),
      logs: "You must monitor a value of a certain type, not a dynamic type.",
      logLevel: TencentCloudChatLogLevel.error,
    );
  }
  return null;
}