15std::shared_ptr<
void> startDelayedJsThreadTask(
17 std::chrono::milliseconds delay)
22 QOhosOptional<
int> timerId;
25 auto context =
std::make_shared<Context>();
26 context->task =
std::move(task);
28 int timerId = jsState.eval<QNapi::Number>(
29 "Global.setTimeout(*)",
31 [context](
const CallbackInfo &cbInfo) {
33 auto task = std::exchange(context->task,
nullptr);
34 context->timerId.reset();
35 task(cbInfo.jsState());
38 std::max(delay, std::chrono::milliseconds(0)).count(),
40 context->timerId = timerId;
42 return QtOhos::makeDestroyNotifier(
44 if (context->timerId.has_value()) {
46 [&](JsState &jsState) {
47 jsState.eval(
"Global.clearTimeout(*)", {context->timerId.value()});
50 context->task =
nullptr;
51 context->timerId.reset();
77QOhosOptional<std::uint32_t> tryGetCodeFromJsBusinessError(
const Napi::Error &error)
79 if (!error.Value().IsObject())
82 auto errorObject = QNapi::checkedCast<QNapi::Object>(error.Value());
83 auto optErrorCode = QNapi::getOptionalPropOrEmpty<QNapi::Number>(errorObject,
"code");
85 return !optErrorCode.IsEmpty()
86 ? makeQOhosOptional(optErrorCode.Uint32Value())
90void rethrowUnlessJsBusinessErrorIs(
91 const Napi::Error &error,
std::uint32_t suppressedErrorCode,
const char *callerContextName)
93 if (tryGetCodeFromJsBusinessError(error) != suppressedErrorCode)
97 "%s: ignored expected JS business error %u",
98 callerContextName, suppressedErrorCode);