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

@ -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,12 +2,14 @@
#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.
@ -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;
} }