- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 1.4k
 
          feat(Worklets): runOnRuntimeSync
          #8224
        
          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
          
     Open
      
      
            skusnierz
  wants to merge
  8
  commits into
  main
  
    
      
        
          
  
    
      Choose a base branch
      
     
    
      
        
      
      
        
          
          
        
        
          
            
              
              
              
  
           
        
        
          
            
              
              
           
        
       
     
  
        
          
            
          
            
          
        
       
    
      
from
@skusnierz/run-on-runtime-sync
  
      
      
   
  
    
  
  
  
 
  
      
    base: main
Could not load branches
            
              
  
    Branch not found: {{ refName }}
  
            
                
      Loading
              
            Could not load tags
            
            
              Nothing to show
            
              
  
            
                
      Loading
              
            Are you sure you want to change the base?
            Some commits from the old base branch may be removed from the timeline,
            and old review comments may become outdated.
          
          
  
     Open
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            8 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      a1daa99
              
                Implement runOnRuntimeSync function
              
              
                skusnierz 88e28d9
              
                add type tests
              
              
                skusnierz e15fb9c
              
                add runtime tests
              
              
                skusnierz 4b1adb2
              
                Add docs
              
              
                skusnierz 61f294a
              
                fix runtime tests path
              
              
                skusnierz 3a80161
              
                add missing import to docs
              
              
                skusnierz 8ebbf2e
              
                Adjust to review
              
              
                skusnierz c5f563e
              
                Sort imports
              
              
                skusnierz File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      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
    
  
  
    
              
        
          
          
            57 changes: 57 additions & 0 deletions
          
          57 
        
  ...on-app/src/apps/reanimated/examples/RuntimeTests/tests/runtimes/runOnRuntimeSync.test.tsx
  
  
      
      
   
        
      
      
    
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,57 @@ | ||
| import { | ||
| createSynchronizable, | ||
| createWorkletRuntime, | ||
| scheduleOnRN, | ||
| runOnRuntimeSync, | ||
| scheduleOnRuntime, | ||
| } from 'react-native-worklets'; | ||
| import { describe, expect, test, notify, waitForNotification } from '../../ReJest/RuntimeTestsApi'; | ||
| import { ComparisonMode } from '../../ReJest/types'; | ||
| 
     | 
||
| const NOTIFICATION_NAME = 'NOTIFICATION_NAME'; | ||
| 
     | 
||
| describe('runOnRuntimeSync', () => { | ||
| test('use runOnRuntimeSync to run a function on the Worker Runtime from RN Runtime', () => { | ||
| // Arrange | ||
| const workletRuntime = createWorkletRuntime({ name: 'test' }); | ||
| 
     | 
||
| // Act | ||
| const result = runOnRuntimeSync(workletRuntime, () => { | ||
| 'worklet'; | ||
| return 100; | ||
| }); | ||
| 
     | 
||
| // Assert | ||
| expect(result).toBe(100, ComparisonMode.NUMBER); | ||
| }); | ||
| 
     | 
||
| test('keep the correct order of execution runOnRuntimeSync and scheduleOnRuntime', async () => { | ||
| // Arrange | ||
| const workletRuntime = createWorkletRuntime({ name: 'test' }); | ||
| const synchronizable = createSynchronizable(0); | ||
| 
     | 
||
| const onJSCallback = () => { | ||
| notify(NOTIFICATION_NAME); | ||
| }; | ||
| 
     | 
||
| // Act | ||
| // TODO: Replace with `runOnRuntimeAsync`. | ||
| scheduleOnRuntime(workletRuntime, () => { | ||
| 'worklet'; | ||
| // heavy computation | ||
| new Array(50_000_000).map((_v, i) => i ** 2); | ||
| synchronizable.setBlocking(100); | ||
| scheduleOnRN(onJSCallback); | ||
| }); | ||
| 
     | 
||
| const result = runOnRuntimeSync(workletRuntime, () => { | ||
| 'worklet'; | ||
| return 100; | ||
| }); | ||
| 
     | 
||
| // Assert | ||
| expect(result).toBe(100, ComparisonMode.NUMBER); | ||
| await waitForNotification(NOTIFICATION_NAME); | ||
| expect(synchronizable.getBlocking()).toBe(100, ComparisonMode.NUMBER); | ||
| }); | ||
| }); | ||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 6 | ||
| sidebar_position: 8 | ||
| --- | ||
| 
     | 
