Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qohosjsutils.cpp
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
#
include
"qohosjsutils.h"
5
#
include
<
QtCore
/
private
/
qcore_ohos_p
.
h
>
6
#
include
<
QtCore
/
private
/
qohoscommon_p
.
h
>
7
#
include
<
qohosutils
.
h
>
8
9
#
include
<
algorithm
>
10
11
using
namespace
std
::chrono_literals;
12
13
QT_BEGIN_NAMESPACE
14
15
namespace
QtOhos
{
16
17
std
::shared_ptr<
void
> startDelayedJsThreadTask(
18
QOhosJsState &jsState, std::function<
void
(QOhosJsState &)> task,
19
std
::chrono::milliseconds delay)
20
{
21
struct
Context
22
{
23
std::function<
void
(QOhosJsState &)> task;
24
std
::optional<
int
> timerId;
25
};
26
27
auto
context =
std
::make_shared<Context>();
28
context->task =
std
::move(task);
29
30
int
timerId = jsState.eval<QNapi::Number>(
31
"Global.setTimeout(*)"
,
32
{
33
[context](
const
QOhosCallbackInfo &cbInfo) {
34
if
(context->task) {
35
auto
task = std::exchange(context->task,
nullptr
);
36
context->timerId.reset();
37
task(cbInfo.jsState());
38
}
39
},
40
std::max(delay, std::chrono::milliseconds(0)).count(),
41
});
42
context->timerId = timerId;
43
44
return
QtOhos::makeDestroyNotifier(
45
[context]() {
46
if
(context->timerId.has_value()) {
47
QOhosJsThreadGateway::runAndWait(
48
[&](QOhosJsState &jsState) {
49
jsState.eval(
"Global.clearTimeout(*)"
, {context->timerId.value()});
50
},
51
Q_FUNC_INFO);
52
context->task =
nullptr
;
53
context->timerId.reset();
54
}
55
});
56
}
57
58
int
setJsTimeout
(
59
QOhosJsState &jsState,
std
::function<
void
(
const
QOhosCallbackInfo &)> timeoutFunc,
60
std
::chrono::milliseconds delay)
61
{
62
int
timerId = jsState.eval<QNapi::Number>(
63
"Global.setTimeout(*)"
, {std::move(timeoutFunc), delay.count()});
64
return
timerId;
65
}
66
67
void
clearJsTimeout
(QOhosJsState &jsState,
int
timerId)
68
{
69
jsState.eval(
"Global.clearTimeout(*)"
, {timerId});
70
}
71
72
QNapi
::
Promise
makeResolvedPromise
(QNapi::Value valueForResolve)
73
{
74
auto
promiseDeferred = QNapi::Promise::Deferred::New(valueForResolve.Env());
75
promiseDeferred.Resolve(valueForResolve);
76
return
promiseDeferred.Promise();
77
}
78
79
QNapi
::
Promise
makeResolvedPromise
(Napi::Env env)
80
{
81
return
makeResolvedPromise(env.Undefined());
82
}
83
84
std
::optional<
std
::
uint32_t
>
tryGetCodeFromJsBusinessError
(
const
Napi::Error &error)
85
{
86
if
(!error.Value().IsObject())
87
return
std
::nullopt;
88
89
auto
errorObject = QNapi::checkedCast<QNapi::Object>(error.Value());
90
auto
optErrorCode = QNapi::getOptionalPropOrEmpty<QNapi::Number>(errorObject,
"code"
);
91
92
return
!optErrorCode.IsEmpty()
93
?
std
::optional(optErrorCode.Uint32Value())
94
:
std
::nullopt;
95
}
96
97
void
rethrowUnlessJsBusinessErrorIsOneOf
(
98
const
Napi::Error &error,
const
std::vector<std::uint32_t> &suppressedErrorCodes,
99
const
char
*callerContextName)
100
{
101
const
auto
optErrorCode = tryGetCodeFromJsBusinessError(error);
102
if
(
std
::find(suppressedErrorCodes.begin(), suppressedErrorCodes.end(), optErrorCode) == suppressedErrorCodes.end())
103
throw
;
104
105
qOhosPrintfWarning(
106
"%s: ignored expected JS business error %u"
,
107
callerContextName, *optErrorCode);
108
}
109
110
bool
runIgnoringJsBusinessError
(
111
QOhosJsState &,
const
std::vector<std::uint32_t> &suppressedErrorCodes,
112
const
char
*callerContextName,
const
std
::function<
void
()> &action)
113
{
114
try
{
115
action();
116
return
true
;
117
}
catch
(
const
Napi::Error &error) {
118
rethrowUnlessJsBusinessErrorIsOneOf(error, suppressedErrorCodes, callerContextName);
119
return
false
;
120
}
121
}
122
123
}
124
125
QT_END_NAMESPACE
QtOhos
Definition
qcore_ohos.cpp:59
QtOhos::rethrowUnlessJsBusinessErrorIsOneOf
void rethrowUnlessJsBusinessErrorIsOneOf(const Napi::Error &error, const std::vector< std::uint32_t > &suppressedErrorCodes, const char *callerContextName)
Definition
qohosjsutils.cpp:97
QtOhos::tryGetCodeFromJsBusinessError
std::optional< std::uint32_t > tryGetCodeFromJsBusinessError(const Napi::Error &error)
Definition
qohosjsutils.cpp:84
QtOhos::makeResolvedPromise
QNapi::Promise makeResolvedPromise(QNapi::Value valueForResolve)
Definition
qohosjsutils.cpp:72
QtOhos::setJsTimeout
int setJsTimeout(QOhosJsState &jsState, std::function< void(const QOhosCallbackInfo &)> timeoutFunc, std::chrono::milliseconds delay)
Definition
qohosjsutils.cpp:58
QtOhos::runIgnoringJsBusinessError
bool runIgnoringJsBusinessError(QOhosJsState &, const std::vector< std::uint32_t > &suppressedErrorCodes, const char *callerContextName, const std::function< void()> &action)
Definition
qohosjsutils.cpp:110
QtOhos::clearJsTimeout
void clearJsTimeout(QOhosJsState &jsState, int timerId)
Definition
qohosjsutils.cpp:67
std
[33]
Definition
src_corelib_tools_qhash.cpp:421
qtbase
src
plugins
platforms
ohos
qohosjsutils.cpp
Generated on
for Qt by
1.18.0