4#ifndef QOHOSJSTOOLS_P_H
5#define QOHOSJSTOOLS_P_H
18#include <QtCore/private/qcore_ohos_p.h>
19#include <QtCore/private/qnapi_p.h>
20#include <QtCore/private/qohoscommon_p.h>
29struct QOhosOnOffMethodsBasedEventHandlerOptions
31 std::function<
bool(QNapi::Object)> optEventSourceAliveCheckFunc;
32 std::optional<QNapi::ValueWrapper> extraOnArg;
33 std::optional<QNapi::ValueWrapper> extraOffArg;
34 std::function<
void(
const Napi::Error &)> optOnCallExceptionHandler;
37Q_CORE_EXPORT std::shared_ptr<
void> registerQOhosOnOffMethodsBasedEventHandler(
38 QNapi::Object eventSourceObject,
const std::string &eventTypeName,
39 QNapi::CallbackFuncWrapper handler, QOhosOnOffMethodsBasedEventHandlerOptions options = {});
41Q_CORE_EXPORT std::shared_ptr<
void> registerOhosAppContextEnvironmentCallback(
42 QOhosJsState &jsState,
43 std::vector<std::pair<std::string, QNapi::CallbackFuncWrapper>> environmentCallbackMethods);
55 std::function<T(QOhosJsState &)> initialValueReader,
56 std::function<std::shared_ptr<
void>(QOhosJsState &, QOhosConsumer<T>)> changeListenerFactory,
57 QOhosConsumer<T> valueChangedHandler,
58 QOhosConsumer<std::function<
void()>> targetThreadExecutor,
59 std::string callerContextName = {});
63 std::function<T(QOhosJsState &)> initialValueReader,
64 std::function<std::shared_ptr<
void>(QOhosJsState &, QOhosConsumer<T>)> changeListenerFactory,
65 QOhosConsumer<T> valueChangedHandler,
66 QOhosConsumer<std::function<
void()>> targetThreadExecutor,
67 std::string callerContextName)
69 Q_UNUSED(callerContextName);
72 QOhosConsumer<T> valueChangedHandler;
74 std::shared_ptr<
void> changeListenerHandle;
77 auto context = QOhosJsThreadGateway::eval(
78 [&](QOhosJsState &jsState) {
79 auto context = std::make_shared<Context>();
80 context->valueChangedHandler = std::move(valueChangedHandler);
81 context->currentValue = initialValueReader(jsState);
82 context->changeListenerHandle = QtOhos::makeProxyWithJsThreadDeleter(
83 changeListenerFactory(
85 [weakContext = QtOhos::makeWeakPtr(context), targetThreadExecutor = std::move(targetThreadExecutor)](T newValue) {
87 [weakContext, newValue = std::move(newValue)]()
mutable {
88 auto context = weakContext.lock();
89 if (context && newValue != context->currentValue) {
90 context->currentValue = std::move(newValue);
91 context->valueChangedHandler(context->currentValue);
99 return context->currentValue;