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);
52 std::function<T(QOhosJsState &)> initialValueReader,
53 std::function<std::shared_ptr<
void>(QOhosJsState &, QOhosConsumer<T>)> changeListenerFactory,
54 QOhosConsumer<T> valueChangedHandler,
55 QOhosConsumer<std::function<
void()>> targetThreadExecutor,
56 std::string callerContextName = {});
60 std::function<T(QOhosJsState &)> initialValueReader,
61 std::function<std::shared_ptr<
void>(QOhosJsState &, QOhosConsumer<T>)> changeListenerFactory,
62 QOhosConsumer<T> valueChangedHandler,
63 QOhosConsumer<std::function<
void()>> targetThreadExecutor,
64 std::string callerContextName)
66 Q_UNUSED(callerContextName);
69 QOhosConsumer<T> valueChangedHandler;
71 std::shared_ptr<
void> changeListenerHandle;
74 auto context = QOhosJsThreadGateway::eval(
75 [&](QOhosJsState &jsState) {
76 auto context = std::make_shared<Context>();
77 context->valueChangedHandler = std::move(valueChangedHandler);
78 context->currentValue = initialValueReader(jsState);
79 context->changeListenerHandle = QtOhos::makeProxyWithJsThreadDeleter(
80 changeListenerFactory(
82 [weakContext = QtOhos::makeWeakPtr(context), targetThreadExecutor = std::move(targetThreadExecutor)](T newValue) {
84 [weakContext, newValue = std::move(newValue)]()
mutable {
85 auto context = weakContext.lock();
86 if (context && newValue != context->currentValue) {
87 context->currentValue = std::move(newValue);
88 context->valueChangedHandler(context->currentValue);
96 return context->currentValue;