14std::shared_ptr<
void> startDelayedJsThreadTask(
16 std::chrono::milliseconds delay)
21 std::optional<
int> timerId;
24 auto context =
std::make_shared<Context>();
25 context->task =
std::move(task);
27 int timerId = jsState.eval<QNapi::Number>(
28 "Global.setTimeout(*)",
30 [context](
const CallbackInfo &cbInfo) {
32 auto task = std::exchange(context->task,
nullptr);
33 context->timerId.reset();
34 task(cbInfo.jsState());
37 std::max(delay, std::chrono::milliseconds(0)).count(),
39 context->timerId = timerId;
41 return QtOhos::makeDestroyNotifier(
43 if (context->timerId.has_value()) {
45 [&](JsState &jsState) {
46 jsState.eval(
"Global.clearTimeout(*)", {context->timerId.value()});
49 context->task =
nullptr;
50 context->timerId.reset();
76std::optional<
std::uint32_t> tryGetCodeFromJsBusinessError(
const Napi::Error &error)
78 if (!error.Value().IsObject())
81 auto errorObject = QNapi::checkedCast<QNapi::Object>(error.Value());
82 auto optErrorCode = QNapi::getOptionalPropOrEmpty<QNapi::Number>(errorObject,
"code");
84 return !optErrorCode.IsEmpty()
85 ?
std::optional(optErrorCode.Uint32Value())
89void rethrowUnlessJsBusinessErrorIs(
90 const Napi::Error &error,
std::uint32_t suppressedErrorCode,
const char *callerContextName)
92 if (tryGetCodeFromJsBusinessError(error) != suppressedErrorCode)
96 "%s: ignored expected JS business error %u",
97 callerContextName, suppressedErrorCode);