Executing JavaScript in UI.access not triggered

Hi, we’re handling a task in a CompletableFuture, and when the first task completes, we call to execute Javascript in ui.access. However the JavaScript sometimes won’t run. Sometimes the only way to resolve it is to F5 browser. The code looks as such:

UI ui = UI.getCurrent();
CompletableFuture<String[]> completableFuture = CompletableFuture.supplyAsync(() -> return getInfo(pojoObject));      
completableFuture.thenAccept(args -> ui.access(() -> ui.getPage().executeJavaScript("runJs($0)", (args[2]
 != null && !args[2]
.isEmpty() ? args[2]
 : "0"))));

Help appreciated. Thanks!

First suggestion (that won’t fix the issue, but will reduce footprint) is to use ui.accessSynchronously instead of ui.access: the latter starts a new thread and tries to lock UI there, while the former does this in current thread.

Second suggestion is that if you have push disabled, you’ll need to enable polling (ui.setPollingInterval), because server cannot send anything to browser on it’s own in this case. Client must request something first, and that’s what polling is for.

Things can be made much simpler with [Async Manager add-on]
(https://vaadin.com/directory/component/async-manager): it takes care of adjusting polling intervals (if needed, push is also supported) and manages running threads.

In fact it’s even used on this website :slight_smile:

A bit of explanation why your js sometimes run and sometimes doesn’t: if your async task finishes execution before the response is sent to browser, it’s results (instruction to call js function) are also included in the response. But if it’s late, it’ll wait for the next request.

Artem Godin:
First suggestion (that won’t fix the issue, but will reduce footprint) is to use ui.accessSynchronously instead of ui.access: the latter starts a new thread and tries to lock UI there, while the former does this in current thread.

Second suggestion is that if you have push disabled, you’ll need to enable polling (ui.setPollingInterval), because server cannot send anything to browser on it’s own in this case. Client must request something first, and that’s what polling is for.

Things can be made much simpler with [Async Manager add-on]
(https://vaadin.com/directory/component/async-manager): it takes care of adjusting polling intervals (if needed, push is also supported) and manages running threads.

In fact it’s even used on this website :slight_smile:

Thanks, Artem! We’ll try the add on. It might save us a lot of time with resetting the ui’s polling. :slight_smile: