Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » Remote Object - instant variable transmittion of both sides?(How to instantly update Java side variable from JS)
Remote Object - instant variable transmittion of both sides? [message #1856144] Wed, 23 November 2022 03:30 Go to next message
David Lee is currently offline David LeeFriend
Messages: 80
Registered: May 2013
Member
Dear all,

I am trying to implement a RAP Remote Object, my code for test as below.

JavaScript
------
isEmpty : function() {
const isEmpty = true;
const jsonIsEmpty = {"isEmpty":isEmpty};
console.log(isEmpty); // excuted finally, but shows after Java call completely finished ?!
rap.getRemoteObject(this).call("Pad", jsonIsEmpty); // try to immeditelly update Java side variable
}

Java remote object(in the form of a plugin)
---
RemoteObject remoteObject;
private boolean isPadEmpty = false;

@Override
public void handleCall(String method, JsonObject parameters) {
switch (method) {
case "Pad":
JsonValue padIsEmpty = (JsonValue) parameters.get("isEmpty");
if (padIsEmpty != null) {
isPadEmpty = padIsEmpty.asBoolean();
System.out.println(isPadEmpty); // shows true, correct
}
break;

default:
break;
}
}

// call JavaScript function
this.remoteObject.call("isEmpty", null);
System.out.println( this.isPadEmpty ); // shows false?! incorrect


The code above works well, but the JavaScript functions excution ALWAYS AFTER Java call finished, I think that's why the variable 'private boolean isPadEmpty' cannot be changed instantly.

I need to use value returned from the JavaScript function to instantly update desired variables outside handleCall() method. and all are completed in a single Java function/method, not the next time of call!

Please point me out why that and the solutions!
If the code above is not enough to express, please let me know!
Thank you so much!

David

[Updated on: Wed, 23 November 2022 13:01]

Report message to a moderator

Re: Remote Object - instant variable transmittion of both sides? [message #1856188 is a reply to message #1856144] Fri, 25 November 2022 09:00 Go to previous messageGo to next message
David Lee is currently offline David LeeFriend
Messages: 80
Registered: May 2013
Member
In short, how to instantly get return value from JS method within the SAME Java method?

for example:(based on my previouse post above)
private void test(){
boolean empty = false;
this.remoteObject.call("isEmpty", null); // JS method "isEmpty" returns true and changes variable 'empty ' from false to true here
System.out.println( empty ); // shows true here
}

Thank you very much!

David

[Updated on: Fri, 25 November 2022 09:01]

Report message to a moderator

Re: Remote Object - instant variable transmittion of both sides? [message #1856213 is a reply to message #1856188] Mon, 28 November 2022 09:26 Go to previous messageGo to next message
Julien Guigné is currently offline Julien GuignéFriend
Messages: 9
Registered: April 2022
Junior Member
Hello,

The operation handler (handleSet / handleCall / handleNotify) receives notifications from your JS remote object.

When it does this.remoteObject.call("isEmpty", null); , the isEmpty method will be executed in JS remote object (if properly registered).

The remote object should make a callback like :
var obj = rap.getRemoteObject(this);
if (obj)
    obj.notify(EVENT_XX, json)

Then handle notification :
@Override
public void handleNotify(String event, JsonObject properties)
{
	if (StringUtils.equals(event, EVENT_XX))
	{
		JsonValue vCommand = properties.get("yourProperty");
		if (vCommand != null)
		{
			// ...
		}
	}
}


When EVENT_XX is a same String defined in Java and JS, and properly registered.

Here a full example of remote object implementation :
https://o7planning.org/10361/create-eclipse-rap-widget-from-clientscripting-based-widget

Moreover I'm not sure about
rap.getRemoteObject(this).call("Pad", jsonIsEmpty);
I've always used notify (which is immediate) or set (which needs update) to notify from javascript to java.

Regards.

[Updated on: Mon, 28 November 2022 09:32]

Report message to a moderator

Re: Remote Object - instant variable transmittion of both sides? [message #1856233 is a reply to message #1856213] Tue, 29 November 2022 04:49 Go to previous messageGo to next message
David Lee is currently offline David LeeFriend
Messages: 80
Registered: May 2013
Member
Hi, Julien,

Thank you for your reply!
Yes, I studied the o7 example you said before.
I tried to express the problem more clear based on your code, please see below.

private boolean isPadEmpty = false;
public boolean isPadEmpty() {
		return isPadEmpty;
}

@Override
public void handleNotify(String event, JsonObject properties)
{
	if (StringUtils.equals(event, EVENT_XX))
	{
		JsonValue vCommand = properties.get("yourProperty");
		if (vCommand != null)
		{
			// ... 
                       // assuming JavaScript function  'isEmpty' returns true here
                       isPadEmpty =  vCommand.asBoolean(); // to transmit value to variable 'isPadEmpty' (see top)
		}
	}
}

public boolean checkPadEmpty() {
		this.remoteObject.call("isEmpty", null);
                System.out.println( this.isPadEmpty ); // shows false?! incorrect
		return this.isPadEmpty;
}


When checkPadEmpty() invoked, I found its excution is in the following order:
this.remoteObject.call("isEmpty", null);
-> System.out.println( this.isPadEmpty ); // expected true, but it's false?!
-> return this.isPadEmpty;
-> JavaScript function 'isEmpty' returns true // excuted after checkPadEmpty() completed,
-> handleNotify(String event, JsonObject properties){ },

Actually, no matter using handCall() or handNotify(), the JavaScript functions excute ALWAYS RIGHT AFTER Java checkPadEmpty() finished, don't know why?!
I think that's why we cannot instantly get the correct/updated value of 'this.isPadEmpty' in checkPadEmpty().

Expected in the following order:
this.remoteObject.call("isEmpty", null);
-> JavaScript function 'isEmpty' returns true
-> handleNotify(String event, JsonObject properties){
isPadEmpty = (the value sent from JavaScript function 'isEmpty')
},
-> System.out.println( this.isPadEmpty ); // shows same value as JavaScript function 'isEmpty'
-> return this.isPadEmpty; // return updated value

We need updated value of 'this.isPadEmpty' instantly to decide what to do next!

---
Moreover I'm not sure about
rap.getRemoteObject(this).call("Pad", jsonIsEmpty);
I've always used notify (which is immediate) or set (which needs update) to notify from javascript to java.
---
"Pad" here is a method name to be identified in handleCall().
BTW, both handleCall() and handleNotify() seem to be of the same way to parse "String, JsonObject".

Briefly, my only question is how to get updated value returned from JavaScript function 'isEmpty' before checkPadEmpty() finishes.

Any help would greatly be appreciated!


Best Regards,
David


[Updated on: Tue, 29 November 2022 06:21]

Report message to a moderator

Re: Remote Object - instant variable transmittion of both sides? [message #1856241 is a reply to message #1856233] Tue, 29 November 2022 08:59 Go to previous messageGo to next message
Julien Guigné is currently offline Julien GuignéFriend
Messages: 9
Registered: April 2022
Junior Member
Hi,

try to debug your code with breakpoints and see code execution order.

I think it's not possible to call client remote object and instantly get the response...

When you call this.remoteObject.call("isEmpty", null);, server has to notify client, then in your case client must callback server, and you get the callback in the operation handler.

If you want instant notifications, I suggest you to move your empty-check process on client-side.
RAP client-side remote objects are designed for this need, when RAP default widgets can't do that because of network latency (like mouseover events, caret events...).

Otherwise there is no other choice to wait for the callback (with asynchronous process ?).

In your case, your java class should be passive.
Your "isEmpty" attribute is updated by operationHandler notifications.
isPadEmpty() is enough. "isEmpty" is likely to differ from client-side if it changes very quickly but I don't think we can do better.

[Updated on: Tue, 29 November 2022 09:02]

Report message to a moderator

Re: Remote Object - instant variable transmittion of both sides? [message #1856258 is a reply to message #1856241] Tue, 29 November 2022 21:37 Go to previous messageGo to next message
David Lee is currently offline David LeeFriend
Messages: 80
Registered: May 2013
Member
Hi, Julien,

Thank you for quick reply!

So, the mechanism of RemoteObject is actually in asynchronous way to communicate both of Java and JavaScript, just like non-blocking operation,
that's why remoteObject.set()/remoteObject.call() are void functions without returning data,
and handleSet()/handleCall()/handleNotify() are specifically used to receive and manage property/method/event sent from JavaScript,
these 3 functions have no operation connection with remoteObject.set()/remoteObject.call().
There is no way to call client remote object and instantly get the response!

Is my understanding above correct?
Thank you.

Best,
David

[Updated on: Tue, 29 November 2022 21:53]

Report message to a moderator

Re: Remote Object - instant variable transmittion of both sides? [message #1856315 is a reply to message #1856258] Fri, 02 December 2022 09:16 Go to previous message
David Lee is currently offline David LeeFriend
Messages: 80
Registered: May 2013
Member
Hi,

I solved the problem by coding in onSend(), which can actively call handleNotify(), and instantly update other variables!
RAP RemoteObject is a pretty good mechanism!

David

[Updated on: Fri, 02 December 2022 09:33]

Report message to a moderator

Previous Topic:How to send data from JS to Java?
Next Topic:Combo and color for items?
Goto Forum:
  


Current Time: Fri Jun 14 07:14:43 GMT 2024

Powered by FUDForum. Page generated in 0.03964 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top