12std::shared_ptr<
void> registerQOhosOnOffMethodsBasedEventHandler(
13 QNapi::Object eventSourceObject,
const std::string &eventTypeName,
14 QNapi::CallbackFuncWrapper eventHandler, QOhosOnOffMethodsBasedEventHandlerOptions options)
18 std::function<QNapi::Value(
const QNapi::CallbackInfo &)> 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 = QtOhos::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 = QtOhos::moveToSharedPtr(
45 QNapi::Reference<>::makePersistentFrom(
47 eventSourceObject.Env(),
48 [eventTypeName, weakContext = QtOhos::makeWeakPtr(sharedContext)](
const QNapi::CallbackInfo &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 bool onCallSuccessful;
67 eventSourceObject.eval(
"on(*)", onCallArgs);
68 onCallSuccessful =
true;
69 }
catch (
const Napi::Error &error) {
70 onCallSuccessful =
false;
71 if (options.optOnCallExceptionHandler) {
72 options.optOnCallExceptionHandler(error);
78 if (!onCallSuccessful)
81 auto eventSourceWeakRef = QtOhos::moveToSharedPtr(Napi::Weak(eventSourceObject));
83 return QtOhos::makeDestroyNotifier(
84 [eventSourceWeakRef, eventTypeName, sharedContext, jsEventHandlerRef]() {
85 auto eventSourceValue = eventSourceWeakRef->Value();
86 if (eventSourceValue.IsObject()) {
87 auto eventSourceObject = QNapi::checkedCast<QNapi::Object>(eventSourceValue);
88 if (sharedContext->eventSourceAliveCheckFunc(eventSourceObject)) {
90 std::vector<QNapi::ValueWrapper> offCallArgs;
91 offCallArgs.push_back(eventTypeName);
92 if (!sharedContext->optExtraOffArg.IsEmpty())
93 offCallArgs.push_back(sharedContext->optExtraOffArg.Value());
94 offCallArgs.push_back(jsEventHandlerRef->Value());
95 eventSourceObject.eval(
"off(*)", offCallArgs);
96 }
catch (
const Napi::Error &e) {
98 "%s: got exception from off(%s, ...) call (ignoring): %s",
99 Q_FUNC_INFO, eventTypeName.c_str(), e.what());
103 "%s: not calling off(%s, ...), event source 'considered' not alive",
104 Q_FUNC_INFO, eventTypeName.c_str());
108 "%s: not calling off(%s, ...), event source not alive",
109 Q_FUNC_INFO, eventTypeName.c_str());
115 QOhosJsState &jsState,
116 std::vector<std::pair<std::string, QNapi::CallbackFuncWrapper>> environmentCallbackMethods)
118 auto optQAbility = jsState.defaultQAbility();
119 if (!optQAbility.has_value())
122 auto environmentCallback = QNapi::makeObject(
124 std::vector<std::pair<std::string, QNapi::ValueWrapper>>(
125 std::make_move_iterator(environmentCallbackMethods.begin()),
126 std::make_move_iterator(environmentCallbackMethods.end())));
128 auto appContextRefPtr = QtOhos::moveToSharedPtr(
129 QNapi::Reference<>::makePersistentFrom(
130 optQAbility->eval<QNapi::Object>(
"context.getApplicationContext()")));
132 double environmentCallbackId = appContextRefPtr->eval<QNapi::Number>(
134 {
"environment", environmentCallback});
136 return std::shared_ptr<
void>(
138 [environmentCallbackId, appContextRefPtr](
auto) {
139 QOhosJsThreadGateway::runAndWait(
140 [&](QOhosJsState &) {
141 auto appContextRef = std::move(*appContextRefPtr);
144 {
"environment", environmentCallbackId});