||
| # createWorkletRuntime | ||
| 
          
            
          
           | 
    ||
        
          
          
            2 changes: 1 addition & 1 deletion
          
          2 
        
  packages/docs-worklets/docs/threading/executeOnUIRuntimeSync.mdx
  
  
      
      
   
        
      
      
    
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 4 | ||
| sidebar_position: 3 | ||
| --- | ||
| 
     | 
||
| <DeprecatedBanner | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 8 | ||
| sidebar_position: 12 | ||
| --- | ||
| 
     | 
||
| # getRuntimeKind | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 8 | ||
| sidebar_position: 13 | ||
| --- | ||
| 
     | 
||
| # isWorkletFunction | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 5 | ||
| sidebar_position: 9 | ||
| --- | ||
| 
     | 
||
| <DeprecatedBanner | ||
| 
          
            
          
           | 
    ||
        
          
          
            79 changes: 79 additions & 0 deletions
          
          79 
        
  packages/docs-worklets/docs/threading/runOnRuntimeSync.mdx
  
  
      
      
   
        
      
      
    
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | ||||||
|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,79 @@ | ||||||||
| --- | ||||||||
| sidebar_position: 11 | ||||||||
| --- | ||||||||
| 
     | 
||||||||
| # runOnRuntimeSync | ||||||||
| 
     | 
||||||||
| `runOnRuntimeSync` lets you run a [workletized](/docs/fundamentals/glossary#to-workletize) function synchronously on a [Worker Runtime](/docs/fundamentals/glossary#worker-worklet-runtime---worker-runtime). | ||||||||
| It might preempt the Runtime where it's called from the thread (it's blocking). | ||||||||
| 
         
      Comment on lines
    
      +7
     to 
      +8
    
   
  There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 
        Suggested change
       
    
  | 
||||||||
| ## Reference | ||||||||
| 
     | 
||||||||
| ```javascript | ||||||||
| import { runOnRuntimeSync, createWorkletRuntime } from 'react-native-worklets'; | ||||||||
| 
     | 
||||||||
| const workletRuntime = createWorkletRuntime({ name: 'background' }); | ||||||||
| 
     | 
||||||||
| const result = runOnRuntimeSync(workletRuntime, () => { | ||||||||
| 'worklet'; | ||||||||
| return 2+2; | ||||||||
| }); // This will block the RN Runtime until the worklet is finished | ||||||||
| 
     | 
||||||||
| console.log(result); // 4 | ||||||||
| ``` | ||||||||
| 
     | 
||||||||
| 
     | 
||||||||
| <details> | ||||||||
| <summary>Type definitions</summary> | ||||||||
| 
     | 
||||||||
| ```typescript | ||||||||
| function runOnRuntimeSync<Args extends unknown[], ReturnValue>( | ||||||||
| workletRuntime: WorkletRuntime, | ||||||||
| worklet: (...args: Args) => ReturnValue, | ||||||||
| ...args: Args | ||||||||
| ): ReturnValue | ||||||||
| ``` | ||||||||
| 
     | 
||||||||
| </details> | ||||||||
| 
     | 
||||||||
| ## Arguments | ||||||||
| 
     | 
||||||||
| ### workletRuntime | ||||||||
| 
     | 
||||||||
| The worklet runtime to run the worklet on. | ||||||||
| 
     | 
||||||||
| ### worklet | ||||||||
| 
     | 
