Skip to content

feat(Worklets): runAsyncGuardedOnUI #7906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 25 commits into from
Jul 30, 2025
Merged

Conversation

tjzel
Copy link
Collaborator

@tjzel tjzel commented Jul 25, 2025

Summary

Previously the AsyncQueue of UIRuntime was always set to nullptr, making it impossible to invoke runGuardedAsync on it. I created a simple implementation of AsyncQueue for UI Runtime to enable that invocation. It's needed for asynchronous promise resolution on the UI Runtime.

Test plan

Apply the following diff and see that runOnRuntime with UIRuntime works properly.

diff --git a/apps/common-app/src/apps/reanimated/examples/EmptyExample.tsx b/apps/common-app/src/apps/reanimated/examples/EmptyExample.tsx
index 7f3a1ac26c..10d21761ab 100644
--- a/apps/common-app/src/apps/reanimated/examples/EmptyExample.tsx
+++ b/apps/common-app/src/apps/reanimated/examples/EmptyExample.tsx
@@ -1,10 +1,21 @@
 import React from 'react';
-import { StyleSheet, Text, View } from 'react-native';
+import { Button, StyleSheet, Text, View } from 'react-native';
+import { runOnRuntime } from 'react-native-worklets';
 
 export default function EmptyExample() {
   return (
     <View style={styles.container}>
       <Text>Hello world!</Text>
+      <Button
+        title="Run on UI Runtime"
+        onPress={() => {
+          const UIRuntimeRef = globalThis.__UIRuntimeRef;
+          runOnRuntime(UIRuntimeRef, () => {
+            'worklet';
+            console.log('This is running on the UI runtime with UIAsyncQueue');
+          })();
+        }}
+      />
     </View>
   );
 }
diff --git a/packages/react-native-worklets/android/src/main/cpp/worklets/android/WorkletsModule.cpp b/packages/react-native-worklets/android/src/main/cpp/worklets/android/WorkletsModule.cpp
index 84979ee67e..6622d138fe 100644
--- a/packages/react-native-worklets/android/src/main/cpp/worklets/android/WorkletsModule.cpp
+++ b/packages/react-native-worklets/android/src/main/cpp/worklets/android/WorkletsModule.cpp
@@ -1,3 +1,4 @@
+#include <jsi/jsi.h>
 #include <worklets/NativeModules/JSIWorkletsModuleProxy.h>
 #include <worklets/Tools/WorkletsJSIUtils.h>
 #include <worklets/WorkletRuntime/RNRuntimeWorkletDecorator.h>
@@ -33,6 +34,11 @@ WorkletsModule::WorkletsModule(
           sourceURL)) {
   auto jsiWorkletsModuleProxy =
       workletsModuleProxy_->createJSIWorkletsModuleProxy();
+  auto uiRuntimeRef = jsi::Object::createFromHostObject(
+      *rnRuntime, jsiWorkletsModuleProxy->getRuntimeManager()->getUIRuntime());
+  rnRuntime_->global().setProperty(
+      *rnRuntime_, "__UIRuntimeRef", std::move(uiRuntimeRef));
+
   auto optimizedJsiWorkletsModuleProxy = jsi_utils::optimizedFromHostObject(
       *rnRuntime_,
       std::static_pointer_cast<jsi::HostObject>(

@tjzel tjzel marked this pull request as ready for review July 25, 2025 15:19
@tjzel tjzel requested a review from Copilot July 25, 2025 22:15
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR adds asynchronous execution support to the UI Runtime by implementing an AsyncQueue for the UIRuntime. Previously, the UI Runtime's AsyncQueue was always null, preventing the use of runAsyncGuarded functionality needed for asynchronous promise resolution.

Key changes:

  • Created AsyncQueueUI class that uses UIScheduler for async execution on UI thread
  • Modified RuntimeManager to accept an AsyncQueue parameter for UI runtime creation
  • Refactored runAsyncGuarded implementation to move from header to implementation file

Reviewed Changes

Copilot reviewed 7 out of 7 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
WorkletRuntime.h Moved runAsyncGuarded method declaration from inline to separate implementation
WorkletRuntime.cpp Added implementation of runAsyncGuarded with improved error messaging
RuntimeManager.h Updated createUninitializedUIRuntime to accept AsyncQueue parameter
RuntimeManager.cpp Modified UI runtime creation to use provided AsyncQueue
AsyncQueueImpl.h Added AsyncQueueUI class declaration
AsyncQueueImpl.cpp Implemented AsyncQueueUI using UIScheduler for UI thread execution
WorkletsModuleProxy.cpp Updated to create UI runtime with new AsyncQueueUI instance

Base automatically changed from @tjzel/worklets/runtime-async-queue to main July 30, 2025 08:54
@tjzel tjzel added this pull request to the merge queue Jul 30, 2025
Merged via the queue into main with commit 38274c5 Jul 30, 2025
17 checks passed
@tjzel tjzel deleted the @tjzel/worklets/runGuardedAsync-on-UI branch July 30, 2025 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants