0

Is it safe to do nested asynchronous calls (websocket calls) with Vaadin like:

    var ui1 = UI.getCurrent();
    listenableFuture1.addCallback(page1 -> {
        ui1.access(() -> {
            //update ui here, then:
            var ui2 = UI.getCurrent();
            listenableFuture1.addCallback(page2 -> {
                ui2.access(() -> {
                    //update ui here, than:
                    // one more WS call, and so on
                }
            }
        }
    }

Also, inside of ui.access block should I use ui reference locked before, or it is safe to use UI.getCurrent() also?

alexanoid
  • 24,051
  • 54
  • 210
  • 410
  • Slightly unrelated, but I'm curious to understand what makes you worry that it wouldn't be safe? – Leif Åstrand Nov 21 '22 at 07:05
  • First of all, thanks for your answer! The reason why I asked this - I already pulled out all the hair on my head while struggling with the problem of losing session(or resync) on my iPhone :) so I started suspecting any line of code in my application and thinking that this could be the cause of this problem :) But I hope I found the reason now - looks like Vaadin heartbeat doesn't work on my iPhone in the idle state of the phone. This is why my app destroys the entire HTTP session after timeout in 30 mins. – alexanoid Nov 21 '22 at 08:08
  • I don't know what to do right now other then extend HTTP session timeout to 1d or even 7days.. Perhaps you can recommend something? – alexanoid Nov 21 '22 at 08:09

1 Answers1

3

It's safe to arbitrarily nest access calls. What happens behind the scenes is that the task is first added to a queue of pending access tasks. That queue is then purged at a suitable occasion. If the session is currently locked when the task is added, then it is run when the current lock is about to be released. If it's not currently locked, then the thread running access acquires the lock and runs the task right away.

It shouldn't matter whether you reuse and existing reference to UI or use UI.getCurrent() again since both will reference the same instance.

Leif Åstrand
  • 7,820
  • 13
  • 19