1 | 'use strict';
|
2 | import { useEffect, useRef } from 'react';
|
3 | import type { WorkletFunction } from '../commonTypes';
|
4 | import { isWeb, isJest } from '../PlatformChecker';
|
5 | import type { DependencyList, ReanimatedEvent } from './commonTypes';
|
6 | import { areDependenciesEqual, buildDependencies } from './utils';
|
7 | import { makeShareable } from '../shareables';
|
8 |
|
9 | interface GeneralHandler<
|
10 | Event extends object,
|
11 | Context extends Record<string, unknown>
|
12 | > {
|
13 | (event: ReanimatedEvent<Event>, context: Context): void;
|
14 | }
|
15 |
|
16 | type GeneralWorkletHandler<
|
17 | Event extends object,
|
18 | Context extends Record<string, unknown>
|
19 | > = WorkletFunction<[event: ReanimatedEvent<Event>, context: Context]>;
|
20 |
|
21 | type GeneralHandlers<
|
22 | Event extends object,
|
23 | Context extends Record<string, unknown>
|
24 | > = Record<string, GeneralHandler<Event, Context> | undefined>;
|
25 |
|
26 | type GeneralWorkletHandlers<
|
27 | Event extends object,
|
28 | Context extends Record<string, unknown>
|
29 | > = Record<string, GeneralWorkletHandler<Event, Context> | undefined>;
|
30 |
|
31 | interface ContextWithDependencies<Context extends Record<string, unknown>> {
|
32 | context: Context;
|
33 | savedDependencies: DependencyList;
|
34 | }
|
35 |
|
36 | export interface UseHandlerContext<Context extends Record<string, unknown>> {
|
37 | context: Context;
|
38 | doDependenciesDiffer: boolean;
|
39 | useWeb: boolean;
|
40 | }
|
41 |
|
42 |
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 | export 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 |
|
59 | export 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 | }
|