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