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