Allow a host message handler to return a null result (#4812)

Host apps will return null if there is no matching handler for a message name
This commit is contained in:
Jason Simmons
2016-07-01 15:50:29 -07:00
committed by GitHub
parent 5ed8f1a1fd
commit cbf7d98874

View File

@@ -57,11 +57,15 @@ class HostMessages {
return completer.future;
}
static dynamic _decode(String message) {
return message != null ? JSON.decode(message) : null;
}
/// Sends a JSON-encoded message to the host application and JSON-decodes the response.
static Future<dynamic> sendJSON(String messageName, [dynamic json]) async {
Completer<dynamic> completer = new Completer<dynamic>();
_hostAppMessagesProxy.sendString(messageName, JSON.encode(json), (String reply) {
completer.complete(JSON.decode(reply));
completer.complete(_decode(reply));
});
return completer.future;
}
@@ -78,7 +82,7 @@ class HostMessages {
/// before being returned to the host application.
static void addJSONMessageHandler(String messageName, Future<dynamic> callback(dynamic json)) {
_appMessages.handlers[messageName] = (String message) async {
return JSON.encode(await callback(JSON.decode(message)));
return JSON.encode(await callback(_decode(message)));
};
}
}