SwingUtilites.invokeLater or SWT's Display.asyncExec analog for Vaadin

In desktop UI toolkits there are things such java.awt.EventQueue.invokeLater (or SwingUtilites.invokeLater) that helps you to execute some Runnable asynchronically AFTER the calling thread complete. Example from javadoc for SwingUtilities class:

In the following example the invokeLater call queues
    the Runnable object doHelloWorld
    on the event dispatching thread and
    then prints a message.
    
    Runnable doHelloWorld = new Runnable() {
    public void run() {
    System.out.println("Hello World on " + Thread.currentThread());
     }
  };
 
 SwingUtilities.invokeLater(doHelloWorld);
  System.out.println("This might well be displayed before the other message.");

Of cource, it is possible implement similar class class in core-vaadin to execute some async Runnable after calling thread finished. To apply changes of UI that was done in his async Runnable we can use poll or push mechanism (Refresher, ICEPushAddon), becouse pending thread send the changes to client and complete, and UI changes in our Runnable is not send to client on his request. But using poll or push is overkill in case when I just want to some appending to client request in async way. There are simple way to implented this. Suppose that there are exists some class InvokeLaterExecutor that have method addInvokeLaterRunnable allowing to add Runnable. The VaadinServlet after collecting UI changes call InvokeLaterExecutor’s execute method that execute all Runnables added with addInvokeLaterRunnable method in adding order. So features that given by EventQueue.invokeLater can be obtained in vaadin. Currently I don’t see this ability in Vaadin. It will be great if vaadin team will implement this, becouse it is total simply and very useful in many cases.

I have implement this functionality. I have take as a base the existing org.vaadin.osgi bundle (becouse I use it in my project) and add to it necessary functionality. In short, I have create custom own VaadinOsgiCommunicationManager with public method invokeLater(Runnable) and override method changeVariables. After calling super.changeVariables that execute all user code I invoke all runnables that this user code is added. It is more simple to see than tell it:


public class VaadinOSGiCommunicationManager extends CommunicationManager
{
	Queue<Runnable> runnables = new LinkedList<>();
	
	public VaadinOSGiCommunicationManager(Application application)
	{
		super(application);
		VaadinOSGiApplicationManager.getInstance().setCommunicationManager(this);
	}
	
	protected void changeVariables(Object source, final VariableOwner owner, Map<String, Object> m) {
        super.changeVariables(source, owner, m);
        
        exec();
    }
	
	private synchronized void exec()
	{
		Runnable runnable;
    	while ((runnable = runnables.poll()) != null)
        {
        	try
			{
        		runnable.run();
			}
			catch (Throwable e)
			{
				e.printStackTrace();
			}
        }
	}

	public synchronized void invokeLater(Runnable runnable)
	{
		this.runnables.add(runnable);
	}	
}

How to use:


getCommunicationManager().invokeLater(new Runnable() {
@Override
public void run()
{
System.out.println("This message will be displayed after all pending actions will be completed");
}
});

The project is in github - as I tell above, it extends existing org.vaadin.osgi project and implemented as osgi bundle. How to install and use it - see in documentation of org.vaadin.osgi project.

https://github.com/semanticsoft/vaaclipse/tree/master/org.semanticsoft.vaadin.osgi