From 0900fd3ceab25c47b366d6be6616b8d2deebc1f4 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Sun, 6 Jul 2025 18:42:47 -0500 Subject: [PATCH] tweak --- esphome/core/scheduler.cpp | 18 +++--------------- esphome/core/scheduler.h | 11 +++++++++++ 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/esphome/core/scheduler.cpp b/esphome/core/scheduler.cpp index 8e756c6b50..fa0f6c00f6 100644 --- a/esphome/core/scheduler.cpp +++ b/esphome/core/scheduler.cpp @@ -440,11 +440,7 @@ bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_c // Only check defer queue for timeouts (intervals never go there) if (type == SchedulerItem::TIMEOUT) { for (auto &item : this->defer_queue_) { - if (item->component != component || item->remove) { - continue; - } - const char *item_name = item->get_name(); - if (item_name != nullptr && strcmp(name_cstr, item_name) == 0) { + if (this->matches_item_(item, component, name_cstr, type)) { item->remove = true; total_cancelled++; } @@ -457,11 +453,7 @@ bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_c // Cancel items in the main heap for (auto &item : this->items_) { - if (item->component != component || item->type != type || item->remove) { - continue; - } - const char *item_name = item->get_name(); - if (item_name != nullptr && strcmp(name_cstr, item_name) == 0) { + if (this->matches_item_(item, component, name_cstr, type)) { item->remove = true; total_cancelled++; this->to_remove_++; // Track removals for heap items @@ -470,11 +462,7 @@ bool HOT Scheduler::cancel_item_locked_(Component *component, const char *name_c // Cancel items in to_add_ for (auto &item : this->to_add_) { - if (item->component != component || item->type != type || item->remove) { - continue; - } - const char *item_name = item->get_name(); - if (item_name != nullptr && strcmp(name_cstr, item_name) == 0) { + if (this->matches_item_(item, component, name_cstr, type)) { item->remove = true; total_cancelled++; // Don't track removals for to_add_ items diff --git a/esphome/core/scheduler.h b/esphome/core/scheduler.h index c154a29a91..1acf9c1d6b 100644 --- a/esphome/core/scheduler.h +++ b/esphome/core/scheduler.h @@ -147,6 +147,17 @@ class Scheduler { // Common implementation for cancel operations bool cancel_item_(Component *component, bool is_static_string, const void *name_ptr, SchedulerItem::Type type); + + // Helper function to check if item matches criteria for cancellation + bool HOT matches_item_(const std::unique_ptr &item, Component *component, const char *name_cstr, + SchedulerItem::Type type) { + if (item->component != component || item->type != type || item->remove) { + return false; + } + const char *item_name = item->get_name(); + return item_name != nullptr && strcmp(name_cstr, item_name) == 0; + } + // Helper to execute a scheduler item void execute_item_(SchedulerItem *item);