diff --git a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp index 3ff260e6a680..b7b80a0aba31 100644 --- a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp +++ b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.cpp @@ -187,6 +187,21 @@ jsi::Value WorkletRuntime::executeSync( return job(uiRuntime); } +void WorkletRuntime::executeAsync( + std::function &&job) const { + queue_->push([job = std::move(job), weakThis = weak_from_this()]() { + auto strongThis = weakThis.lock(); + if (!strongThis) { + return; + } + + auto lock = + std::unique_lock(*strongThis->runtimeMutex_); + jsi::Runtime &runtime = strongThis->getJSIRuntime(); + job(runtime); + }); +} + jsi::Value WorkletRuntime::get( jsi::Runtime &rt, const jsi::PropNameID &propName) { diff --git a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h index 3d3d3724e7e2..2ed89df1696b 100644 --- a/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h +++ b/packages/react-native-worklets/Common/cpp/worklets/WorkletRuntime/WorkletRuntime.h @@ -61,6 +61,8 @@ class WorkletRuntime : public jsi::HostObject, jsi::Value executeSync( const std::function &job) const; + void executeAsync(std::function &&job) const; + std::string toString() const { return "[WorkletRuntime \"" + name_ + "\"]"; }