2

I created my own custom array in JsInterop:

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Array")
public interface Array<T> 
{
    public void push(T value);

    @JsProperty(name = "length")
    public int getLength();

    @JsProperty(name = "length")
    void setLength(int newLength);

    @JsOverlay
    default T get(int index) {
        return JsArrayHelper.getArrayValue(this, index);
    }
}

And here is the JsArrayHelper class scaled down:

public class JsArrayHelper 
{
    //TODO: Eliminate JSNI. Better way to do this?
    public static native <T> T getArrayValue(Array<T> a, int i) /*-{
        return a[i];
    }-*/;
}

Is there a better way to get the value of the array instead of using JSNI?

Jason Washo
  • 536
  • 6
  • 22

1 Answers1

4

Use com.google.jsinterop:base lib, this lib should include utilities for anything that cannot be done with JsInterop, and the lib will maintain compatibility with GWT and j2cl. The lib is pretty small (only 10 classes, 2 of them internal), so just add it to your project and explore all its utilities.

So instead of your custom Array<T>.get(int) and JsArrayHelper classes use jsinterop.base.JsArrayLike<T>.getAt(int).

Ignacio Baca
  • 1,538
  • 14
  • 18
  • Thank you. Trying it out now. I assume I also need to inherit: jsinterop.base.Base. But I am getting this error: "No source code is available for type jsinterop.base.JsArrayLike; did you forget to inherit a required module?" – Jason Washo Jul 10 '17 at 18:25
  • I think you need to use GWT 2.8.1. – Ignacio Baca Jul 10 '17 at 19:58