vaadin / vaadin/framework

When dispatching an asynchronous task I want to easily create completion handlers that use UI.access so that I can avoid boilerplate

Open
#9,718 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Java
Stars
1.8k
Forks
717
Avg merge
2d 6h
Merged PRs (30d)
3

Description

Add shorthands to UI for wrapping a callback with a call to UI.access.

public Runnable accessLater(Runnable runnable) {
    return () -> access(runnable);
}

public <T> Consumer<T> accessLater(Consumer<T> consumer) {
    return t -> access(() -> consumer.accept(t));
}

public <T, U> BiConsumer<T, U> accessLater(BiConsumer<T, U> biConsumer) {
    return (t, u) -> access(() -> biConsumer.accept(t, u));
}

As an example of how this can be used, imagine a service that asynchronously produces a String and passes it to a provided consumer when ready, i.e. public static void requestString(Consumer<String> consumer). Without the suggested helpers, the service would be used like this:

Service.requestString(string -> myUi.access(() -> myLabel.setValue(string)));

With the proposed helpers in UI, the same result could be achieved like this:

Service.requestString(myUi.accessLater(myLabel::setValue));

This kind of API also works nicely with the lambda-friendly API that was introduced in Akka 2.3 (and further refined in later versions). As an example, the createReceive method from AbstractActor could be implemented like this:

return receiveBuilder()
  .match(SomeMessage.class, myUi.accessLater(this::handleSomeMessage))
  .build();

where handleSomeMessage could be defined as private void handleSomeMessage(SomeMessage message) and would in this way automatically be run inside UI.access.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the UI API and its existing access method, then compare the requested Runnable, Consumer, and BiConsumer accessLater entry points with the asynchronous callback examples in the issue. Done means callbacks can be passed directly to services or AbstractActor handlers and are executed through UI.access.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
frontend
Issue type
Feature
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.