1*af2ec015STomasz Sapeta #include "EventHandlerRegistry.h"
2*af2ec015STomasz Sapeta #include "WorkletEventHandler.h"
3*af2ec015STomasz Sapeta
4*af2ec015STomasz Sapeta namespace ABI49_0_0reanimated {
5*af2ec015STomasz Sapeta
registerEventHandler(std::shared_ptr<WorkletEventHandler> eventHandler)6*af2ec015STomasz Sapeta void EventHandlerRegistry::registerEventHandler(
7*af2ec015STomasz Sapeta std::shared_ptr<WorkletEventHandler> eventHandler) {
8*af2ec015STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
9*af2ec015STomasz Sapeta eventMappings[eventHandler->eventName][eventHandler->id] = eventHandler;
10*af2ec015STomasz Sapeta eventHandlers[eventHandler->id] = eventHandler;
11*af2ec015STomasz Sapeta }
12*af2ec015STomasz Sapeta
unregisterEventHandler(uint64_t id)13*af2ec015STomasz Sapeta void EventHandlerRegistry::unregisterEventHandler(uint64_t id) {
14*af2ec015STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
15*af2ec015STomasz Sapeta auto handlerIt = eventHandlers.find(id);
16*af2ec015STomasz Sapeta if (handlerIt != eventHandlers.end()) {
17*af2ec015STomasz Sapeta eventMappings[handlerIt->second->eventName].erase(id);
18*af2ec015STomasz Sapeta if (eventMappings[handlerIt->second->eventName].empty()) {
19*af2ec015STomasz Sapeta eventMappings.erase(handlerIt->second->eventName);
20*af2ec015STomasz Sapeta }
21*af2ec015STomasz Sapeta eventHandlers.erase(handlerIt);
22*af2ec015STomasz Sapeta }
23*af2ec015STomasz Sapeta }
24*af2ec015STomasz Sapeta
processEvent(jsi::Runtime & rt,double eventTimestamp,const std::string & eventName,const jsi::Value & eventPayload)25*af2ec015STomasz Sapeta void EventHandlerRegistry::processEvent(
26*af2ec015STomasz Sapeta jsi::Runtime &rt,
27*af2ec015STomasz Sapeta double eventTimestamp,
28*af2ec015STomasz Sapeta const std::string &eventName,
29*af2ec015STomasz Sapeta const jsi::Value &eventPayload) {
30*af2ec015STomasz Sapeta std::vector<std::shared_ptr<WorkletEventHandler>> handlersForEvent;
31*af2ec015STomasz Sapeta {
32*af2ec015STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
33*af2ec015STomasz Sapeta auto handlersIt = eventMappings.find(eventName);
34*af2ec015STomasz Sapeta if (handlersIt != eventMappings.end()) {
35*af2ec015STomasz Sapeta for (auto handler : handlersIt->second) {
36*af2ec015STomasz Sapeta handlersForEvent.push_back(handler.second);
37*af2ec015STomasz Sapeta }
38*af2ec015STomasz Sapeta }
39*af2ec015STomasz Sapeta }
40*af2ec015STomasz Sapeta
41*af2ec015STomasz Sapeta eventPayload.asObject(rt).setProperty(
42*af2ec015STomasz Sapeta rt, "eventName", jsi::String::createFromUtf8(rt, eventName));
43*af2ec015STomasz Sapeta for (auto handler : handlersForEvent) {
44*af2ec015STomasz Sapeta handler->process(eventTimestamp, eventPayload);
45*af2ec015STomasz Sapeta }
46*af2ec015STomasz Sapeta }
47*af2ec015STomasz Sapeta
isAnyHandlerWaitingForEvent(const std::string & eventName)48*af2ec015STomasz Sapeta bool EventHandlerRegistry::isAnyHandlerWaitingForEvent(
49*af2ec015STomasz Sapeta const std::string &eventName) {
50*af2ec015STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
51*af2ec015STomasz Sapeta auto it = eventMappings.find(eventName);
52*af2ec015STomasz Sapeta return (it != eventMappings.end()) && (!(it->second).empty());
53*af2ec015STomasz Sapeta }
54*af2ec015STomasz Sapeta
55*af2ec015STomasz Sapeta } // namespace reanimated
56