feat: Implement RN Worklets/Background Threads#17674
Merged
andrepimenta merged 17 commits intomainfrom Aug 1, 2025
Merged
Conversation
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
sethkfman
reviewed
Jul 28, 2025
Cal-L
reviewed
Jul 28, 2025
Co-authored-by: Cal Leung <cal.leung@consensys.net>
Co-authored-by: Cal Leung <cal.leung@consensys.net>
Co-authored-by: Cal Leung <cal.leung@consensys.net>
Co-authored-by: Cal Leung <cal.leung@consensys.net>
Co-authored-by: Cal Leung <cal.leung@consensys.net>
Co-authored-by: Cal Leung <cal.leung@consensys.net>
Co-authored-by: Cal Leung <cal.leung@consensys.net>
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.



Description
This PR implements Worklets. Worklets are small JavaScript functions that can be executed on a separate JavaScript Thread so they don't clog the main JS Thread. Perfect for heavy computations and processing.
Screenshots/Recordings
Before
Before.-.worklets.disabled.mov
After
After.-.worklets.enabled.mov
How to use RN Worklets (Threads)
runAsync
Purpose: runAsync is used to execute code on a different thread without blocking the main JS thread.
When to use: Use it to offload functions to a background thread.
Example:
runOnJS
Purpose: runOnJS is used to call a JavaScript function from within a worklet. Since worklets run on a separate thread, they cannot directly call JavaScript functions. runOnJS bridges this gap by allowing you to invoke JavaScript functions on the JavaScript thread.
When to use: Use it when you need to communicate from the worklet thread back to the JavaScript thread.
Example:
useWorklet
Purpose: useWorklet is a hook that allows you to define a worklet function directly within your React component. It ensures that the function is properly marked as a worklet and can run on the worklet thread.
When to use: Use this hook when you need to define a function that will execute on the worklet thread.
Example:
useRunOnJS
Purpose: useRunOnJS is a hook that allows you to define a JavaScript function that can be safely called from a worklet. It ensures that the function is properly wrapped to run on the JavaScript thread when invoked from the worklet thread.
When to use: Use this hook when you need to call a JavaScript function from a worklet.
useSharedValue
Purpose:
useSharedValue is a hook that creates a SharedValue instance, which can be read from and written to by both the JavaScript thread and the worklet thread simultaneously.
For arrays and objects, useSharedValue creates a C++-based proxy implementation, ensuring that all read and write operations on these data structures are thread-safe.
When to use:
Use useSharedValue when you need to share state between the JavaScript thread and the worklet thread.
Example:
Separate Threads/Contexts (Worklets.createContext)
Purpose:
Worklets.createContext is a method that allows you to create a separate thread (context) for running worklets.
Each thread created with createContext operates in isolation, ensuring that tasks in one thread do not interfere with tasks in another.
When to use:
Use Worklets.createContext when you need to offload heavy computations or background tasks to multiple separate threads.
Worklet Examples
WorkletExampleComponent.tsx
WorkletExampleFunctions.ts
Pre-merge author checklist
Pre-merge reviewer checklist