Skip to content

Commit

Permalink
Scheduler.unbind by ID
Browse files Browse the repository at this point in the history
  • Loading branch information
bubner committed Dec 15, 2024
1 parent 1bd8dcf commit 44c3ec9
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,28 @@ public abstract class CommandBasedBunyipsOpMode extends BunyipsOpMode {
@NonNull
private HashSet<BunyipsSubsystem> managedSubsystems = new HashSet<>();

/**
* Unbind a task from the scheduler, based on the index of the task in the scheduler's allocated tasks.
* <p>
* This can either be determined by the order in which the tasks were bound, or by the ID of the task via
* the {@code ScheduledTask.id} property, which is the same thing.
*
* @param index The index of the task to unbind.
* @throws IndexOutOfBoundsException If the index is out of bounds.
*/
public void unbind(int index) {
scheduler.unbind(index);
}

/**
* Unbind a scheduled task from the scheduler.
*
* @param task The {@link Scheduler.ScheduledTask} to unbind.
*/
public void unbind(@NonNull Scheduler.ScheduledTask task) {
scheduler.unbind(task);
}

/**
* Create a new controller trigger creator.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,28 @@ class Scheduler : BunyipsComponent() {
}
}

/**
* Unbind a task from the scheduler, based on the index of the task in the scheduler's allocated tasks.
*
* This can either be determined by the order in which the tasks were bound, or by the ID of the task via
* the [ScheduledTask.id] property, which is the same thing.
*
* @param index The index of the task to unbind.
* @throws IndexOutOfBoundsException If the index is out of bounds.
*/
fun unbind(index: Int) {
allocatedTasks.removeAt(index)
}

/**
* Unbind a scheduled task from the scheduler.
*
* @param task The [ScheduledTask] to unbind.
*/
fun unbind(task: ScheduledTask) {
allocatedTasks.remove(task)
}

/**
* Create a new controller trigger creator.
*
Expand Down Expand Up @@ -470,7 +492,18 @@ class Scheduler : BunyipsComponent() {
* A task that will run when a condition is met.
*/
inner class ScheduledTask(private val originalRunCondition: Condition) {
/**
* The ID (allocated task index) of the task that can be used to unbind and identify the binding.
*/
@JvmField
val id: Int

/**
* The task to run when the condition is met.
*/
var taskToRun: Task = IdleTask()
private set

internal val runCondition: () -> Boolean
internal var debouncing: Boolean = false
internal var stopCondition: (() -> Boolean)? = null
Expand All @@ -488,6 +521,7 @@ class Scheduler : BunyipsComponent() {
|| or.stream().anyMatch { obj -> obj.asBoolean }
}
allocatedTasks.add(this)
id = allocatedTasks.size - 1
}

/**
Expand Down

0 comments on commit 44c3ec9

Please sign in to comment.