register method

Future<Registered> register(
  1. String procedure, {
  2. RegisterOptions? options,
})

This registers a procedure with the given options that may be called by other sessions.

Implementation

Future<Registered> register(String procedure,
    {RegisterOptions? options}) async {
  var register = Register(nextRegisterId++, procedure, options: options);
  _transport.send(register);
  AbstractMessage registered = await _openSessionStreamController.stream
      .where((message) =>
          (message is Registered &&
              message.registerRequestId == register.requestId) ||
          (message is Error &&
              message.requestTypeId == MessageTypes.codeRegister &&
              message.requestId == register.requestId))
      .first;
  if (registered is Registered) {
    registrations[registered.registrationId] = registered;
    registered.procedure = procedure;
    registered.invocationStream =
        _openSessionStreamController.stream.where((message) {
      if (message is Invocation &&
          message.registrationId == registered.registrationId) {
        // Check if there is a registration that has not been unregistered yet
        if (registrations[registered.registrationId] != null) {
          message.onResponse((message) => _transport.send(message));
          return true;
        } else {
          _transport.send(Error(MessageTypes.codeInvocation,
              message.requestId, {}, Error.noSuchRegistration));
          return false;
        }
      }
      return false;
    }).cast();
    return registered;
  } else {
    throw (registered as Error?)!;
  }
}