register method

FBroadcast register(
  1. String key,
  2. ResultCallback receiver, {
  3. Object? context,
  4. Map<String, ResultCallback>? more,
})

注册指定类型的接收者。 如果传入了 context 环境,该接收者将会被注册到环境中。环境可以是任意类型的对象,例如:页面、类.. 接收者通过 value 可以获取到本条消息携带的数据。 当调用 unregister 时,该接收者即会被清除。 key - 消息类型 receiver - 接收者 context - 环境。不为null,receiver 将会被注册到环境中。 more - 方便一次注册多个接收者

Register recipients of the specified type. If the context environment is passed in, the recipient will be registered in the environment. The environment can be any type of object, for example: page, class... The receiver can get the data carried in this message through value. When unregister is called, the receiver will be cleared. key-message type receiver - receiver context - context. Not null, receiver will be registered in the environment. more - Make it easy to register multiple recipients at once

Implementation

FBroadcast register(
  String key,
  ResultCallback receiver, {
  Object? context,
  Map<String, ResultCallback>? more,
}) {
  if (_map == null) return this;
  if (!_textIsEmpty(key)) {
    _get(key).addListener(receiver);
    if (!_getReceivers(context).contains(receiver)) {
      _receiverCache[context]!.add(receiver);
    }
    if (_stickyMap[key] != null) {
      _stickyMap[key]!.forEach((element) {
        _Notifier notifier = element;
//          _stickyMap.remove(key);
        broadcast(key, value: notifier.value, callback: notifier.callback);
      });
      _stickyMap.remove(key);
    }
  }
  if (more?.isNotEmpty ?? false) {
    more?.forEach((key, value) {
      _get(key).addListener(value);
      if (!_getReceivers(context).contains(value)) {
        _receiverCache[context]!.add(value);
      }
      if (_stickyMap[key] != null) {
        _stickyMap[key]?.forEach((element) {
          _Notifier notifier = element;
//          _stickyMap.remove(key);
          broadcast(key, value: notifier.value, callback: notifier.callback);
        });
        _stickyMap.remove(key);
      }
    });
  }
  return this;
}