mirror of
https://gitlab.com/wirelos/sprocket-lib.git
synced 2025-12-17 21:56:40 +01:00
rename mediator to EventChannel as it was named wrongly
This commit is contained in:
31
src/EventChannel.h
Normal file
31
src/EventChannel.h
Normal file
@@ -0,0 +1,31 @@
|
||||
#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
|
||||
Reference in New Issue
Block a user