Commit cbf7d988 authored by Jason Simmons's avatar Jason Simmons Committed by GitHub

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
parent 5ed8f1a1
...@@ -57,11 +57,15 @@ class HostMessages { ...@@ -57,11 +57,15 @@ class HostMessages {
return completer.future; 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. /// Sends a JSON-encoded message to the host application and JSON-decodes the response.
static Future<dynamic> sendJSON(String messageName, [dynamic json]) async { static Future<dynamic> sendJSON(String messageName, [dynamic json]) async {
Completer<dynamic> completer = new Completer<dynamic>(); Completer<dynamic> completer = new Completer<dynamic>();
_hostAppMessagesProxy.sendString(messageName, JSON.encode(json), (String reply) { _hostAppMessagesProxy.sendString(messageName, JSON.encode(json), (String reply) {
completer.complete(JSON.decode(reply)); completer.complete(_decode(reply));
}); });
return completer.future; return completer.future;
} }
...@@ -78,7 +82,7 @@ class HostMessages { ...@@ -78,7 +82,7 @@ class HostMessages {
/// before being returned to the host application. /// before being returned to the host application.
static void addJSONMessageHandler(String messageName, Future<dynamic> callback(dynamic json)) { static void addJSONMessageHandler(String messageName, Future<dynamic> callback(dynamic json)) {
_appMessages.handlers[messageName] = (String message) async { _appMessages.handlers[messageName] = (String message) async {
return JSON.encode(await callback(JSON.decode(message))); return JSON.encode(await callback(_decode(message)));
}; };
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment