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

Add another ways to mark function as worklet. #1310

Merged
merged 6 commits into from
Oct 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ const functionHooks = new Map([
['useDerivedValue', [0]],
['useAnimatedScrollHandler', [0]],
['useAnimatedReaction', [0, 1]],
['useWorkletCallback', [0]],
['createWorklet', [0]],
]);

const objectHooks = new Set([
Expand Down
33 changes: 33 additions & 0 deletions react-native-reanimated-tests.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ import Animated, {
repeat,
sequence,
withDecay,
useWorkletCallback,
createWorklet,
runOnUI,
} from 'react-native-reanimated';

const styles = StyleSheet.create({
Expand Down Expand Up @@ -360,3 +363,33 @@ function WithDecayTest() {
</PanGestureHandler>
);
}

// useWorkletCallback
function UseWorkletCallbackTest() {
const callback = useWorkletCallback((a: number, b: number) => {
return a + b;
}, []);

runOnUI(() => {
const res = callback(1, 1);

console.log(res);
})();

return <Animated.View style={styles.container} />;
}

// createWorklet
function CreateWorkletTest() {
const callback = createWorklet((a: number, b: number) => {
return a + b;
});

runOnUI(() => {
const res = callback(1, 1);

console.log(res);
})();

return <Animated.View style={styles.container} />;
}
9 changes: 9 additions & 0 deletions react-native-reanimated.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ declare module 'react-native-reanimated' {
fn: (...args: A) => R
): (...args: Parameters<typeof fn>) => void;
export function processColor(color: number | string): number;
export function createWorklet<A extends any[], R>(
fn: (...args: A) => R
): (...args: Parameters<typeof fn>) => R;

type DependencyList = ReadonlyArray<any>;

Expand Down Expand Up @@ -480,6 +483,10 @@ declare module 'react-native-reanimated' {
handlers: ScrollHandlers<TContext>,
deps?: DependencyList
): OnScroll;
export function useWorkletCallback<A extends any[], R>(
fn: (...args: A) => R,
deps?: DependencyList
): (...args: Parameters<typeof fn>) => R;

export function useAnimatedRef<T extends Component>(): RefObject<T>;
export function measure<T extends Component>(
Expand Down Expand Up @@ -706,6 +713,8 @@ declare module 'react-native-reanimated' {
export const useAnimatedStyle: typeof Animated.useAnimatedStyle;
export const useAnimatedProps: typeof Animated.useAnimatedProps;
export const useDerivedValue: typeof Animated.useDerivedValue;
export const useWorkletCallback: typeof Animated.useWorkletCallback;
export const createWorklet: typeof Animated.createWorklet;
export const useAnimatedGestureHandler: typeof Animated.useAnimatedGestureHandler;
export const useAnimatedScrollHandler: typeof Animated.useAnimatedScrollHandler;
export const useAnimatedRef: typeof Animated.useAnimatedRef;
Expand Down
10 changes: 9 additions & 1 deletion src/reanimated2/Hooks.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useRef } from 'react';
import { useEffect, useRef, useCallback } from 'react';

import WorkletEventHandler from './WorkletEventHandler';
import {
Expand Down Expand Up @@ -594,3 +594,11 @@ export function useAnimatedReaction(prepare, react) {
};
}, inputs);
}

export function useWorkletCallback(fun, deps) {
return useCallback(fun, deps);
}

export function createWorklet(fun) {
return fun;
}