[commands] Revert to original Trigger implementation (#4673)

Trigger was refactored to use BooleanEvent when it was introduced in #4104.
This reverts to the original implementation until edge-based BooleanEvents can be fixed.
This commit is contained in:
Starlight220
2022-11-28 23:48:48 +02:00
committed by GitHub
parent 15561338d5
commit cb38bacfe8
14 changed files with 555 additions and 243 deletions

View File

@@ -54,7 +54,12 @@ public class BooleanEvent implements BooleanSupplier {
* @param action the action to run if this event is active.
*/
public final void ifHigh(Runnable action) {
m_loop.bind(m_signal, action);
m_loop.bind(
() -> {
if (m_signal.getAsBoolean()) {
action.run();
}
});
}
/**

View File

@@ -6,45 +6,27 @@ package edu.wpi.first.wpilibj.event;
import java.util.Collection;
import java.util.LinkedHashSet;
import java.util.function.BooleanSupplier;
/** The loop polling {@link BooleanEvent} objects and executing the actions bound to them. */
public final class EventLoop {
private final Collection<Binding> m_bindings = new LinkedHashSet<>();
private final Collection<Runnable> m_bindings = new LinkedHashSet<>();
/**
* Bind a new action to run whenever the condition is true.
*
* @param condition the condition to listen to.
* @param action the action to run.
*/
public void bind(BooleanSupplier condition, Runnable action) {
m_bindings.add(new Binding(condition, action));
public void bind(Runnable action) {
m_bindings.add(action);
}
/** Poll all bindings. */
public void poll() {
m_bindings.forEach(Binding::poll);
m_bindings.forEach(Runnable::run);
}
/** Clear all bindings. */
public void clear() {
m_bindings.clear();
}
private static class Binding {
private final BooleanSupplier m_condition;
private final Runnable m_action;
private Binding(BooleanSupplier condition, Runnable action) {
this.m_condition = condition;
this.m_action = action;
}
void poll() {
if (m_condition.getAsBoolean()) {
m_action.run();
}
}
}
}