||||||||
| A reference to a function you want to execute on the [Worklet Runtime](/docs/fundamentals/glossary#worklet-runtime). | ||||||||
| 
     | 
||||||||
| ### args | ||||||||
| 
     | 
||||||||
| Arguments to the function you want to execute on the [Worklet Runtime](/docs/fundamentals/glossary#worklet-runtime). | ||||||||
| 
     | 
||||||||
| ## Remarks | ||||||||
                
      
                  tjzel marked this conversation as resolved.
               
          
            Show resolved
            Hide resolved
         | 
||||||||
| 
     | 
||||||||
| - `runOnRuntimeSync` can only be called on the [RN Runtime](/docs/fundamentals/glossary#react-native-runtime) unless the [Bundle Mode](/docs/experimental/bundleMode) is enabled. | ||||||||
| 
     | 
||||||||
| ```javascript | ||||||||
| import { createWorkletRuntime, runOnRuntimeSync } from 'react-native-worklets'; | ||||||||
| 
     | 
||||||||
| const workletRuntime = createWorkletRuntime({ name: 'background' }); | ||||||||
| 
     | 
||||||||
| runOnUI(() => { | ||||||||
| runOnRuntimeSync(workletRuntime, (greeting: string) => { | ||||||||
| console.log(`${greeting} from the background Worklet Runtime`); | ||||||||
| }, 'Hello'); // This will throw an error outside of Bundle Mode 🚨 | ||||||||
| }); | ||||||||
| ``` | ||||||||
| 
     | 
||||||||
| ```javascript | ||||||||
| import { createWorkletRuntime, scheduleOnRuntime } from 'react-native-worklets'; | ||||||||
| 
     | 
||||||||
| const workletRuntime = createWorkletRuntime({ name: 'background' }); | ||||||||
| const anotherWorkletRuntime = createWorkletRuntime({ name: 'anotherBackground' }); | ||||||||
| 
     | 
||||||||
| runOnRuntimeSync(anotherWorkletRuntime, () => { | ||||||||
| runOnRuntimeSync(workletRuntime, (greeting: string) => { | ||||||||
| console.log(`${greeting} from the background Worklet Runtime`); | ||||||||
| }, 'Hello'); // This will throw an error outside of Bundle Mode 🚨 | ||||||||
| }); | ||||||||
| ``` | ||||||||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 3 | ||
| sidebar_position: 4 | ||
| --- | ||
| 
     | 
||
| # runOnUIAsync | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 10 | ||
| sidebar_position: 5 | ||
| --- | ||
| 
     | 
||
| # runOnUISync | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 9 | ||
| sidebar_position: 7 | ||
| --- | ||
| 
     | 
||
| # scheduleOnRN | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| sidebar_position: 11 | ||
| sidebar_position: 6 | ||
| --- | ||
| 
     | 
||
| # scheduleOnUI | ||
| 
          
            
          
           | 
    ||
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
        
          
          
            46 changes: 46 additions & 0 deletions
          
          46 
        
  packages/react-native-worklets/__typetests__/runOnRuntimeSync.tsx
  
  
      
      
   
        
      
      
    
  
    
      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
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| /* eslint-disable @typescript-eslint/no-unused-vars */ | ||
| import { createWorkletRuntime, runOnRuntimeSync, runOnUISync } from '..'; | ||
| 
     | 
||
| function runOnRuntimeSyncTypeTests() { | ||
| const workletRuntime = createWorkletRuntime({ name: 'test' }); | ||
| // Correct usage - correct usage | ||
| runOnRuntimeSync( | ||
| workletRuntime, | ||
| (num: number): number => { | ||
| return num + 1; | ||
| }, | ||
| 0 | ||
| ); | ||
| 
     | 
||
| // @ts-expect-error - expected no args, but arg is provided | ||
| runOnRuntimeSync(workletRuntime, (): void => {}, 0); | ||
| 
     | 
||
| // Wrong args type | ||
| runOnRuntimeSync( | ||
| workletRuntime, | ||
| (num: number): number => { | ||
| return num + 1; | ||
| }, | ||
| // @ts-expect-error - wrong args type | ||
| 'tets' | ||
| ); | ||
| 
     | 
||
| // Wrong return type | ||
| runOnRuntimeSync( | ||
| workletRuntime, | ||
| (num: number): string => { | ||
| // @ts-expect-error - wrong return type | ||
| return num + 1; | ||
| }, | ||
| 0 | ||
| ); | ||
| 
     | 
||
| // @ts-expect-error - wrong return type | ||
| const result: string = runOnRuntimeSync( | ||
| workletRuntime, | ||
| (num: number): number => { | ||
| return num + 1; | ||
| }, | ||
| 0 | ||
| ); | ||
| } | 
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
  
    
      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
    
  
  
    
              
      
      Oops, something went wrong.
        
    
  
      
      Oops, something went wrong.
        
    
  
  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.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.