Skip to main content

isWorkletFunction

isWorkletFunction checks if a function is a worklet function. It only works with Reanimated Babel plugin enabled. Unless you are doing something with internals of Reanimated you shouldn't need to use this function.

Reference

import { isWorkletFunction } from 'react-native-worklets';

const workletFunction = () => {
'worklet';
return 1;
};

const isWorkletFunction = isWorkletFunction(workletFunction);

console.log(isWorkletFunction); // true

const nonWorkletFunction = () => {
return 1;
};

const isNonWorkletFunction = isWorkletFunction(nonWorkletFunction);

console.log(isNonWorkletFunction); // false

Type definitions

type WorkletClosure = Record<string, unknown>;

export type WorkletStackDetails = [
error: Error,
lineOffset: number,
columnOffset: number,
];

interface WorkletInitData {
code: string;
/** Only in dev builds. */
location?: string;
/** Only in dev builds. */
sourceMap?: string;
/** Only in dev builds. */
version?: string;
}

interface WorkletProps {
__closure: WorkletClosure;
__workletHash: number;
__initData: WorkletInitData;
__init?: () => unknown;
/** `__stackDetails` is removed after parsing. */
__stackDetails?: WorkletStackDetails;
}

type WorkletFunction<
TArgs extends unknown[] = unknown[],
TReturn = unknown,
> = ((...args: TArgs) => TReturn) & WorkletProps;

function isWorkletFunction<
Args extends unknown[] = unknown[],
ReturnValue = unknown,
>(value: unknown): value is WorkletFunction<Args, ReturnValue>

Arguments

value

A function to check if it is a worklet function.

Returns

isWorkletFunction returns a boolean value.

Remarks

  • Do not call it before the worklet is declared, as it will always return false then.

    isWorkletFunction(myWorklet); // Will always return false.

    function myWorklet() {
    'worklet';
    }