Fix linting issues

This commit is contained in:
Rapsssito 2024-07-13 10:52:19 +02:00
parent 4be588c749
commit 0a4aad9eb0
3 changed files with 18 additions and 5 deletions

View file

@ -45,7 +45,7 @@ void ESP32ImprovComponent::setup_characteristics() {
if (!data.empty()) { if (!data.empty()) {
this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end()); this->incoming_data_.insert(this->incoming_data_.end(), data.begin(), data.end());
} }
}); });
BLEDescriptor *rpc_descriptor = new BLE2902(); BLEDescriptor *rpc_descriptor = new BLE2902();
this->rpc_->add_descriptor(rpc_descriptor); this->rpc_->add_descriptor(rpc_descriptor);

View file

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

View file

@ -2,16 +2,18 @@
#include <unordered_map> #include <unordered_map>
#include <vector> #include <vector>
#include <functional> #include <functional>
#include <limits>
#include "esphome/core/log.h" #include "esphome/core/log.h"
namespace esphome { namespace esphome {
using EventEmitterListenerID = uint32_t; 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. // 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. // Supports multiple listeners for each event.
template <typename EvtType, typename... Args> class EventEmitter { template<typename EvtType, typename... Args> class EventEmitter {
public: public:
EventEmitterListenerID on(EvtType event, std::function<void(Args...)> listener) { EventEmitterListenerID on(EvtType event, std::function<void(Args...)> listener) {
EventEmitterListenerID listener_id = get_next_id(event); EventEmitterListenerID listener_id = get_next_id(event);
@ -38,8 +40,7 @@ template <typename EvtType, typename... Args> class EventEmitter {
// Check if the map is full // Check if the map is full
if (listeners_[event].size() == std::numeric_limits<EventEmitterListenerID>::max()) { if (listeners_[event].size() == std::numeric_limits<EventEmitterListenerID>::max()) {
// Raise an error if the map is full // Raise an error if the map is full
ESP_LOGE("event_emitter", "EventEmitter has reached the maximum number of listeners for event %d", event); RaiseEventEmitterFullError(0);
ESP_LOGW("event_emitter", "Removing listener with ID %d for event %d", 0, event);
off(event, 0); off(event, 0);
return 0; return 0;
} }
@ -51,7 +52,7 @@ template <typename EvtType, typename... Args> class EventEmitter {
current_id_ = next_id; current_id_ = next_id;
return current_id_; return current_id_;
} }
private: private:
std::unordered_map<EvtType, std::unordered_map<EventEmitterListenerID, std::function<void(Args...)>>> listeners_; std::unordered_map<EvtType, std::unordered_map<EventEmitterListenerID, std::function<void(Args...)>>> listeners_;
EventEmitterListenerID current_id_ = 0; EventEmitterListenerID current_id_ = 0;