handleResponse method

void handleResponse(
  1. JavaScriptMessage message
)

Implementation

void handleResponse(JavaScriptMessage message) {
  try {
    var json = message.decode();

    // parse JSON into General RelayerResponse to fetch id first, result will handled in the function interface
    RelayerResponse relayerResponse =
        RelayerResponse<dynamic>.fromJson(json, (result) => result);
    MagicRPCResponse rpcResponse = relayerResponse.response;

    var result = rpcResponse.result;
    var id = rpcResponse.id;

    // get callbacks in the handlers map
    var completer = _messageHandlers[id];

    // Surface the Raw JavaScriptMessage back to the function call so it can converted back to Result type
    // Only decode when result is not null, so the result is not null
    if (result != null) {
      completer!.complete(message);
    }

    if (rpcResponse.error != null) {
      completer!.completeError(rpcResponse.error!.toJson());
    }
  } catch (err) {
    //Todo Add internal error collector
    debugPrint("parse Error ${err.toString()}");
  }
}