4#include <QtCore/private/qohoscommon_p.h>
5#include <QtCore/private/qohoslogger_p.h>
12std::shared_ptr<
void> registerOnOffMethodsBasedEventHandler(
13 QNapi::Object eventSourceObject,
const std::string &eventTypeName,
14 QNapi::CallbackFuncWrapper eventHandler, OnOffMethodsBasedEventHandlerOptions options)
18 std::function<QNapi::Value(
const QOhosCallbackInfo &)> eventHandler;
19 std::function<
bool(QNapi::Object)> eventSourceAliveCheckFunc;
20 QNapi::Reference<QNapi::Value> optExtraOnArg;
21 QNapi::Reference<QNapi::Value> optExtraOffArg;
24 auto env = eventSourceObject.Env();
26 auto sharedContext = moveToSharedPtr(
28 .eventHandler = std::move(eventHandler.callbackFunc()),
29 .eventSourceAliveCheckFunc = options.optEventSourceAliveCheckFunc
30 ? std::move(options.optEventSourceAliveCheckFunc)
34 .optExtraOnArg = options.extraOnArg.has_value()
35 ? QNapi::Reference<>::makePersistentFrom(
36 options.extraOnArg.value().mapToValue(env))
37 : QNapi::Reference<>::makeEmpty(),
38 .optExtraOffArg = options.extraOffArg.has_value()
39 ? QNapi::Reference<>::makePersistentFrom(
40 options.extraOffArg.value().mapToValue(env))
41 : QNapi::Reference<>::makeEmpty(),
44 auto jsEventHandlerRef = moveToSharedPtr(
45 QNapi::Reference<>::makePersistentFrom(
47 eventSourceObject.Env(),
48 [eventTypeName, weakContext = makeWeakPtr(sharedContext)](
const QOhosCallbackInfo &cbInfo) {
49 auto sharedContext = weakContext.lock();
51 return sharedContext->eventHandler(cbInfo);
54 "%s: got unexpected '%s' event callback call for detached handler",
55 Q_FUNC_INFO, eventTypeName.c_str());
56 return cbInfo.Env().Undefined();
60 std::vector<QNapi::ValueWrapper> onCallArgs;
61 onCallArgs.push_back(eventTypeName);
62 if (!sharedContext->optExtraOnArg.IsEmpty())
63 onCallArgs.push_back(sharedContext->optExtraOnArg.Value());
64 onCallArgs.push_back(jsEventHandlerRef->Value());
65 eventSourceObject.call(
"on", onCallArgs);
67 auto eventSourceWeakRef = moveToSharedPtr(Napi::Weak(eventSourceObject));
69 return makeProxyWithJsThreadDeleter(
70 QtOhos::makeDestroyNotifier(
71 [eventSourceWeakRef, eventTypeName, sharedContext, jsEventHandlerRef]() {
72 auto eventSourceValue = eventSourceWeakRef->Value();
73 if (eventSourceValue.IsObject()) {
74 auto eventSourceObject = QNapi::checkedCast<QNapi::Object>(eventSourceValue);
75 if (sharedContext->eventSourceAliveCheckFunc(eventSourceObject)) {
77 std::vector<QNapi::ValueWrapper> offCallArgs;
78 offCallArgs.push_back(eventTypeName);
79 if (!sharedContext->optExtraOffArg.IsEmpty())
80 offCallArgs.push_back(sharedContext->optExtraOffArg.Value());
82 offCallArgs.push_back(jsEventHandlerRef->Value());
83 eventSourceObject.call(
"off", offCallArgs);
84 }
catch (
const Napi::Error &e) {
86 "%s: got exception from off(%s, ...) call (ignoring): %s",
87 Q_FUNC_INFO, eventTypeName.c_str(), e.what());
91 "%s: not calling off(%s, ...), event source 'considered' not alive",
92 Q_FUNC_INFO, eventTypeName.c_str());
96 "%s: not calling off(%s, ...), event source not alive",
97 Q_FUNC_INFO, eventTypeName.c_str());
Combined button and popup list for selecting options.