Monday, January 15, 2018

Re: @FunctionalInterface VS @JsFunction



On Monday, January 15, 2018 at 4:03:11 PM UTC+1, Jan Blok wrote:
Hi,

Would it be possible for GWTc to handle java.lang.FunctionalInterface as if it was jsinterop.annotations.JsFunction ?

No, if only because JsFunction has many constraints: http://www.gwtproject.org/javadoc/latest/jsinterop/annotations/JsFunction.html
 
The reason for asking is, I have this java code:

    public void transaction(Runnable runnable) {
        startTransaction();
        try {
            runnable.run();
        }
        finally {
            endTransaction();
        }
    }

when I expose this method via jsinterop I get a warning "not usable from javascript"

I intent to use it from JS/TS as: myJavaLib.transaction(() => doSomethingInTransaction(...));

but this is not possible unless I supersource java.lang.Runnable and add @JsFunction 

You'd need to create an overload taking a JsFunction as input, and exposing that one.

@JsFunction @FunctionalInterface
interface TransactionCallback {
 
void run();
}


@JsMethod
public void transaction(TransactionCallback callback) {
  startTransaction
();
 
try {
    callback
.run();
 
} finally {
    endTransaction
();
 
}
}

// Note: not a @JsMethod
public void transaction(Runnable runnable) {
  transaction
((TransactionCallback) runnable::run);
}

 
It seems to me that @FunctionalInterface and @JsFunction are very similar already...

And this is why the javadoc for JsFunction recommends also annotating the interface with FunctionalInterface. 

--
You received this message because you are subscribed to the Google Groups "GWT Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-web-toolkit+unsubscribe@googlegroups.com.
To post to this group, send email to google-web-toolkit@googlegroups.com.
Visit this group at https://groups.google.com/group/google-web-toolkit.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment