Exemplo n.º 1
0
  /**
   * 第一步:通过subscriptionsByEventType得到该事件类型所有订阅者信息队列,根据优先级将当前订阅者信息插入到订阅者队列subscriptionsByEventType中;
   * 第二步:在typesBySubscriber中得到当前订阅者订阅的所有事件队列,将此事件保存到队列typesBySubscriber中,用于后续取消订阅; 第三步:检查这个事件是否是
   * Sticky 事件,如果是则从stickyEvents事件保存队列中取出该事件类型最后一个事件发送给当前订阅者。
   *
   * @param subscriber
   * @param subscriberMethod
   * @param sticky
   * @param priority
   */
  private void subscribe(
      Object subscriber, SubscriberMethod subscriberMethod, boolean sticky, int priority) {
    Class<?> eventType = subscriberMethod.eventType;
    CopyOnWriteArrayList<Subscription> subscriptions =
        subscriptionsByEventType.get(eventType); // 通过subscriptionsByEventType得到该事件类型所有订阅者信息队列
    Subscription newSubscription = new Subscription(subscriber, subscriberMethod, priority);
    // 根据当前的eventType,在subscriptionsByEventType生成对应的key-value(没有则加入,有则跳过次处理)
    if (subscriptions == null) {
      subscriptions = new CopyOnWriteArrayList<Subscription>();
      subscriptionsByEventType.put(eventType, subscriptions);
    } else {
      if (subscriptions.contains(newSubscription)) {
        throw new EventBusException(
            "Subscriber " + subscriber.getClass() + " already registered to event " + eventType);
      }
    }

    // Starting with EventBus 2.2 we enforced methods to be public (might change with annotations
    // again)
    // subscriberMethod.method.setAccessible(true);

    // 根据优先级将当前订阅者信息插入到订阅者队列subscriptionsByEventType中;
    int size = subscriptions.size();
    for (int i = 0; i <= size; i++) {
      if (i == size || newSubscription.priority > subscriptions.get(i).priority) {
        subscriptions.add(i, newSubscription);
        break;
      }
    }

    // 在typesBySubscriber中得到当前订阅者订阅的所有事件队列,将此事件保存到队列typesBySubscriber中,用于后续取消订阅;
    List<Class<?>> subscribedEvents = typesBySubscriber.get(subscriber);
    if (subscribedEvents == null) {
      subscribedEvents = new ArrayList<Class<?>>();
      typesBySubscriber.put(subscriber, subscribedEvents);
    }
    subscribedEvents.add(eventType);

    // 检查这个事件是否是 Sticky 事件,如果是,则从stickyEvents事件保存队列中取出该事件类型最后一个事件发送给当前订阅者。
    if (sticky) {
      if (eventInheritance) {
        // Existing sticky events of all subclasses of eventType have to be considered.
        // Note: Iterating over all events may be inefficient with lots of sticky events,
        // thus data structure should be changed to allow a more efficient lookup
        // (e.g. an additional map storing sub classes of super classes: Class -> List<Class>).
        Set<Map.Entry<Class<?>, Object>> entries = stickyEvents.entrySet();
        for (Map.Entry<Class<?>, Object> entry : entries) {
          Class<?> candidateEventType = entry.getKey();
          if (eventType.isAssignableFrom(
              candidateEventType)) { // 是用来判断一个类Class1和另一个类Class2是否相同或是另一个类的超类或接口
            Object stickyEvent = entry.getValue();
            checkPostStickyEventToSubscription(newSubscription, stickyEvent);
          }
        }
      } else {
        Object stickyEvent = stickyEvents.get(eventType);
        checkPostStickyEventToSubscription(newSubscription, stickyEvent);
      }
    }
  }