5050import java .util .Locale ;
5151import java .util .Set ;
5252import java .util .concurrent .CompletableFuture ;
53- import java .util .concurrent .ExecutionException ;
5453import java .util .function .Function ;
5554import java .util .stream .Collectors ;
5655import java .util .stream .IntStream ;
@@ -81,7 +80,7 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
8180 Function <JsonValue , Object > mapping = getResultTypeMapper (method );
8281
8382 String name = method .getName ();
84- CompletableFuture <JsonValue > result = invoke (name , args );
83+ CompletableFuture <JsonValue > result = invoke (mapping != null , name , args );
8584
8685 if (mapping != null ) {
8786 return result .thenApply (json -> (json instanceof JsonNull ) ? null : mapping .apply (json ));
@@ -113,7 +112,7 @@ private Function<JsonValue, Object> getResultTypeMapper(Method method) {
113112 }
114113 }
115114
116- private CompletableFuture <JsonValue > invoke (String name , Object [] args ) {
115+ private CompletableFuture <JsonValue > invoke (boolean hasResult , String name , Object [] args ) {
117116 if (name .startsWith ("set" ) && args .length == 1 ) {
118117 name = name .substring ("set" .length ());
119118 name = name .substring (0 , 1 ).toLowerCase (Locale .ENGLISH ) + name .substring (1 );
@@ -125,19 +124,19 @@ private CompletableFuture<JsonValue> invoke(String name, Object[] args) {
125124 } else {
126125 arg = (Serializable ) args [0 ];
127126 }
128- return executeJs ("this.terminal.setOption($0,$1)" , name , arg );
127+ return executeJs (false , "this.terminal.setOption($0,$1)" , name , arg );
129128 } else if (args == null || args .length == 0 ) {
130- return executeJs (" this.terminal[$0]()" , name );
129+ return executeJs (hasResult , "return this.terminal[$0]()" , name );
131130 } else if (args .length == 1 ) {
132- return executeJs (" this.terminal[$0]($1)" , name , (Serializable ) args [0 ]);
131+ return executeJs (hasResult , "return this.terminal[$0]($1)" , name , (Serializable ) args [0 ]);
133132 } else {
134133 Serializable [] sargs = new Serializable [args .length ];
135134 System .arraycopy (args , 0 , sargs , 0 , args .length );
136135 String expr =
137136 IntStream .rangeClosed (1 , args .length )
138137 .mapToObj (i -> "$" + i )
139138 .collect (Collectors .joining ("," ));
140- return executeJs (" this.terminal[$0](" + expr + ")" , name , sargs );
139+ return executeJs (hasResult , "return this.terminal[$0](" + expr + ")" , name , sargs );
141140 }
142141 }
143142 }
@@ -188,22 +187,22 @@ private void afterInitialization() {
188187 }
189188 }
190189
191- protected CompletableFuture <JsonValue > executeJs (String expression , Serializable ... parameters ) {
192- if (deferredCommands == null ) {
190+ private CompletableFuture <JsonValue > executeJs (boolean hasResult , String expression , Serializable ... parameters ) {
191+ if (!hasResult ) {
192+ executeJs (expression , parameters );
193+ return CompletableFuture .completedFuture (null );
194+ } else if (deferredCommands == null ) {
193195 return getElement ().executeJs (expression , parameters ).toCompletableFuture ();
194196 } else {
195- CompletableFuture <JsonValue > future = new CompletableFuture <>();
196- deferredCommands .add (
197- () -> {
198- try {
199- executeJs (expression , parameters ).get ();
200- } catch (ExecutionException e ) {
201- future .completeExceptionally (e .getCause ());
202- } catch (Exception e ) {
203- future .completeExceptionally (e );
204- }
205- });
206- return future ;
197+ throw new IllegalStateException ("Component is not attached" );
198+ }
199+ }
200+
201+ protected void executeJs (String expression , Serializable ... parameters ) {
202+ if (deferredCommands == null ) {
203+ getElement ().executeJs (expression , parameters );
204+ } else {
205+ deferredCommands .add (() -> getElement ().executeJs (expression , parameters ));
207206 }
208207 }
209208
0 commit comments