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>
30struct QOhosOnOffMethodsBasedEventHandlerOptions
32 std::function<
bool(QNapi::Object)> optEventSourceAliveCheckFunc;
33 std::optional<QNapi::ValueWrapper> extraOnArg;
34 std::optional<QNapi::ValueWrapper> extraOffArg;
35 std::function<
void(
const Napi::Error &)> optOnCallExceptionHandler;
38Q_CORE_EXPORT std::shared_ptr<
void> registerQOhosOnOffMethodsBasedEventHandler(
39 QNapi::Object eventSourceObject,
const std::string &eventTypeName,
40 QNapi::CallbackFuncWrapper handler, QOhosOnOffMethodsBasedEventHandlerOptions options = {});
42Q_CORE_EXPORT std::shared_ptr<
void> registerOhosAppContextEnvironmentCallback(
43 QOhosJsState &jsState,
44 std::vector<std::pair<std::string, QNapi::CallbackFuncWrapper>> environmentCallbackMethods);
53 std::function<T(QOhosJsState &)> initialValueReader,
54 std::function<std::shared_ptr<
void>(QOhosJsState &, QOhosConsumer<T>)> changeListenerFactory,
55 QOhosConsumer<T> valueChangedHandler,
56 QOhosConsumer<std::function<
void()>> targetThreadExecutor,
57 std::string callerContextName = {});
61 std::function<T(QOhosJsState &)> initialValueReader,
62 std::function<std::shared_ptr<
void>(QOhosJsState &, QOhosConsumer<T>)> changeListenerFactory,
63 QOhosConsumer<T> valueChangedHandler,
64 QOhosConsumer<std::function<
void()>> targetThreadExecutor,
65 std::string callerContextName)
67 Q_UNUSED(callerContextName);
70 QOhosConsumer<T> valueChangedHandler;
72 std::shared_ptr<
void> changeListenerHandle;
75 auto context = QOhosJsThreadGateway::eval(
76 [&](QOhosJsState &jsState) {
77 auto context = std::make_shared<Context>();
78 context->valueChangedHandler = std::move(valueChangedHandler);
79 context->currentValue = initialValueReader(jsState);
80 context->changeListenerHandle = QtOhos::makeProxyWithJsThreadDeleter(
81 changeListenerFactory(
83 [weakContext = QtOhos::makeWeakPtr(context), targetThreadExecutor = std::move(targetThreadExecutor)](T newValue) {
85 [weakContext, newValue = std::move(newValue)]()
mutable {
86 auto context = weakContext.lock();
87 if (context && newValue != context->currentValue) {
88 context->currentValue = std::move(newValue);
89 context->valueChangedHandler(context->currentValue);
97 return context->currentValue;