Giter VIP home page Giter VIP logo

async-manager's Introduction

Published on Vaadin  Directory Latest published version Stars on vaadin.com/directory Build status

Async Manager for Vaadin Flow

In complex application quite often you end up having a view that takes ages to load because some parts of view require heavy computation. If you are pursuing the goal of making your application responsive, you would probably want to defer updates for these parts. It's quite tempting to use UI.access() for that:

...
ui.access(() -> {
    SomeData result = doHeavyLifting();
    showData(result);
});

While it will make load time much faster, there is a problem there: since the UI is blocked by computation thread, user will have to wait for it to finish before his actions will be processed. That is, he will see the same loading indicator if he, for example, click some button that has server side click listener.

Practically that means that you have to create a worker thread yourself and then call UI.accessSynchronously() when done:

Thread workerThread = new Thread(() -> {
    SomeData result = doHeavyLifting();
    ui.accessSynchronously(() -> showData(result));
});
workerThread.start();

That will work with Push out of the box. However, if you don't have Push enabled, you'll have to call ui.setPollingInterval() to enable polling:

ui.setPollingInterval(200);
Thread workerThread = new Thread(() -> {
    SomeData result = doHeavyLifting();
    ui.accessSynchronously(() -> {
        showData(result);
        ui.setPollingInterval(-1); // Disable polling afterwards
    });
});
workerThread.start();

Now we have another tiny problem: we need to clear polling interval if the user is leaving the view before thread finishes:

ui.setPollingInterval(200);
Thread workerThread = new Thread(() -> {
    SomeData result = doHeavyLifting();
    ui.accessSynchronously(() -> {
        showData(result);
        ui.setPollingInterval(-1); // Disable polling afterwards
    });
});
workerThread.start();
addBeforeLeaveListener(event -> ui.setPollingInterval(-1));

See how easy our 4 line snippet turns to 10 line monster? And what if we have several of those worker threads? Moreover, even if we have push enabled, something should terminate threads when their results are not needed anymore (i.e. user has left the view).

But wander no more, there is an easy solution: Async Manager. It is really easy to use:

AsyncManager.register(this, task -> {
    SomeData result = doHeavyLifting();
    task.push(() -> showData(result));
})

AsyncManager supports both push and polling, and takes care of cleanup and thread termination. For polling mode it also supports dynamic polling intervals: i.e. you can have 5 polls per second in the first second and then throttle it to send poll requests once per second:

AsyncManager.getInstance().setPollingIntervals(200, 200, 200, 200, 200, 1000);

It is also possible to set custom exception handler if you want some custom logging or exception reporting:

AsyncManager.getInstance().setExceptionHandler((task, exception) -> ...);

Note: By default all worker threads are started by ThreadPoolExecutor which defaults to pool size of 25 threads. You can change that with AsyncManager.getInstance().setExecutorService().

Installing with Maven

<repository>
   <id>vaadin-addons</id>
   <url>http://maven.vaadin.com/vaadin-addons</url>
</repository>
<dependency>
   <groupId>org.vaadin.helper</groupId>
   <artifactId>async-manager</artifactId>
   <version>1.1.0-alpha1</version>
</dependency>

Development instructions

Starting the test/demo server:

mvn jetty:run

This deploys demo at http://localhost:8080

async-manager's People

Contributors

fluorumlabs avatar jlleitschuh avatar nikolaigorokhov avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

async-manager's Issues

a way to listen when the task end

is there a way to listen to the task when it ends?
My idea for using this plugin is to display a Dialog with a customizable message like "Verifying..", "Processing.." and when the task ends put a dialog.close(), currently I have to remember to close the dialog at the end of each task

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.