Wait until retrieveExtendedClientDetails callback

Hi, i’m using retrieveExtendedClientDetails in a MySession class that is initialized when a new session is started.

Than i need to store the timeZone in this class like this.

UI.getCurrent().getPage().retrieveExtendedClientDetails(details -> {

	// Set the time zone
	setUITimeZone(TimeZone.getTimeZone(details.getTimeZoneId()));

});

while (true) {

	if( getUITimeZone() != null ) break;

	Thread.sleep(50);
}

i tried adding while statement, but it never return and the browser stay white and waiting.

Seems that the retrieveExtendedClientDetails function returns after the page is loaded into the browser.

The MySession class is instantiated in the MainLayout constractor, but at that moment the page is not loaded into the browser.

So, what can i do ? any ideas ? ( May be using a loading page ? )

Thanks

but it never return and the browser stay white and waiting

Yes. That happens because Thread.sleep(…) will stop the request thread execution. So the logic of yours is flawed.

You options are

  1. If you need the time zone info immediately, you can call the method needing that in the same lambda
UI.getCurrent().getPage().retrieveExtendedClientDetails(details -> {

	// Set the time zone
	timeZone = TimeZone.getTimeZone(details.getTimeZoneId());
	setUITimeZone(timeZone);
    doWhatYouNeedToDo(timeZone);
});
  1. Retrieve the time zone when your mainlayot is attached (it should happen quickly after your app is started)
    @Override
    protected void onAttach(AttachEvent attachEvent) {
        setUITimeZone(TimeZone.getTimeZone(details.getTimeZoneId()));
    }

And provide method to access saved data later. It is quite likely that when you need TimeZone info in your app, it happens long after mainlayout has been attached.

Tatu Lund:

but it never return and the browser stay white and waiting

Yes. That happens because Thread.sleep(…) will stop the request thread execution. So the logic of yours is flawed.

You options are

  1. If you need the time zone info immediately, you can call the method needing that in the same lambda
UI.getCurrent().getPage().retrieveExtendedClientDetails(details -> {

	// Set the time zone
	timeZone = TimeZone.getTimeZone(details.getTimeZoneId());
	setUITimeZone(timeZone);
    doWhatYouNeedToDo(timeZone);
});
  1. Retrieve the time zone when your mainlayot is attached (it should happen quickly after your app is started)
    @Override
    protected void onAttach(AttachEvent attachEvent) {
        setUITimeZone(TimeZone.getTimeZone(details.getTimeZoneId()));
    }

And provide method to access saved data later. It is quite likely that when you need TimeZone info in your app, it happens long after mainlayout has been attached.

Hi Tatu, thanks for your help.

Yes, my logic is flawed, but i’m not able to find the proper logic easily.

The main problem is tha i instantiate MySession class into the first call of MainLayout class.


@Push
@Theme(Lumo.class)
@PWA(name = "My app", shortName = "My app")
public class MainLayout extends AppLayoutRouterLayout<LeftLayouts.LeftHybrid> {

	private MySession cs = null;

	public MainLayout() {

		cs = MySession.getCurrent();  
		// Here the retrieveExtendedClientDetails is called, but the callback is called at the moment javascript is ready
						
		Image logo = cs.getImageLogoHeader();
		
// than setup the views that uses the timezone.
		

May be the only solution is to load the login page without MainLayout, instantiate the MySession class and than redirect to the first view that uses mainLayout after login.

Anyway, thanks for your help.