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 <utility>
26#include <vector>
27
28QT_BEGIN_NAMESPACE
29
30struct QOhosOnOffMethodsBasedEventHandlerOptions
31{
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;
36};
37
38Q_CORE_EXPORT std::shared_ptr<void> registerQOhosOnOffMethodsBasedEventHandler(
39 QNapi::Object eventSourceObject, const std::string &eventTypeName,
40 QNapi::CallbackFuncWrapper handler, QOhosOnOffMethodsBasedEventHandlerOptions options = {});
41
42Q_CORE_EXPORT std::shared_ptr<void> registerOhosAppContextEnvironmentCallback(
43 QOhosJsState &jsState,
44 std::vector<std::pair<std::string, QNapi::CallbackFuncWrapper>> environmentCallbackMethods);
45
46// Creates a Data Source: an object tracking a value owned by a JS object and mirrored
47// onto a caller-chosen target thread. The returned supplier keeps it alive and yields
48// the current value; valueChangedHandler is called on each change. Value updates and the
49// handler are dispatched onto the target thread via targetThreadExecutor. The two
50// std::function parameters provide the JS-thread-side implementation building blocks.
51template<typename T>
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 = {});
58
59template<typename T>
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)
66{
67 Q_UNUSED(callerContextName);
68
69 struct Context {
70 QOhosConsumer<T> valueChangedHandler;
71 T currentValue;
72 std::shared_ptr<void> changeListenerHandle;
73 };
74
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(
82 jsState,
83 [weakContext = QtOhos::makeWeakPtr(context), targetThreadExecutor = std::move(targetThreadExecutor)](T newValue) {
84 targetThreadExecutor(
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);
90 }
91 });
92 }));
93 return context;
94 });
95
96 return [context]() {
97 return context->currentValue;
98 };
99}
100
101QT_END_NAMESPACE
102
103#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={})