dispatchMessage method

Future<MessageDispatchResult> dispatchMessage(
  1. Message message, {
  2. required MessageDispatchCallback onDispatch,
})

Sends message through onDispatch and tracks its state in the list.

The message is shown with a loading indicator right away. Then:

  • If onDispatch returns a message, it replaces the local one.
  • If it returns null, the message is marked as failed and can be retried with retryMessage.
  • If it throws MessageBlockedException, the message is removed.

The result is returned and also emitted on dispatchResultStream.

await controller.dispatchMessage(
  SenderMessage(name: 'Me', timestamp: DateTime.now().toIso8601String(), elements: [TextElement(text)]),
  onDispatch: (message) => api.send(message),
);

Implementation

Future<MessageDispatchResult> dispatchMessage(
  Message message, {
  required MessageDispatchCallback onDispatch,
}) async {
  final messageGroup = addMessage(
    message.copyWith(isLoading: true, isFailed: false),
  );
  // Show the sent bubble right away, not only after onDispatch completes (a server round trip).
  if (message.isSender) {
    _scrollToBottomForNewMessage();
  }
  late final MessageDispatchResult dispatchResult;
  late final Message? result;

  try {
    result = await onDispatch(message);
  } on MessageBlockedException catch (e) {
    // A block is not a failure: remove the bubble and do not register a retry entry.
    final blockedIndex = messageGroup.messages.indexWhere(
      (element) => element.id == message.id,
    );
    if (blockedIndex >= 0) {
      removeMessageGroupAt(messageGroup, blockedIndex);
    }
    final blockedResult = MessageDispatchResult(
      message: message,
      isSuccess: false,
      exception: e,
    );
    _dispatchResultController.add(blockedResult);
    _notifyMessagesChanged();
    return blockedResult;
  }
  final index = messageGroup.messages.indexWhere(
    (element) => element.id == message.id,
  );
  if (result != null) {
    messageGroup.messages[index] = result;
    messageGroup.sortMessages();
    dispatchResult = MessageDispatchResult(
      message: message,
      isSuccess: true,
      exception: null,
    );
    if (!result.isSender) {
      _newReceiveMessageController.add(result);
    }
    if (result.isRead == true) {
      _unreadMessageIds.remove(result.id);
    } else if (result.isRead == false) {
      _unreadMessageIds.add(result.id);
    }
    _notifyMessagesChanged();
  } else {
    dispatchResult = MessageDispatchResult(
      message: message,
      isSuccess: false,
      exception: Exception("Failed to dispatch message"),
    );
    removeMessageGroupAt(messageGroup, index);

    _failedMessageEntries[message.id] = FailedMessageEntry(
      messageId: message.id,
      messageGroup: messageGroup,
      onDispatch: onDispatch,
    );

    // The bubble already appeared; re-adding it as failed is not a new message.
    _suppressAppear = true;
    try {
      addMessage(message.copyWith(isLoading: false, isFailed: true));
    } finally {
      _suppressAppear = false;
    }
  }
  if (message.isSender) {
    // The bubble changed from the loading indicator to its content (or to failed).
    _scrollToBottomForNewMessage();
  }
  if (_dispatchResultController.isClosed) return dispatchResult;
  _dispatchResultController.add(dispatchResult);

  return dispatchResult;
}