1
0
mirror of https://github.com/esphome/esphome.git synced 2025-11-17 15:26:01 +00:00

preserve order

This commit is contained in:
J. Nick Koston
2025-11-09 23:10:06 -06:00
parent 4c3931363f
commit 6feaa8dd13

View File

@@ -889,10 +889,11 @@ template<typename... Ts> class PartitionedCallbackManager<void(Ts...)> {
this->callbacks_ = make_unique<std::vector<std::function<void(Ts...)>>>(); this->callbacks_ = make_unique<std::vector<std::function<void(Ts...)>>>();
} }
// Add to first partition: append then swap into position // Add to first partition: append then rotate into position
this->callbacks_->push_back(std::move(callback)); this->callbacks_->push_back(std::move(callback));
if (*first_count < this->callbacks_->size() - 1) { if (*first_count < this->callbacks_->size() - 1) {
std::swap((*this->callbacks_)[*first_count], (*this->callbacks_)[this->callbacks_->size() - 1]); // Use std::rotate to maintain registration order in second partition
std::rotate(this->callbacks_->begin() + *first_count, this->callbacks_->end() - 1, this->callbacks_->end());
} }
(*first_count)++; (*first_count)++;
} }