From 0a4aad9eb0dbdceb10ef2d8f1740d012c3f63ace Mon Sep 17 00:00:00 2001 From: Rapsssito Date: Sat, 13 Jul 2024 10:52:19 +0200 Subject: [PATCH] Fix linting issues --- .../esp32_improv/esp32_improv_component.cpp | 2 +- esphome/core/event_emitter.cpp | 12 ++++++++++++ esphome/core/event_emitter.h | 9 +++++---- 3 files changed, 18 insertions(+), 5 deletions(-) create mode 100644 esphome/core/event_emitter.cpp diff --git a/esphome/components/esp32_improv/esp32_improv_component.cpp b/esphome/components/esp32_improv/esp32_improv_component.cpp index e35a441957..d0239916d8 100644 --- a/esphome/components/esp32_improv/esp32_improv_component.cpp +++ b/esphome/components/esp32_improv/esp32_improv_component.cpp @@ -45,7 +45,7 @@ void ESP32ImprovComponent::setup_characteristics() { if (!data.empty()) { this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end()); } - }); + }); BLEDescriptor *rpc_descriptor = new BLE2902(); this->rpc_->add_descriptor(rpc_descriptor); diff --git a/esphome/core/event_emitter.cpp b/esphome/core/event_emitter.cpp new file mode 100644 index 0000000000..59c3947226 --- /dev/null +++ b/esphome/core/event_emitter.cpp @@ -0,0 +1,12 @@ +#include "event_emitter.h" + +namespace esphome { + +static const char *const TAG = "event_emitter"; + +void RaiseEventEmitterFullError(EventEmitterListenerID id) { + ESP_LOGE(TAG, "EventEmitter has reached the maximum number of listeners for event"); + ESP_LOGW(TAG, "Removing listener with ID %d to make space for new listener", id); +} + +} // namespace esphome diff --git a/esphome/core/event_emitter.h b/esphome/core/event_emitter.h index 5da9ab888b..d1752c2d13 100644 --- a/esphome/core/event_emitter.h +++ b/esphome/core/event_emitter.h @@ -2,16 +2,18 @@ #include #include #include +#include #include "esphome/core/log.h" namespace esphome { using EventEmitterListenerID = uint32_t; +void RaiseEventEmitterFullError(EventEmitterListenerID id); // EventEmitter class that can emit events with a specific name (it is highly recommended to use an enum class for this) and a list of arguments. // Supports multiple listeners for each event. -template class EventEmitter { +template class EventEmitter { public: EventEmitterListenerID on(EvtType event, std::function listener) { EventEmitterListenerID listener_id = get_next_id(event); @@ -38,8 +40,7 @@ template class EventEmitter { // Check if the map is full if (listeners_[event].size() == std::numeric_limits::max()) { // Raise an error if the map is full - ESP_LOGE("event_emitter", "EventEmitter has reached the maximum number of listeners for event %d", event); - ESP_LOGW("event_emitter", "Removing listener with ID %d for event %d", 0, event); + RaiseEventEmitterFullError(0); off(event, 0); return 0; } @@ -51,7 +52,7 @@ template class EventEmitter { current_id_ = next_id; return current_id_; } - + private: std::unordered_map>> listeners_; EventEmitterListenerID current_id_ = 0;