[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:
Sam Carlberg
2025-12-17 01:24:58 -05:00
committed by GitHub
parent 5a22abb85b
commit 7cb58962c5
7 changed files with 181 additions and 13 deletions

View File

@@ -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;
}
}
}