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// Creates a Data Source: an object tracking a value owned by a JS object and mirrored
46// onto a caller-chosen target thread. The returned supplier keeps it alive and yields
47// the current value; valueChangedHandler is called on each change. Value updates and the
48// handler are dispatched onto the target thread via targetThreadExecutor. The two
49// std::function parameters provide the JS-thread-side implementation building blocks.
50template<typename T>
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 = {});
57
58template<typename T>
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)
65{
66 Q_UNUSED(callerContextName);
67
68 struct Context {
69 QOhosConsumer<T> valueChangedHandler;
70 T currentValue;
71 std::shared_ptr<void> changeListenerHandle;
72 };
73
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(
81 jsState,
82 [weakContext = QtOhos::makeWeakPtr(context), targetThreadExecutor = std::move(targetThreadExecutor)](T newValue) {
83 targetThreadExecutor(
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);
89 }
90 });
91 }));
92 return context;
93 });
94
95 return [context]() {
96 return context->currentValue;
97 };
98}
99
100QT_END_NAMESPACE
101
102#endif // QOHOSJSTOOLS_P_H
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={})