Skip to content
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

Auto-workletize React Native Gesture Handler callback functions #2433

Merged
merged 5 commits into from
Sep 21, 2021

Conversation

tomekzaw
Copy link
Member

@tomekzaw tomekzaw commented Sep 17, 2021

Description

This PR adds support for detecting patterns in JavaScript code which are specific to React Native Gesture Handler, like Gesture.Tap().onEnd(...), so that they can be automatically converted into worklets even if they are not marked with 'worklet'; directive.

The general pattern is Gesture.Foo()[*].bar(fun), where:

  • Foo is a gesture object, like Tap, Pan, Pinch, etc.
  • [*] is any number of chained builder method calls (e.g. numberOfTaps(2) or onStart(...))
  • bar is any builder method name, e.g. onEnd
  • fun is a function declaration, function expression or arrow function expression

The following callback functions will be auto-workletized:

import { Gesture } from 'react-native-gesture-handler';

const foo = Gesture.Tap()
  .numberOfTaps(2)
  .onBegan(() => {
    // from now on, you may skip the 'worklet'; directive here...
    console.log('onBegan');
  })
  .onStart((_event) => {
    // ...and here...
    console.log('onStart');
  })
  .onEnd((_event, _success) => {
    // ...and here too!
    console.log('onEnd');
  });

Please note that the following callback functions will not be auto-workletized:

somethingNotRelatedToGestures.onEnd(( /* ... */ ) => { /* ... */ });
const tap = Gesture.Tap();
tap.onEnd((_event, success) => { /* ... */ });
const fun = (_event, success) => { /* ... */ };
Gesture.Tap().onEnd(fun);
import { Gesture as RNGHGesture } from 'react-native-gesture-handler';
RNGHGesture.Tap().onEnd((_event, success) => { /* ... */ });

In all the above cases, you still need to specify the 'worklet'; directive explicitly.

Changes

  • Added processIfGestureHandlerEventCallbackFunctionNode function in babel.config.js
  • Added snapshot tests for functions that should and should not be workletized

Test code and steps to reproduce

yarn jest

Checklist

  • Included code example that can be used to test this change
  • Updated TS types
  • Added TS types tests
  • Added unit / integration tests
  • Updated documentation
  • Ensured that CI passes

Copy link
Member

@piaskowyk piaskowyk left a comment

Choose a reason for hiding this comment

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

🚀

@piaskowyk piaskowyk merged commit 2b7b92b into master Sep 21, 2021
@piaskowyk piaskowyk deleted the @tomekzaw/autoworkletize-gesture-handler-callbacks branch September 21, 2021 11:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants