remove OriginID from state service

This commit is contained in:
Proddy
2024-02-12 11:22:07 +01:00
parent 65cf8005a4
commit 1f7c968d0d
20 changed files with 302 additions and 366 deletions

View File

@@ -23,8 +23,8 @@ using JsonStateUpdater = std::function<StateUpdateResult(JsonObject root, T & se
template <typename T>
using JsonStateReader = std::function<void(T & settings, JsonObject root)>;
typedef size_t update_handler_id_t;
typedef std::function<void(const String & originId)> StateUpdateCallback;
typedef size_t update_handler_id_t;
typedef std::function<void()> StateUpdateCallback;
typedef struct StateUpdateHandlerInfo {
static update_handler_id_t currentUpdatedHandlerId;
@@ -33,7 +33,7 @@ typedef struct StateUpdateHandlerInfo {
bool _allowRemove;
StateUpdateHandlerInfo(StateUpdateCallback cb, bool allowRemove)
: _id(++currentUpdatedHandlerId)
, _cb(cb)
, _cb(std::move(cb))
, _allowRemove(allowRemove){};
} StateUpdateHandlerInfo_t;
@@ -41,7 +41,7 @@ template <class T>
class StatefulService {
public:
template <typename... Args>
StatefulService(Args &&... args)
explicit StatefulService(Args &&... args)
: _state(std::forward<Args>(args)...)
, _accessMutex(xSemaphoreCreateRecursiveMutex()) {
}
@@ -50,27 +50,28 @@ class StatefulService {
if (!cb) {
return 0;
}
StateUpdateHandlerInfo_t updateHandler(cb, allowRemove);
_updateHandlers.push_back(updateHandler);
StateUpdateHandlerInfo_t updateHandler(std::move(cb), allowRemove);
_updateHandlers.push_back(std::move(updateHandler));
return updateHandler._id;
}
void removeUpdateHandler(update_handler_id_t id) {
for (auto i = _updateHandlers.begin(); i != _updateHandlers.end();) {
if ((*i)._allowRemove && (*i)._id == id) {
i = _updateHandlers.erase(i);
for (auto it = _updateHandlers.begin(); it != _updateHandlers.end();) {
auto & elem = *it;
if (elem._allowRemove && elem._id == id) {
it = _updateHandlers.erase(it);
} else {
++i;
++it;
}
}
}
StateUpdateResult update(std::function<StateUpdateResult(T &)> stateUpdater, const String & originId) {
StateUpdateResult update(std::function<StateUpdateResult(T &)> stateUpdater) {
beginTransaction();
StateUpdateResult result = stateUpdater(_state);
endTransaction();
if (result == StateUpdateResult::CHANGED) {
callUpdateHandlers(originId);
callUpdateHandlers();
}
return result;
}
@@ -82,12 +83,12 @@ class StatefulService {
return result;
}
StateUpdateResult update(JsonObject jsonObject, JsonStateUpdater<T> stateUpdater, const String & originId) {
StateUpdateResult update(JsonObject jsonObject, JsonStateUpdater<T> stateUpdater) {
beginTransaction();
StateUpdateResult result = stateUpdater(jsonObject, _state);
endTransaction();
if (result == StateUpdateResult::CHANGED) {
callUpdateHandlers(originId);
callUpdateHandlers();
}
return result;
}
@@ -111,9 +112,9 @@ class StatefulService {
endTransaction();
}
void callUpdateHandlers(const String & originId) {
void callUpdateHandlers() {
for (const StateUpdateHandlerInfo_t & updateHandler : _updateHandlers) {
updateHandler._cb(originId);
updateHandler._cb();
}
}
@@ -129,9 +130,8 @@ class StatefulService {
}
private:
SemaphoreHandle_t _accessMutex;
SemaphoreHandle_t _accessMutex;
std::vector<StateUpdateHandlerInfo_t> _updateHandlers;
// std::list<StateUpdateHandlerInfo_t> _updateHandlers;
};
#endif