Thursday, June 2, 2016

gwt-inerop-utils preview

I have committed an initial version of gwt-interop-utils.


This library provides some common utilities for working with JsInterop e.g.

1) Object Literal support
2) Shared JSON compatible structures
3) Common functional interfaces
4) JSON utilities
5) Low level Javascript utilities.

One of my major goals was to enable creating complex object literals with arrays and maps that could be represented by a single class, accessible both on the client and server. You can now define a class such as

import gwt.interop.utils.shared.collections.Array;
import gwt.interop.utils.shared.collections.StringMap;
import gwt.interop.utils.shared.valuetypes.NumberValue;

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object")
public class CommonDataObject {
public int intVal;
public double doubleVal;
public boolean booleanVal;
public String stringVal;
public NumberValue numberVal;
public Array<String> anArray;
public StringMap<String> aMap;
public CommonDataObject2 embeddedObj;

@JsOverlay
public static CommonDataObject create() {
CommonDataObject o = new CommonDataObject();
o.intVal = 10;
o.doubleVal = 20.20;
o.booleanVal = true;
o.stringVal = "A String Value";
o.numberVal = NumberValue.from(10);
o.anArray = Array.create();

o.anArray.set(0, "ArrayValue1");
o.anArray.set(2, "ArrayValue2");
o.anArray.set(2, "ArrayValue3");

o.aMap = StringMap.create();

o.aMap.put("v1", "A Map Value 1");
o.aMap.put("v2", "A Map Value 2");

o.embeddedObj = new CommonDataObject2();
o.embeddedObj.field1 = "An embbeded object field";
return o;
}

@JsOverlay
public final String convolutedSharedMethod(String someArg) {
StringBuilder o = new StringBuilder();

anArray.forEachElem((e) -> {
o.append(aMap.get(someArg));
o.append(embeddedObj.field1);
o.append(e);
});

return o.toString();
}
}

@JsType(isNative = true, namespace = JsPackage.GLOBAL, name = "Object")
public class CommonDataObject2 {
public String field1;
}

Instances of this class can be encoded to/from JSON using standard JSON functions. You can also create/use the same class on the server and full emulation is provided. Given this setup, it gives you a lot of the benefits of GWT RPC (i.e. shared data objects and behavior across client/server) without the code bloat or performance hit.

Before I publish it to Maven, I still need to finish the docs and do some more testing. I am also waiting for some compiler fixes to land.

Please take a look and let me know if there are any changes/additions you would like.

--
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