| Simple non-UI object example [message #846346] |
Sun, 15 April 2012 21:34  |
Eclipse User |
|
|
|
I'm trying to understand how to use RAP to provide client<->server communication without a widget in the loop. I've tried to use the Geolocation code in the rap-mobile github source and AbstractObjectSyncronizer seemed like it might be the clue I needed...but there was no client-side code in the Git repo for me to complete the picture.
Here is a simple hypothetical that I hope will point me in the correct direction. I have a third-party Javascript library that I load into RAP using the org.eclipse.rap.ui.resources extension point. This Javascript library has a function ThirdPary.isSomethingReady().
In the Activator of my bundle I want to execute this client-side code and then read the result. My naive approach is to try something like this:
JSExecutor.executeJS("var isReady = ThirdPary.isSomethingReady()");
But then I'm stuck figuring out how to read the isReady result. Thanks for any help/illumination you can provide!
~Michael
|
|
|
|
|
|
|
| Re: Simple non-UI object example [message #849880 is a reply to message #847912] |
Thu, 19 April 2012 10:31   |
Eclipse User |
|
|
|
Ralf/Mark,
Thanks for the help. I've included a full example below for others who run across this thread...however, I struggled to find a way to do exactly what I was trying to accomplish. My goal was to provide a synchronous API...something like this:
public class MyWidget implements IEntryPoint {
public int createUI() {
Display display = new Display();
if (isThirdPartyReady()) {
// Do one thing
} else {
// Do another thing
}
}
public boolean isThirdPartyReady(IProgressMonitor monitor) {
// add life cycle listener
// execute JS
// while !monitor.isCanceled()
// check for response
// return response
}
}
This, of course, will produce a deadlock because isThirdPartyReady was called in the UIThread and the LifeCycleListeners are also called in the UIThread. I tried using a custom ServiceHandler and qx.io.request.Xhr() but I was unable to get that to work (should I continue to try down this path?). Fundamentally I'm attempting to execute client-side code and receive the response while blocking the UI thread...perhaps this is a naive goal.
In my particular case, I want to build my UI differently based off results of client-side execution. For example:
public int createUI() {
if (detectedSomethingAboutClientSide()) {
// series of SWT widget creation
} else {
// alternate series of SWT widget creation
}
}
I can accomplish this through asynchronous callbacks (like below), it just makes the code a bit more obtuse. In my particular case, it would suffice if I could contribute client-side code into rwt-index.html initialization. I'm thinking something like IEarlyStartupJS. Javascript from this extension-point would be executed in rwt-index.html and be able to contribute parameters to the "rwt_initialize" message. The LifeCycleServiceHandler would store these in RWT.getSessionStore() or some other location. Is this a useful idea? If so, I will happily make the changes and provide a patch.
Anyways here is the complete final solution:
public interface IAsyncCallback<T> {
public void onSuccess(T result);
public void onFailure(Throwable t);
}
...
public static void isThirdPartyReady(final Display display, final IAsyncCallback<Boolean> callback) {
RWT.getLifeCycle().addPhaseListener(new PhaseListener() {
@Override
public PhaseId getPhaseId() {
return PhaseId.READ_DATA;
}
@Override
public void beforePhase(PhaseEvent event) {
}
@Override
public void afterPhase(PhaseEvent event) {
if (display != LifeCycleUtil.getSessionDisplay()) return;
HttpServletRequest req = RWT.getRequest();
String isThirdPartyReadyStr = req.getParameter("isThirdPartyReady");
if ((req != null) && (isThirdPartyReadyStr!= null)) {
final boolean isThirdPartyReady = Boolean.parseBoolean(isThirdPartyReadyStr);
if (callback != null) {
SafeRunner.run(new ISafeRunnable() {
@Override
public void run() throws Exception {
callback.onSuccess(isThirdPartyReady);
}
@Override
public void handleException(Throwable exception) {
}
});
}
RWT.getSessionStore().setAttribute("isThirdPartyReady", isThirdPartyReady );
RWT.getLifeCycle().removePhaseListener(this);
}
}
});
StringBuilder code = new StringBuilder();
code.append("var req = org.eclipse.swt.Request.getInstance();\n");
code.append("req.addParameter(\"isThirdPartyReady\", ThirdParty.util.isReady());\n");
code.append("req.send();\n");
JSExecutor.executeJS(code.toString());
}
Thanks again for all the help.
~Michael
|
|
|
|
|
Powered by
FUDForum. Page generated in 0.05901 seconds