Skip to content

Commit 243a607

Browse files
authored
Automatically workletize callbacks from Gesture Handler hooks api (#8599)
## Summary Adds autoworkletization for Gesture Handler hooks API. It uses the same mechanism as the Reanimated hooks. This PR also adds support for reference following inside objects. I.e.: ```js const handleStart = () => { // will be workletized console.log('start') } useTap({ onStart: handleStart }) ``` Let me know if you would prefer to have that separately. ## Test plan Added tests
1 parent 2442f28 commit 243a607

File tree

7 files changed

+391
-151
lines changed

7 files changed

+391
-151
lines changed

packages/react-native-worklets/__tests__/__snapshots__/plugin.test.ts.snap

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2908,6 +2908,72 @@ var foo = function foo_null1Factory(_ref) {
29082908
});"
29092909
`;
29102910

2911+
exports[`babel plugin for react-native-gesture-handler workletizes gesture callbacks using the hooks api 1`] = `
2912+
"var _worklet_13539781966089_init_data = {
2913+
code: "function null1(){console.log('onBegin');}",
2914+
location: "/dev/null",
2915+
sourceMap: "\\"mock source map\\""
2916+
};
2917+
var _worklet_16446448315454_init_data = {
2918+
code: "function null2(_event){console.log('onStart');}",
2919+
location: "/dev/null",
2920+
sourceMap: "\\"mock source map\\""
2921+
};
2922+
var _worklet_16462958026656_init_data = {
2923+
code: "function null3(_event,_success){console.log('onEnd');}",
2924+
location: "/dev/null",
2925+
sourceMap: "\\"mock source map\\""
2926+
};
2927+
var foo = useTapGesture({
2928+
numberOfTaps: 2,
2929+
onBegin: function null1Factory(_ref) {
2930+
var _worklet_13539781966089_init_data = _ref._worklet_13539781966089_init_data;
2931+
var _e = [new global.Error(), 1, -27];
2932+
var null1 = function null1() {
2933+
console.log('onBegin');
2934+
};
2935+
null1.__closure = {};
2936+
null1.__workletHash = 13539781966089;
2937+
null1.__pluginVersion = "x.y.z";
2938+
null1.__initData = _worklet_13539781966089_init_data;
2939+
null1.__stackDetails = _e;
2940+
return null1;
2941+
}({
2942+
_worklet_13539781966089_init_data: _worklet_13539781966089_init_data
2943+
}),
2944+
onStart: function null2Factory(_ref2) {
2945+
var _worklet_16446448315454_init_data = _ref2._worklet_16446448315454_init_data;
2946+
var _e = [new global.Error(), 1, -27];
2947+
var null2 = function null2(_event) {
2948+
console.log('onStart');
2949+
};
2950+
null2.__closure = {};
2951+
null2.__workletHash = 16446448315454;
2952+
null2.__pluginVersion = "x.y.z";
2953+
null2.__initData = _worklet_16446448315454_init_data;
2954+
null2.__stackDetails = _e;
2955+
return null2;
2956+
}({
2957+
_worklet_16446448315454_init_data: _worklet_16446448315454_init_data
2958+
}),
2959+
onEnd: function null3Factory(_ref3) {
2960+
var _worklet_16462958026656_init_data = _ref3._worklet_16462958026656_init_data;
2961+
var _e = [new global.Error(), 1, -27];
2962+
var null3 = function null3(_event, _success) {
2963+
console.log('onEnd');
2964+
};
2965+
null3.__closure = {};
2966+
null3.__workletHash = 16462958026656;
2967+
null3.__pluginVersion = "x.y.z";
2968+
null3.__initData = _worklet_16462958026656_init_data;
2969+
null3.__stackDetails = _e;
2970+
return null3;
2971+
}({
2972+
_worklet_16462958026656_init_data: _worklet_16462958026656_init_data
2973+
})
2974+
});"
2975+
`;
2976+
29112977
exports[`babel plugin for react-native-gesture-handler workletizes possibly chained gesture object callback functions automatically 1`] = `
29122978
"var _worklet_2359797567586_init_data = {
29132979
code: "function null1(_event,_success){console.log('onEnd');}",
@@ -2992,6 +3058,62 @@ var onStart = function onStart_null1Factory({
29923058
var foo = Gesture.Tap().onStart(onStart);"
29933059
`;
29943060

3061+
exports[`babel plugin for react-native-gesture-handler workletizes referenced gesture callbacks using the hooks api 1`] = `
3062+
"var _worklet_12687812344336_init_data = {
3063+
code: "function onBegin_null1(){console.log('onBegin');}",
3064+
location: "/dev/null",
3065+
sourceMap: "\\"mock source map\\""
3066+
};
3067+
var onBegin = function onBegin_null1Factory({
3068+
_worklet_12687812344336_init_data
3069+
}) {
3070+
const _e = [new global.Error(), 1, -27];
3071+
const onBegin = function () {
3072+
console.log('onBegin');
3073+
};
3074+
onBegin.__closure = {};
3075+
onBegin.__workletHash = 12687812344336;
3076+
onBegin.__pluginVersion = "x.y.z";
3077+
onBegin.__initData = _worklet_12687812344336_init_data;
3078+
onBegin.__stackDetails = _e;
3079+
return onBegin;
3080+
}({
3081+
_worklet_12687812344336_init_data
3082+
});
3083+
var foo = useTapGesture({
3084+
numberOfTaps: 2,
3085+
onBegin: onBegin
3086+
});"
3087+
`;
3088+
3089+
exports[`babel plugin for react-native-gesture-handler workletizes referenced gesture callbacks using the hooks api and shorthand syntax 1`] = `
3090+
"var _worklet_12687812344336_init_data = {
3091+
code: "function onBegin_null1(){console.log('onBegin');}",
3092+
location: "/dev/null",
3093+
sourceMap: "\\"mock source map\\""
3094+
};
3095+
var onBegin = function onBegin_null1Factory({
3096+
_worklet_12687812344336_init_data
3097+
}) {
3098+
const _e = [new global.Error(), 1, -27];
3099+
const onBegin = function () {
3100+
console.log('onBegin');
3101+
};
3102+
onBegin.__closure = {};
3103+
onBegin.__workletHash = 12687812344336;
3104+
onBegin.__pluginVersion = "x.y.z";
3105+
onBegin.__initData = _worklet_12687812344336_init_data;
3106+
onBegin.__stackDetails = _e;
3107+
return onBegin;
3108+
}({
3109+
_worklet_12687812344336_init_data
3110+
});
3111+
var foo = useTapGesture({
3112+
numberOfTaps: 2,
3113+
onBegin: onBegin
3114+
});"
3115+
`;
3116+
29953117
exports[`babel plugin for referenced worklets prefers AssignmentExpression over VariableDeclarator 1`] = `
29963118
"var styleFactory = function styleFactory() {
29973119
return 1;

