Note that "@FunctionalInterface" is not required to express a Java 
interface with only one abstract method as a lambda. You can write:

DomGlobal.window.onpopstate = event -> {
    // Do something
    return null;
};

to use it as a lambda. This compiles for me with GWT 2.12.1 and Java 11.

The key is the "return null", missing in your example (and in Thomas's as 
well as a return type for the anon inner class). 

The return type is declared as Object, which may be an oversight in the 
original closure externs file, I don't see a way that an event handler can 
change anything by returning a different value. In JS, a missing return 
would implicitly return undefined, but Java requires an explicit return for 
non-void methods.
On Wednesday, December 4, 2024 at 3:54:27 AM UTC-6 Thomas Broyer wrote:

> addEventListener indeed is an option (and I'd say possibly a better 
> option), but AFAICT you could also have done:
>
> DomGlobal.window.onpopstate = new OnpopstateFn() {
>     @Override
>     public onInvoke(Event event) {
>         // do something
>     }
> });
>
> On Wednesday, December 4, 2024 at 5:23:00 AM UTC+1 
> [email protected] wrote:
>
>> I'm finally getting around to replacing these calls with Elemental2.
>>
>> To replace the JSNI method:
>>
>> public static native void initialise() /*-{
>> $wnd.onpopstate = $entry(function(e) {
>> @com.blah.YourClass::yourMethodThatListensForUrlChange()();
>> });
>> }-*/;
>>
>> I would have thought it would be:
>>
>> public static void initialise() {
>> DomGlobal.window.onpopstate.onInvoke(event -> {
>> // Do something
>> });
>> }
>>
>> However, that doesn't compile, as onInvoke takes a @JsFunction, not 
>> a @FunctionalInterface.  So, I have to do something like this:
>>
>> public static void initialise() {
>> DomGlobal.window.addEventListener("popstate", event -> {
>> // Do something
>> });
>> }
>>
>> Is this correct?
>>
>> On Friday, 16 October 2020 at 3:40:39 pm UTC+11 Craig Mitchell wrote:
>>
>>> As Tomas said, not directly with History.  It's simple JSNI though:
>>>
>>> public static native void updateURL(String newUrl) /*-{
>>>     $wnd.history.pushState(newUrl, "", newUrl);
>>> }-*/;
>>>
>>> public static native void initialise() /*-{
>>> $wnd.onpopstate = $entry(function(e) {
>>> @com.blah.YourClass::yourMethodThatListensForUrlChange()();
>>> });
>>> }-*/;
>>>
>>>

-- 
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 [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/google-web-toolkit/e13d42ad-95ac-422e-8694-db9dc997723cn%40googlegroups.com.

Reply via email to