import { useEffect, useMemo } from "react"; import type { ColorValue } from "react-native"; import { type HybridView as _HybridView, type HybridViewMethods, type HybridViewProps, NitroModules, } from "react-native-nitro-modules"; import { isSharedValue as _isSharedValue, type DerivedValue, processColor, type SharedValue, startMapper, stopMapper, useSharedValue, } from "react-native-reanimated"; import { runOnUISync, type WorkletFunction } from "react-native-worklets"; type WithSharedValue = | T | SharedValue | DerivedValue | SharedValue> | DerivedValue>; export type WithSharedValueObj = { [k in keyof T]: WithSharedValue }; export const isSharedValue = _isSharedValue as ( value: unknown, ) => value is | SharedValue | DerivedValue | SharedValue> | DerivedValue>; export const getValue = (value: WithSharedValue): T => { "worklet"; return isSharedValue(value) ? value.value : value; }; export const useAnimatedNitroViewRef = < T extends HybridViewProps, U extends HybridViewMethods, >() => { type HybridView = _HybridView; const svRef = useSharedValue(null); const setRef = useMemo( () => ({ f: (ref: HybridView) => { runOnUISync((boxedRef) => { svRef.value = boxedRef.unbox(); }, NitroModules.box(ref)); }, }), [svRef], ); return [svRef, setRef] as const; }; export const processColors = (colors: ColorValue[]) => { "worklet"; return colors.map((it) => typeof it === "number" ? it : processColor(it) || 0, ); }; // @ts-expect-error This is how reanimated works. export function useSharedValuesEffect( effect: () => void, ) export function useSharedValuesEffect( effect: WorkletFunction, ) { useEffect(()=>{ const mapperId = startMapper(effect, Object.values(effect.__closure || {})); return () => stopMapper(mapperId); }, [effect.__workletHash]) }