packages/react-native-worklets/__tests__/plugin.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -841,6 +841,59 @@ describe('babel plugin', () => {
841841
});
842842

843843
describe('for react-native-gesture-handler', () => {
844+
test('workletizes gesture callbacks using the hooks api', () => {
845+
const input = html`<script>
846+
const foo = useTapGesture({
847+
numberOfTaps: 2,
848+
onBegin: () => {
849+
console.log('onBegin');
850+
},
851+
onStart: (_event) => {
852+
console.log('onStart');
853+
},
854+
onEnd: (_event, _success) => {
855+
console.log('onEnd');
856+
},
857+
});
858+
</script>`;
859+
860+
const { code } = runPlugin(input);
861+
expect(code).toHaveWorkletData(3);
862+
expect(code).toMatchSnapshot();
863+
});
864+
865+
test('workletizes referenced gesture callbacks using the hooks api', () => {
866+
const input = html`<script>
867+
const onBegin = () => {
868+
console.log('onBegin');
869+
};
870+
const foo = useTapGesture({
871+
numberOfTaps: 2,
872+
onBegin: onBegin,
873+
});
874+
</script>`;
875+
876+
const { code } = runPlugin(input);
877+
expect(code).toHaveWorkletData(1);
878+
expect(code).toMatchSnapshot();
879+
});
880+
881+
test('workletizes referenced gesture callbacks using the hooks api and shorthand syntax', () => {
882+
const input = html`<script>
883+
const onBegin = () => {
884+
console.log('onBegin');
885+
};
886+
const foo = useTapGesture({
887+
numberOfTaps: 2,
888+
onBegin,
889+
});
890+
</script>`;
891+
892+
const { code } = runPlugin(input);
893+
expect(code).toHaveWorkletData(1);
894+
expect(code).toMatchSnapshot();
895+
});
896+
844897
test('workletizes possibly chained gesture object callback functions automatically', () => {
845898
const input = html`<script>
846899
const foo = Gesture.Tap()

0 commit comments

Comments
 (0)