mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-15 13:08:21 +01:00
31 lines
831 B
C++
31 lines
831 B
C++
#ifndef __MEDIATOR__
|
|
#define __MEDIATOR__
|
|
|
|
#include <Arduino.h>
|
|
#include <ArduinoJson.h>
|
|
#include <list>
|
|
#include <functional>
|
|
#include <map>
|
|
#include <vector>
|
|
|
|
using namespace std;
|
|
|
|
typedef std::function<void(String msg)> subscriptionHandler_t;
|
|
|
|
class EventChannel {
|
|
public:
|
|
std::map<std::string, vector<subscriptionHandler_t>> subscriptions;
|
|
void subscribe(String topic, subscriptionHandler_t handler) {
|
|
subscriptions[topic.c_str()].reserve(1);
|
|
subscriptions[topic.c_str()].push_back(handler);
|
|
}
|
|
void publish(String topic, String msg) {
|
|
if (subscriptions.find(topic.c_str()) != subscriptions.end()){
|
|
for(subscriptionHandler_t h : subscriptions[topic.c_str()]){
|
|
h(msg);
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
#endif |