mirror of
https://github.com/esphome/esphome.git
synced 2024-11-28 09:44:12 +01:00
Fix linting issues
This commit is contained in:
parent
4be588c749
commit
0a4aad9eb0
3 changed files with 18 additions and 5 deletions
12
esphome/core/event_emitter.cpp
Normal file
12
esphome/core/event_emitter.cpp
Normal 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
|
|
@ -2,16 +2,18 @@
|
|||
#include <unordered_map>
|
||||
#include <vector>
|
||||
#include <functional>
|
||||
#include <limits>
|
||||
|
||||
#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 <typename EvtType, typename... Args> class EventEmitter {
|
||||
template<typename EvtType, typename... Args> class EventEmitter {
|
||||
public:
|
||||
EventEmitterListenerID on(EvtType event, std::function<void(Args...)> listener) {
|
||||
EventEmitterListenerID listener_id = get_next_id(event);
|
||||
|
@ -38,8 +40,7 @@ template <typename EvtType, typename... Args> class EventEmitter {
|
|||
// Check if the map is full
|
||||
if (listeners_[event].size() == std::numeric_limits<EventEmitterListenerID>::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;
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue