13std::shared_ptr<
void> registerQOhosOnOffMethodsBasedEventHandler(
14 QNapi::Object eventSourceObject,
const std::string &eventTypeName,
15 QNapi::CallbackFuncWrapper eventHandler, QOhosOnOffMethodsBasedEventHandlerOptions options)
19 std::function<QNapi::Value(
const QNapi::CallbackInfo &)> eventHandler;
20 std::function<
bool(QNapi::Object)> eventSourceAliveCheckFunc;
21 QNapi::Reference<QNapi::Value> optExtraOnArg;
22 QNapi::Reference<QNapi::Value> optExtraOffArg;
25 auto env = eventSourceObject.Env();
27 auto sharedContext = QtOhos::moveToSharedPtr(
29 .eventHandler = std::move(eventHandler.callbackFunc()),
30 .eventSourceAliveCheckFunc = options.optEventSourceAliveCheckFunc
31 ? std::move(options.optEventSourceAliveCheckFunc)
35 .optExtraOnArg = options.extraOnArg.has_value()
36 ? QNapi::Reference<>::makePersistentFrom(
37 options.extraOnArg.value().mapToValue(env))
38 : QNapi::Reference<>::makeEmpty(),
39 .optExtraOffArg = options.extraOffArg.has_value()
40 ? QNapi::Reference<>::makePersistentFrom(
41 options.extraOffArg.value().mapToValue(env))
42 : QNapi::Reference<>::makeEmpty(),
45 auto jsEventHandlerRef = QtOhos::moveToSharedPtr(
46 QNapi::Reference<>::makePersistentFrom(
48 eventSourceObject.Env(),
49 [eventTypeName, weakContext = QtOhos::makeWeakPtr(sharedContext)](
const QNapi::CallbackInfo &cbInfo) {
50 auto sharedContext = weakContext.lock();
52 return sharedContext->eventHandler(cbInfo);
55 "%s: got unexpected '%s' event callback call for detached handler",
56 Q_FUNC_INFO, eventTypeName.c_str());
57 return cbInfo.Env().Undefined();
61 std::vector<QNapi::ValueWrapper> onCallArgs;
62 onCallArgs.push_back(eventTypeName);
63 if (!sharedContext->optExtraOnArg.IsEmpty())
64 onCallArgs.push_back(sharedContext->optExtraOnArg.Value());
65 onCallArgs.push_back(jsEventHandlerRef->Value());
66 bool onCallSuccessful;
68 eventSourceObject.eval(
"on(*)", onCallArgs);
69 onCallSuccessful =
true;
70 }
catch (
const Napi::Error &error) {
71 onCallSuccessful =
false;
72 if (options.optOnCallExceptionHandler) {
73 options.optOnCallExceptionHandler(error);
79 if (!onCallSuccessful)
82 auto eventSourceWeakRef = QtOhos::moveToSharedPtr(Napi::Weak(eventSourceObject));
84 return QtOhos::makeDestroyNotifier(
85 [eventSourceWeakRef, eventTypeName, sharedContext, jsEventHandlerRef]() {
86 auto eventSourceValue = eventSourceWeakRef->Value();
87 if (eventSourceValue.IsObject()) {
88 auto eventSourceObject = QNapi::checkedCast<QNapi::Object>(eventSourceValue);
89 if (sharedContext->eventSourceAliveCheckFunc(eventSourceObject)) {
91 std::vector<QNapi::ValueWrapper> offCallArgs;
92 offCallArgs.push_back(eventTypeName);
93 if (!sharedContext->optExtraOffArg.IsEmpty())
94 offCallArgs.push_back(sharedContext->optExtraOffArg.Value());
95 offCallArgs.push_back(jsEventHandlerRef->Value());
96 eventSourceObject.eval(
"off(*)", offCallArgs);
97 }
catch (
const Napi::Error &e) {
99 "%s: got exception from off(%s, ...) call (ignoring): %s",
100 Q_FUNC_INFO, eventTypeName.c_str(), e.what());
104 "%s: not calling off(%s, ...), event source 'considered' not alive",
105 Q_FUNC_INFO, eventTypeName.c_str());
109 "%s: not calling off(%s, ...), event source not alive",
110 Q_FUNC_INFO, eventTypeName.c_str());
116 QOhosJsState &jsState,
117 std::vector<std::pair<std::string, QNapi::CallbackFuncWrapper>> environmentCallbackMethods)
119 auto optQAbility = jsState.defaultQAbility();
120 if (!optQAbility.has_value())
123 auto environmentCallback = QNapi::makeObject(
125 std::vector<std::pair<std::string, QNapi::ValueWrapper>>(
126 std::make_move_iterator(environmentCallbackMethods.begin()),
127 std::make_move_iterator(environmentCallbackMethods.end())));
129 auto appContextRefPtr = QtOhos::moveToSharedPtr(
130 QNapi::Reference<>::makePersistentFrom(
131 optQAbility->eval<QNapi::Object>(
"context.getApplicationContext()")));
133 double environmentCallbackId = appContextRefPtr->eval<QNapi::Number>(
135 {
"environment", environmentCallback});
137 return std::shared_ptr<
void>(
139 [environmentCallbackId, appContextRefPtr](
auto) {
140 QOhosJsThreadGateway::runAndWait(
141 [&](QOhosJsState &) {
142 auto appContextRef = std::move(*appContextRefPtr);
145 {
"environment", environmentCallbackId});