@@ -29,14 +29,30 @@ class JavaOptions {
2929
3030void _writeHostApi (Indent indent, Api api) {
3131 assert (api.location == ApiLocation .host);
32+
33+ if (api.methods.any ((Method it) => it.isAsynchronous)) {
34+ indent.write ('public interface Result<T> ' );
35+ indent.scoped ('{' , '}' , () {
36+ indent.writeln ('void success(T result);' );
37+ });
38+ indent.addln ('' );
39+ }
40+
3241 indent.writeln (
3342 '/** Generated interface from Pigeon that represents a handler of messages from Flutter.*/' );
3443 indent.write ('public interface ${api .name } ' );
3544 indent.scoped ('{' , '}' , () {
3645 for (Method method in api.methods) {
37- final String argSignature =
38- method.argType == 'void' ? '' : '${method .argType } arg' ;
39- indent.writeln ('${method .returnType } ${method .name }($argSignature );' );
46+ final String returnType =
47+ method.isAsynchronous ? 'void' : method.returnType;
48+ final List <String > argSignature = < String > [];
49+ if (method.argType != 'void' ) {
50+ argSignature.add ('${method .argType } arg' );
51+ }
52+ if (method.isAsynchronous) {
53+ argSignature.add ('Result<${method .returnType }> result' );
54+ }
55+ indent.writeln ('$returnType ${method .name }(${argSignature .join (', ' )});' );
4056 }
4157 indent.addln ('' );
4258 indent.writeln (
@@ -65,17 +81,26 @@ void _writeHostApi(Indent indent, Api api) {
6581 'HashMap<String, HashMap> wrapped = new HashMap<>();' );
6682 indent.write ('try ' );
6783 indent.scoped ('{' , '}' , () {
68- String methodArgument;
69- if (argType == 'void' ) {
70- methodArgument = '' ;
71- } else {
84+ final List <String > methodArgument = < String > [];
85+ if (argType != 'void' ) {
7286 indent.writeln ('@SuppressWarnings("ConstantConditions")' );
7387 indent.writeln (
7488 '$argType input = $argType .fromMap((HashMap)message);' );
75- methodArgument = 'input' ;
89+ methodArgument. add ( 'input' ) ;
7690 }
77- final String call = 'api.${method .name }($methodArgument )' ;
78- if (method.returnType == 'void' ) {
91+ if (method.isAsynchronous) {
92+ methodArgument.add (
93+ 'result -> { '
94+ 'wrapped.put("${Keys .result }", result.toMap()); '
95+ 'reply.reply(wrapped); '
96+ '}' ,
97+ );
98+ }
99+ final String call =
100+ 'api.${method .name }(${methodArgument .join (', ' )})' ;
101+ if (method.isAsynchronous) {
102+ indent.writeln ('$call ;' );
103+ } else if (method.returnType == 'void' ) {
79104 indent.writeln ('$call ;' );
80105 indent.writeln ('wrapped.put("${Keys .result }", null);' );
81106 } else {
@@ -88,8 +113,13 @@ void _writeHostApi(Indent indent, Api api) {
88113 indent.scoped ('{' , '}' , () {
89114 indent.writeln (
90115 'wrapped.put("${Keys .error }", wrapError(exception));' );
116+ if (method.isAsynchronous) {
117+ indent.writeln ('reply.reply(wrapped);' );
118+ }
91119 });
92- indent.writeln ('reply.reply(wrapped);' );
120+ if (! method.isAsynchronous) {
121+ indent.writeln ('reply.reply(wrapped);' );
122+ }
93123 });
94124 });
95125 indent.scoped (null , '}' , () {
0 commit comments