mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-19 00:41:43 +00:00
[cmd3] Scope scheduled commands to the running opmode, if one exists (#8492)
This prevents commands from outliving the opmodes in which they were scheduled
This commit is contained in:
@@ -25,6 +25,14 @@ interface BindingScope {
|
||||
return new ForCommand(scheduler, command);
|
||||
}
|
||||
|
||||
static BindingScope forOpmode(long opmodeId) {
|
||||
if (opmodeId == 0) {
|
||||
throw new IllegalArgumentException("Invalid OpMode ID provided");
|
||||
}
|
||||
|
||||
return new ForOpmode(opmodeId);
|
||||
}
|
||||
|
||||
/** A global binding scope. Bindings in this scope are always active. */
|
||||
final class Global implements BindingScope {
|
||||
// No reason not to be a singleton.
|
||||
@@ -49,4 +57,16 @@ interface BindingScope {
|
||||
return scheduler.isRunning(command);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A binding scoped to a running opmode.
|
||||
*
|
||||
* @param opmodeId The ID of the opmode that the binding is scoped to.
|
||||
*/
|
||||
record ForOpmode(long opmodeId) implements BindingScope {
|
||||
@Override
|
||||
public boolean active() {
|
||||
return OpModeFetcher.getFetcher().getOpModeId() == opmodeId;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user