invoke method

Future<Object?> invoke(
  1. String methodName, {
  2. List<Object>? args,
})

Invokes a hub method on the server using the specified name and arguments.

The Future returned by this method resolves when the server indicates it has finished invoking the method. When the Future resolves, the server has finished invoking the method. If the server method returns a result, it is produced as the result of resolving the Promise.

methodName: The name of the server method to invoke. args: The arguments used to invoke the server method. Returns a Future that resolves with the result of the server method (if any), or rejects with an error.

Implementation

Future<Object?> invoke(String methodName, {List<Object>? args}) {
  args = args ?? [];
  final t = _replaceStreamingParams(args);
  final invocationDescriptor =
      _createInvocation(methodName, args, false, t.item2);

  final completer = Completer<Object?>();

  _callbacks[invocationDescriptor.invocationId] =
      (HubMessageBase? invocationEvent, Exception? error) {
    if (error != null) {
      if (!completer.isCompleted) completer.completeError(error);
      return;
    } else if (invocationEvent != null) {
      if (invocationEvent is CompletionMessage) {
        if (invocationEvent.error != null) {
          if (!completer.isCompleted) {
            completer.completeError(new GeneralError(invocationEvent.error));
          }
        } else {
          if (!completer.isCompleted) {
            completer.complete(invocationEvent.result);
          }
        }
      } else {
        if (!completer.isCompleted) {
          completer.completeError(new GeneralError(
              "Unexpected message type: ${invocationEvent.type}"));
        }
      }
    }
  };

  final promiseQueue =
      _sendWithProtocol(invocationDescriptor).catchError((e) {
    if (!completer.isCompleted) completer.completeError(e);
    // invocationId will always have a value for a non-blocking invocation
    _callbacks.remove(invocationDescriptor.invocationId);
  });

  _launchStreams(t.item1, promiseQueue);

  return completer.future;
}