UNPKG

2.94 kBPlain TextView Raw
1'use strict';
2import { useEffect, useRef } from 'react';
3import type { WorkletFunction } from '../commonTypes';
4import { isWeb, isJest } from '../PlatformChecker';
5import type { DependencyList, ReanimatedEvent } from './commonTypes';
6import { areDependenciesEqual, buildDependencies } from './utils';
7import { makeShareable } from '../shareables';
8
9interface GeneralHandler<
10 Event extends object,
11 Context extends Record<string, unknown>
12> {
13 (event: ReanimatedEvent<Event>, context: Context): void;
14}
15
16type GeneralWorkletHandler<
17 Event extends object,
18 Context extends Record<string, unknown>
19> = WorkletFunction<[event: ReanimatedEvent<Event>, context: Context]>;
20
21type GeneralHandlers<
22 Event extends object,
23 Context extends Record<string, unknown>
24> = Record<string, GeneralHandler<Event, Context> | undefined>;
25
26type GeneralWorkletHandlers<
27 Event extends object,
28 Context extends Record<string, unknown>
29> = Record<string, GeneralWorkletHandler<Event, Context> | undefined>;
30
31interface ContextWithDependencies<Context extends Record<string, unknown>> {
32 context: Context;
33 savedDependencies: DependencyList;
34}
35
36export interface UseHandlerContext<Context extends Record<string, unknown>> {
37 context: Context;
38 doDependenciesDiffer: boolean;
39 useWeb: boolean;
40}
41
42/**
43 * Lets you find out whether the event handler dependencies have changed.
44 *
45 * @param handlers - An object of event handlers.
46 * @param dependencies - An optional array of dependencies.
47 * @returns An object containing a boolean indicating whether the dependencies have changed, and a boolean indicating whether the code is running on the web.
48 * @see https://docs.swmansion.com/react-native-reanimated/docs/advanced/useHandler
49 */
50// @ts-expect-error This overload is required by our API.
51export function useHandler<
52 Event extends object,
53 Context extends Record<string, unknown>
54>(
55 handlers: GeneralHandlers<Event, Context>,
56 dependencies?: DependencyList
57): UseHandlerContext<Context>;
58
59export function useHandler<
60 Event extends object,
61 Context extends Record<string, unknown>
62>(
63 handlers: GeneralWorkletHandlers<Event, Context>,
64 dependencies?: DependencyList
65): UseHandlerContext<Context> {
66 const initRef = useRef<ContextWithDependencies<Context> | null>(null);
67 if (initRef.current === null) {
68 const context = makeShareable({} as Context);
69 initRef.current = {
70 context,
71 savedDependencies: [],
72 };
73 }
74
75 useEffect(() => {
76 return () => {
77 initRef.current = null;
78 };
79 }, []);
80
81 const { context, savedDependencies } = initRef.current;
82
83 dependencies = buildDependencies(
84 dependencies,
85 handlers as Record<string, WorkletFunction | undefined>
86 );
87
88 const doDependenciesDiffer = !areDependenciesEqual(
89 dependencies,
90 savedDependencies
91 );
92 initRef.current.savedDependencies = dependencies;
93 const useWeb = isWeb() || isJest();
94
95 return { context, doDependenciesDiffer, useWeb };
96}