Skip to content

Hey there! If you like my work, you can support me by sponsoring me on GitHub. Thanks! ❤

Sponsor me
on GitHub

Async Argument

To parse arguments asynchronously you can choose from three different ways:

  1. Create ArgumentResolver with an async ParseResult<T> (recommended)
  2. Add @Async annotation before argument
  3. Use argument with the CompletableFuture<T> type

To create an argument resolver that parses arguments asynchronously, you need to:

  • Return a ParseResult.completableFuture() in the parse() method
  • Validate the argument synchronously in the match() method
java
public class UserArgumentResolver extends ArgumentResolver<CommandSender, User> {

    private final Pattern VALID_USER_PATTERN = Pattern.compile("^[a-zA-Z0-9_]{3,16}$");
    private final UserService userService;

    public UserArgumentResolver(UserService userService) {
        this.userService = userService;
    }

    @Override
    protected ParseResult<User> parse(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        CompletableFuture<User> userFuture = userService.getUser(argument);

        return ParseResult.completableFuture(userFutur, user -> {
            if (user == null) {
                return ParseResult.failure("User not found.");
            }

            return ParseResult.success(user);
        });
    }

    @Override
    public boolean match(Invocation<CommandSender> invocation, Argument<User> context, String argument) {
        return VALID_USER_PATTERN.matcher(argument).matches();
    }
}

Full example

See this example on GitHub.

2. @Async annotation

You need to use the @Async annotation on the argument:

Java
@Command(name = "user")
public class UserCommand {

    @Execute(name = "load")
    void execute(@Async @Arg User argument) {
        System.out.print("This message is triggered in the main thread!");
    }
}

WARNING

The @Async annotation on the argument will parse the argument asynchronously, but the command will be executed in the main thread.

3. CompletableFuture<T> type

Using CompletableFuture<T> is more complicated:

java
@Command(name = "user")
public class UserCommand {

    @Execute(name = "load")
    void execute(@Arg CompletableFuture<User> argument) {
        System.out.print("This message is triggered in the main thread!");
        
        argument.whenComplete((user, error) -> {
            if (error != null) {
                return;
            }
            
            System.out.print("This message is triggered asynchronously!");
        });
    }
}

Made by LiteDevelopers and Contributors with ❤️

Layout Switch

Adjust the layout style of VitePress to adapt to different reading needs and screens.

Expand all
The sidebar and content area occupy the entire width of the screen.
Expand sidebar with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Expand all with adjustable values
Expand sidebar width and add a new slider for user to choose and customize their desired width of the maximum width of sidebar can go, but the content area width will remain the same.
Original width
The original layout width of VitePress

Page Layout Max Width

Adjust the exact value of the page width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the page layout
A ranged slider for user to choose and customize their desired width of the maximum width of the page layout can go.

Content Layout Max Width

Adjust the exact value of the document content width of VitePress layout to adapt to different reading needs and screens.

Adjust the maximum width of the content layout
A ranged slider for user to choose and customize their desired width of the maximum width of the content layout can go.

Spotlight

Highlight the line where the mouse is currently hovering in the content to optimize for users who may have reading and focusing difficulties.

ONOn
Turn on Spotlight.
OFFOff
Turn off Spotlight.