I'm calling some existing JS that returns a JS Object which I've implemented in JsInterop:
@JsType(isNative = true, namespace = "window.CrazyGames.SDK")
public static class JsUser {
public native Promise<CrazyGamesUser> getUser();
}
I can happily call it:
sdk.user.getUser()
.then(user -> {
// Do something with the user
return null;
})
.catch_(error -> {
return null;
});
This issue is I'm struggling to work out how to define the return object "CrazyGamesUser". The actual JS object is just this:
If I define it like this:
@JsType(isNative = true, namespace = JsPackage.GLOBAL)
public static class CrazyGamesUser {
public String username;
public String profilePictureUrl;
}
I get a java.lang.ClassCastException.
So if I set the name to "?":
@JsType(isNative = true, name = "?", namespace = JsPackage.GLOBAL)
public static class CrazyGamesUser {
public String username;
public String profilePictureUrl;
}
Then I get a compile error:
'?' can only be used as a name for native interfaces in the global namespace.
But if I make it an interface, I can't have the member variables.
If I do remove the member variables, it does work, and I can access them via some JSNI:
public static native String getUsername(CrazyGamesUser instance) /*-{
return instance.username;
}-*/;
But that's really ugly. What's the correct approach here?
Thanks.