Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qohosjstools_p.h
Go to the documentation of this file.
1// Copyright (C) 2026 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#ifndef QOHOSJSTOOLS_P_H
5#define QOHOSJSTOOLS_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtCore/private/qcore_ohos_p.h>
19#include <QtCore/private/qnapi_p.h>
20#include <QtCore/private/qohoscommon_p.h>
21#include <functional>
22#include <memory>
23#include <optional>
24#include <string>
25#include <vector>
26
27QT_BEGIN_NAMESPACE
28
29struct QOhosOnOffMethodsBasedEventHandlerOptions
30{
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;
35};
36
37Q_CORE_EXPORT std::shared_ptr<void> registerQOhosOnOffMethodsBasedEventHandler(
38 QNapi::Object eventSourceObject, const std::string &eventTypeName,
39 QNapi::CallbackFuncWrapper handler, QOhosOnOffMethodsBasedEventHandlerOptions options = {});
40
41Q_CORE_EXPORT std::shared_ptr<void> registerOhosAppContextEnvironmentCallback(
42 QOhosJsState &jsState,
43 std::vector<std::pair<std::string, QNapi::CallbackFuncWrapper>> environmentCallbackMethods);
44
45// runs code in a handle scope; a C++ exception it throws and a JS exception it leaves pending are logged under callerContextName, never propagated
46Q_CORE_EXPORT void runQOhosJsCodeLoggingExceptions(napi_env env, const char *callerContextName, const std::function<void()> &code);
47
48// Creates a Data Source: an object tracking a value owned by a JS object and mirrored
49// onto a caller-chosen target thread. The returned supplier keeps it alive and yields
50// the current value; valueChangedHandler is called on each change. Value updates and the
51// handler are dispatched onto the target thread via targetThreadExecutor. The two
52// std::function parameters provide the JS-thread-side implementation building blocks.
53template<typename T>
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 = {});
60
61template<typename T>
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)
68{
69 Q_UNUSED(callerContextName);
70
71 struct Context {
72 QOhosConsumer<T> valueChangedHandler;
73 T currentValue;
74 std::shared_ptr<void> changeListenerHandle;
75 };
76
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(
84 jsState,
85 [weakContext = QtOhos::makeWeakPtr(context), targetThreadExecutor = std::move(targetThreadExecutor)](T newValue) {
86 targetThreadExecutor(
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);
92 }
93 });
94 }));
95 return context;
96 });
97
98 return [context]() {
99 return context->currentValue;
100 };
101}
102
103QT_END_NAMESPACE
104
105#endif // QOHOSJSTOOLS_P_H
void runQOhosJsCodeLoggingExceptions(napi_env env, const char *callerContextName, const std::function< void()> &code)
std::shared_ptr< void > registerOhosAppContextEnvironmentCallback(QOhosJsState &jsState, std::vector< std::pair< std::string, QNapi::CallbackFuncWrapper > > environmentCallbackMethods)
QOhosSupplier< T > makeQOhosDataSource(std::function< T(QOhosJsState &)> initialValueReader, std::function< std::shared_ptr< void >(QOhosJsState &, QOhosConsumer< T >)> changeListenerFactory, QOhosConsumer< T > valueChangedHandler, QOhosConsumer< std::function< void()> > targetThreadExecutor, std::string callerContextName={})