sendAndAwaitCrossEvent method

Future<NUIMiniProgramMessage?> sendAndAwaitCrossEvent(
  1. NUIMiniProgramMessage message, {
  2. Duration timeoutDuration = const Duration(seconds: 60),
})

Send an event message and wait for the reply (two-ways)

Implementation

Future<NUIMiniProgramMessage?> sendAndAwaitCrossEvent(NUIMiniProgramMessage message, {Duration timeoutDuration = const Duration(seconds: 60)}) async{
  final sent = await _sendCrossEvent(message);
  if(sent) {
    final queue = _getResponseMessageQueue(message.id);
    if (queue == null) return null;

    final competer = Completer<NUIMiniProgramMessage>();
    NUIAsync.delayForResult(timeoutDuration, () async => true).then((value) {
      //If time out duration reached and completer is still not completed, complete with null to end the conversation
      if(!competer.isCompleted){
        competer.complete(null);
      }
    });
    queue.future.then((value){
      //If a value is returned before the timeout, complete with the value (Message)
      if(!competer.isCompleted){
        competer.complete(value);
      }
    });

    return competer.future;
  }
  return null;
}