Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Commands.runIf #7807

Open
otter502 opened this issue Feb 20, 2025 · 2 comments · May be fixed by #7808
Open

Add Commands.runIf #7807

otter502 opened this issue Feb 20, 2025 · 2 comments · May be fixed by #7808
Labels
type: feature Brand new functionality, features, pages, workflows, endpoints, etc.

Comments

@otter502
Copy link

Is your feature request related to a problem? Please describe.
I often find myself writing command "logic" that will run a command if and only if a boolean supplier is true. There are ways of doing this in house but these methods aren't as readable or simple to debug as a dedicated Commands factory method would be.

Describe the solution you'd like
I think a good solution would be a version of the Commands.either() command which calls the boolean selector when the command is run and then runs the passed command if the selector returns true.

Describe alternatives you've considered
There are several methods to achieve the same result using the commands factory method but each would be larger or are going to have costs in other respects.
The two methods I've found are using Commands.defer(...) with a if statement in the command supplier and using Commands.either(...).

  • Commands.defer(...)
    • Doesn't use the ConditionalCommand class under the hood, instead using the DeferredCommand class
  • Commands.either(...)
    • This solution I'm proposing would do what commands.either() would do with its onFalse parameter set to Commands.none()

Additional context
Here is a rough idea of a potential solution (I did my best to follow the javadocs of the Commands.either method)

This assumes it's being run in the command factory class (Commands), if it's not then the this.either can be replaced with Commands.either

/**
   * Runs a command if the boolean selector function is true.
   *
   * @param selector the selector function
   * @param onTrue the command to run if the selector function returns true
   * @return the command
   * @see ConditionalCommand
   */
  public static Command runIf(BooleanSupplier selector, Command onTrue){
      return this.either(onTrue, Commands.none(), selector);
  }

This also follows the visual look of an if statement where the condition comes first then the code that'll run.

@otter502 otter502 added the type: feature Brand new functionality, features, pages, workflows, endpoints, etc. label Feb 20, 2025
@rzblue
Copy link
Member

rzblue commented Feb 20, 2025

The onlyIf decorator also provides this functionality, though I think adding a factory would be fine

@otter502
Copy link
Author

I modified the code in the associated PR to use the onlyIf decorator, thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: feature Brand new functionality, features, pages, workflows, endpoints, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants