/**
   * Dispatch a message.
   *
   * @threadsafe
   * @remarks: The function may not be running from the UI thread. It is listener's duty to handle
   *     multiple threading issue.
   */
  public static void dispatch(String name, Map message) {

    try {

      Payload payload;

      mutex.acquire();

      if (dispatching) {
        payload = new Payload();
        payload.name = name;
        payload.message = message;
        queue.add(payload);
        mutex.release();
        return;
      }

      dispatching = true;
      //            printMap(message);
      mutex.release();

      emit(name, message); // Emit

      mutex.acquire(); // Process queued message

      while (queue.size() > 0) {
        payload = queue.poll();
        mutex.release();

        emit(payload.name, payload.message);

        mutex.acquire();
      }
      dispatching = false;
      mutex.release();

    } catch (Exception e) {
      Log.e(TAG, "exception", e);
    }
  }