diff --git a/src/reanimated2/PropAdapters.ts b/src/reanimated2/PropAdapters.ts index 4c8e0f9af0e..9d3226d0d24 100644 --- a/src/reanimated2/PropAdapters.ts +++ b/src/reanimated2/PropAdapters.ts @@ -1,22 +1,24 @@ 'use strict'; import { addWhitelistedNativeProps } from '../ConfigHelper'; -import type { __AdapterWorkletFunction } from './commonTypes'; -import type { AnimatedPropsAdapterFunction } from './helperTypes'; +import type { + AnimatedPropsAdapterFunction, + AnimatedPropsAdapterWorklet, +} from './commonTypes'; -// TODO TYPESCRIPT This is a temporary type to get rid of .d.ts file. -type createAnimatedPropAdapterType = ( +// @ts-expect-error This overload is required by our API. +export function createAnimatedPropAdapter( adapter: AnimatedPropsAdapterFunction, nativeProps?: string[] -) => AnimatedPropsAdapterFunction; +): AnimatedPropsAdapterFunction; -export const createAnimatedPropAdapter = (( - adapter: __AdapterWorkletFunction, +export function createAnimatedPropAdapter( + adapter: AnimatedPropsAdapterWorklet, nativeProps?: string[] -): __AdapterWorkletFunction => { +): AnimatedPropsAdapterWorklet { const nativePropsToAdd: { [key: string]: boolean } = {}; nativeProps?.forEach((prop) => { nativePropsToAdd[prop] = true; }); addWhitelistedNativeProps(nativePropsToAdd); return adapter; -}) as createAnimatedPropAdapterType; +} diff --git a/src/reanimated2/commonTypes.ts b/src/reanimated2/commonTypes.ts index 45ebc410551..31e6a8b24b2 100644 --- a/src/reanimated2/commonTypes.ts +++ b/src/reanimated2/commonTypes.ts @@ -60,6 +60,12 @@ export type MapperRegistry = { stop: (mapperID: number) => void; }; +export type WorkletStackDetails = [ + error: Error, + lineOffset: number, + columnOffset: number +]; + type WorkletClosure = Record; interface WorkletInitDataCommon { @@ -85,7 +91,10 @@ interface WorkletBaseRelease extends WorkletBaseCommon { interface WorkletBaseDev extends WorkletBaseCommon { __initData: WorkletInitDataDev; - __stackDetails: Error; + /** + * `__stackDetails` is removed after parsing. + */ + __stackDetails?: WorkletStackDetails; } export type WorkletFunction< @@ -93,6 +102,30 @@ export type WorkletFunction< ReturnValue = unknown > = ((...args: Args) => ReturnValue) & (WorkletBaseRelease | WorkletBaseDev); +/** + * This function works well on the JS thread performance-wise, since the JIT can inline it. + * However, on other threads it will not get optimized and we will get a function call overhead. + */ +export function isWorklet< + Args extends unknown[] = unknown[], + ReturnValue = unknown, + BuildType extends WorkletBaseDev | WorkletBaseRelease = WorkletBaseDev +>(value: unknown): value is WorkletFunction & BuildType { + 'worklet'; + // Since host objects always return true for `in` operator, we have to use dot notation to check if the property exists. + // See https://github.com/facebook/hermes/blob/340726ef8cf666a7cce75bc60b02fa56b3e54560/lib/VM/JSObject.cpp#L1276. + return !!(value as Record).__workletHash; +} + +export type AnimatedPropsAdapterFunction = ( + props: Record +) => void; + +export type AnimatedPropsAdapterWorklet = WorkletFunction< + [props: Record], + void +>; + export interface NestedObject { [key: string]: NestedObjectValues; } @@ -253,36 +286,3 @@ export enum ReduceMotion { Always = 'always', Never = 'never', } - -// THE LAND OF THE DEPRECATED - -/** - * @deprecated don't use - */ -export interface __WorkletFunction { - __closure?: Record; - __workletHash?: number; -} - -/** - * @deprecated don't use - */ -export interface __BasicWorkletFunction extends __WorkletFunction { - (): T; -} - -/** - * @deprecated don't use - */ -export interface __ComplexWorkletFunction - extends __WorkletFunction { - (...args: A): R; - __remoteFunction?: (...args: A) => R; -} - -/** - * @deprecated don't use - */ -export interface __AdapterWorkletFunction extends __WorkletFunction { - (value: NestedObject): void; -} diff --git a/src/reanimated2/errors.ts b/src/reanimated2/errors.ts index 89b563663c8..4073489b010 100644 --- a/src/reanimated2/errors.ts +++ b/src/reanimated2/errors.ts @@ -1,11 +1,11 @@ 'use strict'; -type StackDetails = [Error, number, number]; +import type { WorkletStackDetails } from './commonTypes'; -const _workletStackDetails = new Map(); +const _workletStackDetails = new Map(); export function registerWorkletStackDetails( hash: number, - stackDetails: StackDetails + stackDetails: WorkletStackDetails ) { _workletStackDetails.set(hash, stackDetails); } diff --git a/src/reanimated2/globals.d.ts b/src/reanimated2/globals.d.ts index 4bbae6d930c..dbc346cc2a9 100644 --- a/src/reanimated2/globals.d.ts +++ b/src/reanimated2/globals.d.ts @@ -7,7 +7,6 @@ import type { MapperRegistry, ShareableRef, ShadowNodeWrapper, - __ComplexWorkletFunction, FlatShareableRef, } from './commonTypes'; import type { AnimatedStyle } from './helperTypes'; @@ -42,10 +41,7 @@ declare global { var _notifyAboutEnd: (tag: number, removeView: boolean) => void; var _setGestureState: (handlerTag: number, newState: number) => void; var _makeShareableClone: (value: T) => FlatShareableRef; - var _scheduleOnJS: ( - fun: __ComplexWorkletFunction, - args?: unknown[] - ) => void; + var _scheduleOnJS: (fun: (...args: A) => R, args?: A) => void; var _scheduleOnRuntime: ( runtime: WorkletRuntime, worklet: ShareableRef<() => void> diff --git a/src/reanimated2/helperTypes.ts b/src/reanimated2/helperTypes.ts index 9bfd4ff5b9d..e26438eff65 100644 --- a/src/reanimated2/helperTypes.ts +++ b/src/reanimated2/helperTypes.ts @@ -23,7 +23,6 @@ import type { } from './layoutReanimation/animationBuilder/commonTypes'; import type { ReanimatedKeyframe } from './layoutReanimation/animationBuilder/Keyframe'; import type { SharedTransition } from './layoutReanimation/sharedTransitions'; -import type { DependencyList } from './hook/commonTypes'; export type TransformArrayItem = Extract< TransformsStyle['transform'], @@ -160,20 +159,6 @@ export type AnimatedProps = RestProps & animatedProps?: Partial>; }; -export type AnimatedPropsAdapterFunction = ( - props: Record -) => void; - -export type useAnimatedPropsType = ( - updater: () => Partial, - deps?: DependencyList | null, - adapters?: - | AnimatedPropsAdapterFunction - | AnimatedPropsAdapterFunction[] - | null, - isAnimatedProps?: boolean -) => Partial; - // THE LAND OF THE DEPRECATED /** diff --git a/src/reanimated2/hook/commonTypes.ts b/src/reanimated2/hook/commonTypes.ts index a346da9a47f..e294c9b10db 100644 --- a/src/reanimated2/hook/commonTypes.ts +++ b/src/reanimated2/hook/commonTypes.ts @@ -1,6 +1,11 @@ 'use strict'; import type { Component, MutableRefObject } from 'react'; -import type { ShadowNodeWrapper, SharedValue } from '../commonTypes'; +import type { + AnimatedPropsAdapterFunction, + ShadowNodeWrapper, + SharedValue, + WorkletFunction, +} from '../commonTypes'; import type { ImageStyle, NativeSyntheticEvent, @@ -92,3 +97,13 @@ export interface JestAnimatedStyleHandle< > extends AnimatedStyleHandle