1 | 'use strict';
|
2 | import NativeReanimatedModule from './NativeReanimated';
|
3 | import { isWeb, shouldBeUseWeb, isFabric } from './PlatformChecker';
|
4 | import type {
|
5 | AnimatedKeyboardOptions,
|
6 | SensorConfig,
|
7 | SensorType,
|
8 | SharedValue,
|
9 | Value3D,
|
10 | ValueRotation,
|
11 | } from './commonTypes';
|
12 | import { makeShareableCloneRecursive } from './shareables';
|
13 | import { initializeUIRuntime } from './initializers';
|
14 | import type { LayoutAnimationBatchItem } from './layoutReanimation/animationBuilder/commonTypes';
|
15 | import { SensorContainer } from './SensorContainer';
|
16 |
|
17 | export { startMapper, stopMapper } from './mappers';
|
18 | export { runOnJS, runOnUI, executeOnUIRuntimeSync } from './threads';
|
19 | export { createWorkletRuntime, runOnRuntime } from './runtimes';
|
20 | export type { WorkletRuntime } from './runtimes';
|
21 | export { makeShareable, makeShareableCloneRecursive } from './shareables';
|
22 | export { makeMutable } from './mutables';
|
23 |
|
24 | const SHOULD_BE_USE_WEB = shouldBeUseWeb();
|
25 |
|
26 |
|
27 |
|
28 |
|
29 | export const isReanimated3 = () => true;
|
30 |
|
31 |
|
32 |
|
33 |
|
34 |
|
35 |
|
36 |
|
37 |
|
38 |
|
39 |
|
40 | export const isConfigured = isReanimated3;
|
41 |
|
42 |
|
43 | if (SHOULD_BE_USE_WEB) {
|
44 | global._WORKLET = false;
|
45 | global._log = console.log;
|
46 | global._getAnimationTimestamp = () => performance.now();
|
47 | }
|
48 |
|
49 | export function getViewProp<T>(
|
50 | viewTag: number,
|
51 | propName: string,
|
52 | component?: React.Component
|
53 | ): Promise<T> {
|
54 | if (isFabric() && !component) {
|
55 | throw new Error(
|
56 | '[Reanimated] Function `getViewProp` requires a component to be passed as an argument on Fabric.'
|
57 | );
|
58 | }
|
59 |
|
60 |
|
61 | return new Promise((resolve, reject) => {
|
62 | return NativeReanimatedModule.getViewProp(
|
63 | viewTag,
|
64 | propName,
|
65 | component,
|
66 | (result: T) => {
|
67 | if (typeof result === 'string' && result.substr(0, 6) === 'error:') {
|
68 | reject(result);
|
69 | } else {
|
70 | resolve(result);
|
71 | }
|
72 | }
|
73 | );
|
74 | });
|
75 | }
|
76 |
|
77 | function getSensorContainer(): SensorContainer {
|
78 | if (!global.__sensorContainer) {
|
79 | global.__sensorContainer = new SensorContainer();
|
80 | }
|
81 | return global.__sensorContainer;
|
82 | }
|
83 |
|
84 | export function registerEventHandler<T>(
|
85 | eventHandler: (event: T) => void,
|
86 | eventName: string,
|
87 | emitterReactTag = -1
|
88 | ): number {
|
89 | function handleAndFlushAnimationFrame(eventTimestamp: number, event: T) {
|
90 | 'worklet';
|
91 | global.__frameTimestamp = eventTimestamp;
|
92 | eventHandler(event);
|
93 | global.__flushAnimationFrame(eventTimestamp);
|
94 | global.__frameTimestamp = undefined;
|
95 | }
|
96 | return NativeReanimatedModule.registerEventHandler(
|
97 | makeShareableCloneRecursive(handleAndFlushAnimationFrame),
|
98 | eventName,
|
99 | emitterReactTag
|
100 | );
|
101 | }
|
102 |
|
103 | export function unregisterEventHandler(id: number): void {
|
104 | return NativeReanimatedModule.unregisterEventHandler(id);
|
105 | }
|
106 |
|
107 | export function subscribeForKeyboardEvents(
|
108 | eventHandler: (state: number, height: number) => void,
|
109 | options: AnimatedKeyboardOptions
|
110 | ): number {
|
111 |
|
112 |
|
113 | function handleAndFlushAnimationFrame(state: number, height: number) {
|
114 | 'worklet';
|
115 | const now = global._getAnimationTimestamp();
|
116 | global.__frameTimestamp = now;
|
117 | eventHandler(state, height);
|
118 | global.__flushAnimationFrame(now);
|
119 | global.__frameTimestamp = undefined;
|
120 | }
|
121 | return NativeReanimatedModule.subscribeForKeyboardEvents(
|
122 | makeShareableCloneRecursive(handleAndFlushAnimationFrame),
|
123 | options.isStatusBarTranslucentAndroid ?? false
|
124 | );
|
125 | }
|
126 |
|
127 | export function unsubscribeFromKeyboardEvents(listenerId: number): void {
|
128 | return NativeReanimatedModule.unsubscribeFromKeyboardEvents(listenerId);
|
129 | }
|
130 |
|
131 | export function registerSensor(
|
132 | sensorType: SensorType,
|
133 | config: SensorConfig,
|
134 | eventHandler: (
|
135 | data: Value3D | ValueRotation,
|
136 | orientationDegrees: number
|
137 | ) => void
|
138 | ): number {
|
139 | const sensorContainer = getSensorContainer();
|
140 | return sensorContainer.registerSensor(
|
141 | sensorType,
|
142 | config,
|
143 | makeShareableCloneRecursive(eventHandler)
|
144 | );
|
145 | }
|
146 |
|
147 | export function initializeSensor(
|
148 | sensorType: SensorType,
|
149 | config: SensorConfig
|
150 | ): SharedValue<Value3D | ValueRotation> {
|
151 | const sensorContainer = getSensorContainer();
|
152 | return sensorContainer.initializeSensor(sensorType, config);
|
153 | }
|
154 |
|
155 | export function unregisterSensor(sensorId: number): void {
|
156 | const sensorContainer = getSensorContainer();
|
157 | return sensorContainer.unregisterSensor(sensorId);
|
158 | }
|
159 |
|
160 | if (!isWeb()) {
|
161 | initializeUIRuntime();
|
162 | }
|
163 |
|
164 | type FeaturesConfig = {
|
165 | enableLayoutAnimations: boolean;
|
166 | setByUser: boolean;
|
167 | };
|
168 |
|
169 | let featuresConfig: FeaturesConfig = {
|
170 | enableLayoutAnimations: false,
|
171 | setByUser: false,
|
172 | };
|
173 |
|
174 | export function enableLayoutAnimations(
|
175 | flag: boolean,
|
176 | isCallByUser = true
|
177 | ): void {
|
178 | if (isCallByUser) {
|
179 | featuresConfig = {
|
180 | enableLayoutAnimations: flag,
|
181 | setByUser: true,
|
182 | };
|
183 | NativeReanimatedModule.enableLayoutAnimations(flag);
|
184 | } else if (
|
185 | !featuresConfig.setByUser &&
|
186 | featuresConfig.enableLayoutAnimations !== flag
|
187 | ) {
|
188 | featuresConfig.enableLayoutAnimations = flag;
|
189 | NativeReanimatedModule.enableLayoutAnimations(flag);
|
190 | }
|
191 | }
|
192 |
|
193 | export function configureLayoutAnimationBatch(
|
194 | layoutAnimationsBatch: LayoutAnimationBatchItem[]
|
195 | ): void {
|
196 | NativeReanimatedModule.configureLayoutAnimationBatch(layoutAnimationsBatch);
|
197 | }
|
198 |
|
199 | export function setShouldAnimateExitingForTag(
|
200 | viewTag: number | HTMLElement,
|
201 | shouldAnimate: boolean
|
202 | ) {
|
203 | NativeReanimatedModule.setShouldAnimateExitingForTag(
|
204 | viewTag as number,
|
205 | shouldAnimate
|
206 | );
|
207 | }
|
208 |
|
209 | export function jsiConfigureProps(
|
210 | uiProps: string[],
|
211 | nativeProps: string[]
|
212 | ): void {
|
213 | if (!SHOULD_BE_USE_WEB) {
|
214 | NativeReanimatedModule.configureProps(uiProps, nativeProps);
|
215 | }
|
216 | }
|