1*753557f6STomasz Sapeta #include "EventHandlerRegistry.h"
2*753557f6STomasz Sapeta #include "WorkletEventHandler.h"
3*753557f6STomasz Sapeta
4*753557f6STomasz Sapeta namespace ABI47_0_0reanimated {
5*753557f6STomasz Sapeta
registerEventHandler(std::shared_ptr<WorkletEventHandler> eventHandler)6*753557f6STomasz Sapeta void EventHandlerRegistry::registerEventHandler(
7*753557f6STomasz Sapeta std::shared_ptr<WorkletEventHandler> eventHandler) {
8*753557f6STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
9*753557f6STomasz Sapeta eventMappings[eventHandler->eventName][eventHandler->id] = eventHandler;
10*753557f6STomasz Sapeta eventHandlers[eventHandler->id] = eventHandler;
11*753557f6STomasz Sapeta }
12*753557f6STomasz Sapeta
unregisterEventHandler(unsigned long id)13*753557f6STomasz Sapeta void EventHandlerRegistry::unregisterEventHandler(unsigned long id) {
14*753557f6STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
15*753557f6STomasz Sapeta auto handlerIt = eventHandlers.find(id);
16*753557f6STomasz Sapeta if (handlerIt != eventHandlers.end()) {
17*753557f6STomasz Sapeta eventMappings[handlerIt->second->eventName].erase(id);
18*753557f6STomasz Sapeta if (eventMappings[handlerIt->second->eventName].empty()) {
19*753557f6STomasz Sapeta eventMappings.erase(handlerIt->second->eventName);
20*753557f6STomasz Sapeta }
21*753557f6STomasz Sapeta eventHandlers.erase(handlerIt);
22*753557f6STomasz Sapeta }
23*753557f6STomasz Sapeta }
24*753557f6STomasz Sapeta
processEvent(jsi::Runtime & rt,std::string eventName,std::string eventPayload)25*753557f6STomasz Sapeta void EventHandlerRegistry::processEvent(
26*753557f6STomasz Sapeta jsi::Runtime &rt,
27*753557f6STomasz Sapeta std::string eventName,
28*753557f6STomasz Sapeta std::string eventPayload) {
29*753557f6STomasz Sapeta std::vector<std::shared_ptr<WorkletEventHandler>> handlersForEvent;
30*753557f6STomasz Sapeta {
31*753557f6STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
32*753557f6STomasz Sapeta auto handlersIt = eventMappings.find(eventName);
33*753557f6STomasz Sapeta if (handlersIt != eventMappings.end()) {
34*753557f6STomasz Sapeta for (auto handler : handlersIt->second) {
35*753557f6STomasz Sapeta handlersForEvent.push_back(handler.second);
36*753557f6STomasz Sapeta }
37*753557f6STomasz Sapeta }
38*753557f6STomasz Sapeta }
39*753557f6STomasz Sapeta // We receive here a JS Map with JSON as a value of NativeMap key
40*753557f6STomasz Sapeta // { NativeMap: { "jsonProp": "json value" } }
41*753557f6STomasz Sapeta // So we need to extract only JSON part
42*753557f6STomasz Sapeta std::string delimimter = "NativeMap:";
43*753557f6STomasz Sapeta auto positionToSplit = eventPayload.find(delimimter) + delimimter.size();
44*753557f6STomasz Sapeta auto lastBracketCharactedPosition = eventPayload.size() - positionToSplit - 1;
45*753557f6STomasz Sapeta auto eventJSON =
46*753557f6STomasz Sapeta eventPayload.substr(positionToSplit, lastBracketCharactedPosition);
47*753557f6STomasz Sapeta
48*753557f6STomasz Sapeta if (eventJSON.compare(std::string("null")) == 0) {
49*753557f6STomasz Sapeta return;
50*753557f6STomasz Sapeta }
51*753557f6STomasz Sapeta
52*753557f6STomasz Sapeta auto eventObject = jsi::Value::createFromJsonUtf8(
53*753557f6STomasz Sapeta rt, reinterpret_cast<uint8_t *>(&eventJSON[0]), eventJSON.size());
54*753557f6STomasz Sapeta
55*753557f6STomasz Sapeta eventObject.asObject(rt).setProperty(
56*753557f6STomasz Sapeta rt, "eventName", jsi::String::createFromUtf8(rt, eventName));
57*753557f6STomasz Sapeta for (auto handler : handlersForEvent) {
58*753557f6STomasz Sapeta handler->process(rt, eventObject);
59*753557f6STomasz Sapeta }
60*753557f6STomasz Sapeta }
61*753557f6STomasz Sapeta
isAnyHandlerWaitingForEvent(std::string eventName)62*753557f6STomasz Sapeta bool EventHandlerRegistry::isAnyHandlerWaitingForEvent(std::string eventName) {
63*753557f6STomasz Sapeta const std::lock_guard<std::mutex> lock(instanceMutex);
64*753557f6STomasz Sapeta auto it = eventMappings.find(eventName);
65*753557f6STomasz Sapeta return (it != eventMappings.end()) && (!(it->second).empty());
66*753557f6STomasz Sapeta }
67*753557f6STomasz Sapeta
68*753557f6STomasz Sapeta } // namespace reanimated
69