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
qohosjsutils.h
Go to the documentation of this file.
1// Copyright (C) 2025 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 QOHOSJSUTILS_H
5#define QOHOSJSUTILS_H
6
7#include <QtCore/private/qnapi_p.h>
8#include <QtCore/private/qohoscommon_p.h>
9#include <QtCore/private/qohosjstools_p.h>
10#include <QtCore/qglobal.h>
11#include <chrono>
12#include <cstdint>
13#include <functional>
14#include <memory>
15#include <optional>
16#include <qohosplugincore.h>
17#include <qohosutils.h>
18#include <string>
19#include <type_traits>
20
22
24
25namespace QtOhos {
26
27template<typename T>
29 JsState &jsState, std::function<QNapi::Value(JsState &, T)> promiseValueFactory,
30 const QOhosConsumer<JsState &, QOhosConsumer<JsState &, T>> &asyncCallFunc);
31
35
36int setJsTimeout(
37 JsState &jsState, std::function<void(const CallbackInfo &)> timeoutFunc,
38 std::chrono::milliseconds delay);
39
40void clearJsTimeout(JsState &jsState, int timerId);
41
42QNapi::Promise makeResolvedPromise(QNapi::Value valueForResolve);
43
44template<typename T>
45std::optional<T> getOptionalProperty(const QNapi::Object &object, const std::string &propName);
46
47std::optional<std::uint32_t> tryGetCodeFromJsBusinessError(const Napi::Error &error);
48
49// On JS business error matching suppressedErrorCode warns and returns, otherwise rethrows.
50// Call from a catch block.
52 const Napi::Error &error, std::uint32_t suppressedErrorCode, const char *callerContextName);
53
54// Runs action; on JS business error matching suppressedErrorCode warns and returns, otherwise rethrows.
55// Returns true if action completed, false if error was swallowed.
57 JsState &, std::uint32_t suppressedErrorCode, const char *callerContextName,
58 const std::function<void()> &action);
59
60template<typename T>
62 std::function<T(JsState &)> initialValueReader,
63 std::function<std::shared_ptr<void>(JsState &, QOhosConsumer<T>)> changeListenerFactory,
64 QOhosConsumer<T> valueChangedHandler,
65 QOhosConsumer<std::function<void()>> targetThreadExecutor,
66 std::string callerContextName = {});
67
68template<typename T>
70 JsState &jsState, std::function<QNapi::Value(JsState &, T)> promiseValueFactory,
71 const QOhosConsumer<JsState &, QOhosConsumer<JsState &, T>> &asyncCallFunc)
72{
73 auto promiseDeferred = QNapi::Promise::Deferred::New(jsState.env());
74
75 asyncCallFunc(
76 jsState,
77 makeCallOnceConsumerWrapper<JsState &, T>(
78 [promiseDeferred, promiseValueFactory = std::move(promiseValueFactory)](JsState &jsState, T result) {
79 promiseDeferred.Resolve(promiseValueFactory(jsState, result));
80 }));
81
82 return promiseDeferred.Promise();
83}
84
85template<typename T>
86std::optional<T> getOptionalProperty(const QNapi::Object &object, const std::string &propName)
87{
88 auto optPropValue = QNapi::getOptionalPropOrEmpty<T>(object, propName);
89 return !optPropValue.IsEmpty()
90 ? std::optional(optPropValue)
91 : std::nullopt;
92}
93
94template<typename T>
96 std::function<T(JsState &)> initialValueReader,
97 std::function<std::shared_ptr<void>(JsState &, QOhosConsumer<T>)> changeListenerFactory,
98 QOhosConsumer<T> valueChangedHandler,
99 QOhosConsumer<std::function<void()>> targetThreadExecutor,
100 std::string callerContextName)
101{
102 return makeQOhosDataSource<T>(
103 [initialValueReader = std::move(initialValueReader)](QOhosJsState &jsState) {
104 return initialValueReader(static_cast<JsState &>(jsState));
105 },
106 [changeListenerFactory = std::move(changeListenerFactory)](
107 QOhosJsState &jsState, QOhosConsumer<T> valueUpdatesConsumer) {
108 return changeListenerFactory(static_cast<JsState &>(jsState), std::move(valueUpdatesConsumer));
109 },
110 std::move(valueChangedHandler),
111 std::move(targetThreadExecutor),
112 std::move(callerContextName));
113}
114
115template<typename ConfigValue>
117 std::function<ConfigValue(QtOhos::JsState &)> initValueSupplier,
118 std::function<ConfigValue(QtOhos::JsState &, const QNapi::Object &)> valueFetcher,
119 QOhosConsumer<ConfigValue> valueChangedHandler)
120{
121 return QtOhos::makeDataSource<ConfigValue>(
122 std::move(initValueSupplier),
123 [valueFetcher = std::move(valueFetcher)](QtOhos::JsState &jsState, QOhosConsumer<ConfigValue> valueUpdatesConsumer) mutable {
124 return registerOhosAppContextEnvironmentCallback(
125 jsState,
126 {
127 {
128 "onConfigurationUpdated",
129 [valueFetcher = std::move(valueFetcher), valueUpdatesConsumer = std::move(valueUpdatesConsumer)](const QtOhos::CallbackInfo &cbInfo) {
130 auto config = cbInfo.getFirstArg<QNapi::Object>(Q_FUNC_INFO);
131 valueUpdatesConsumer(valueFetcher(cbInfo.jsState(), config));
132 }
133 },
134 });
135 },
136 std::move(valueChangedHandler),
137 QtOhos::invokeInQtThread,
138 Q_FUNC_INFO);
139}
140
141}
142
143QT_END_NAMESPACE
144
145#endif // QOHOSJSUTILS_H
std::vector< QOhosDisplayInfo > getRegisteredDisplayInfos()
static std::shared_ptr< QOhosDisplayManager > create(QtOhos::JsState &jsState, CreateInfo createInfo)
QOhosDisplayInfo::JsDisplayId JsDisplayId
JsState & jsState() const
QPoint mapFromDisplayToGlobal(const QPoint &displayOffset, QOhosDisplayInfo::JsDisplayId jsDisplayId)
Combined button and popup list for selecting options.
void clearJsTimeout(JsState &jsState, int timerId)
std::optional< std::uint32_t > tryGetCodeFromJsBusinessError(const Napi::Error &error)
int setJsTimeout(JsState &jsState, std::function< void(const CallbackInfo &)> timeoutFunc, std::chrono::milliseconds delay)
bool runIgnoringJsBusinessError(JsState &, std::uint32_t suppressedErrorCode, const char *callerContextName, const std::function< void()> &action)
QNapi::Promise adaptAsyncCallResultToJsPromise(JsState &jsState, std::function< QNapi::Value(JsState &, T)> promiseValueFactory, const QOhosConsumer< JsState &, QOhosConsumer< JsState &, T > > &asyncCallFunc)
std::optional< T > getOptionalProperty(const QNapi::Object &object, const std::string &propName)
QNapi::Promise makeResolvedPromise(QNapi::Value valueForResolve)
QOhosSupplier< T > makeDataSource(std::function< T(JsState &)> initialValueReader, std::function< std::shared_ptr< void >(JsState &, QOhosConsumer< T >)> changeListenerFactory, QOhosConsumer< T > valueChangedHandler, QOhosConsumer< std::function< void()> > targetThreadExecutor, std::string callerContextName={})
void rethrowUnlessJsBusinessErrorIs(const Napi::Error &error, std::uint32_t suppressedErrorCode, const char *callerContextName)
QOhosSupplier< ConfigValue > makeOhosConfigValueDataSource(std::function< ConfigValue(QtOhos::JsState &)> initValueSupplier, std::function< ConfigValue(QtOhos::JsState &, const QNapi::Object &)> valueFetcher, QOhosConsumer< ConfigValue > valueChangedHandler)
QOhosConsumer< QtOhos::JsState &, JsDisplayId, QRectF > displayAvailableAreaChangedCb
QOhosConsumer< QtOhos::JsState &, std::vector< QOhosDisplayInfo > > displaysUpdatedCb