15std::shared_ptr<
void> startDelayedJsThreadTask(
16 QOhosJsState &jsState, std::function<
void(QOhosJsState &)> task,
17 std::chrono::milliseconds delay)
21 std::function<
void(QOhosJsState &)> task;
22 std::optional<
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 QOhosCallbackInfo &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()) {
45 QOhosJsThreadGateway::runAndWait(
46 [&](QOhosJsState &jsState) {
47 jsState.eval(
"Global.clearTimeout(*)", {context->timerId.value()});
50 context->task =
nullptr;
51 context->timerId.reset();
57 QOhosJsState &jsState,
std::function<
void(
const QOhosCallbackInfo &)> timeoutFunc,
58 std::chrono::milliseconds delay)
60 int timerId = jsState.eval<QNapi::Number>(
61 "Global.setTimeout(*)", {std::move(timeoutFunc), delay.count()});