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.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
11using namespace std::chrono_literals;
12
13QT_BEGIN_NAMESPACE
14
15namespace QtOhos {
16
17std::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
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
67void clearJsTimeout(QOhosJsState &jsState, int timerId)
68{
69 jsState.eval("Global.clearTimeout(*)", {timerId});
70}
71
72QNapi::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
80{
81 return makeResolvedPromise(env.Undefined());
82}
83
84std::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
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
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
125QT_END_NAMESPACE
void rethrowUnlessJsBusinessErrorIsOneOf(const Napi::Error &error, const std::vector< std::uint32_t > &suppressedErrorCodes, const char *callerContextName)
std::optional< std::uint32_t > tryGetCodeFromJsBusinessError(const Napi::Error &error)
QNapi::Promise makeResolvedPromise(QNapi::Value valueForResolve)
int setJsTimeout(QOhosJsState &jsState, std::function< void(const QOhosCallbackInfo &)> timeoutFunc, std::chrono::milliseconds delay)
bool runIgnoringJsBusinessError(QOhosJsState &, const std::vector< std::uint32_t > &suppressedErrorCodes, const char *callerContextName, const std::function< void()> &action)
void clearJsTimeout(QOhosJsState &jsState, int timerId)