UNPKG

4.2 kBPlain TextView Raw
1/* eslint-disable @typescript-eslint/no-redundant-type-constituents */
2'use strict';
3import type { MutableRefObject } from 'react';
4import { processColorsInProps } from './Colors';
5import type { ShadowNodeWrapper, SharedValue, StyleProps } from './commonTypes';
6import type { AnimatedStyle } from './helperTypes';
7import type { Descriptor } from './hook/commonTypes';
8import { _updatePropsJS } from './js-reanimated';
9import { isFabric, isJest, shouldBeUseWeb } from './PlatformChecker';
10import type { ViewRefSet } from './ViewDescriptorsSet';
11import { runOnUIImmediately } from './threads';
12
13let updateProps: (
14 viewDescriptor: SharedValue<Descriptor[]>,
15 updates: StyleProps | AnimatedStyle<any>,
16 maybeViewRef: ViewRefSet<any> | undefined,
17 isAnimatedProps?: boolean
18) => void;
19
20if (shouldBeUseWeb()) {
21 updateProps = (_, updates, maybeViewRef, isAnimatedProps) => {
22 'worklet';
23 if (maybeViewRef) {
24 maybeViewRef.items.forEach((item, _index) => {
25 _updatePropsJS(updates, item, isAnimatedProps);
26 });
27 }
28 };
29} else {
30 updateProps = (viewDescriptors, updates) => {
31 'worklet';
32 processColorsInProps(updates);
33 global.UpdatePropsManager.update(viewDescriptors, updates);
34 };
35}
36
37export const updatePropsJestWrapper = (
38 viewDescriptors: SharedValue<Descriptor[]>,
39 updates: AnimatedStyle<any>,
40 maybeViewRef: ViewRefSet<any> | undefined,
41 animatedStyle: MutableRefObject<AnimatedStyle<any>>,
42 adapters: ((updates: AnimatedStyle<any>) => void)[]
43): void => {
44 adapters.forEach((adapter) => {
45 adapter(updates);
46 });
47 animatedStyle.current.value = {
48 ...animatedStyle.current.value,
49 ...updates,
50 };
51
52 updateProps(viewDescriptors, updates, maybeViewRef);
53};
54
55export default updateProps;
56
57const createUpdatePropsManager = isFabric()
58 ? () => {
59 'worklet';
60 // Fabric
61 const operations: {
62 shadowNodeWrapper: ShadowNodeWrapper;
63 updates: StyleProps | AnimatedStyle<any>;
64 }[] = [];
65 return {
66 update(
67 viewDescriptors: SharedValue<Descriptor[]>,
68 updates: StyleProps | AnimatedStyle<any>
69 ) {
70 viewDescriptors.value.forEach((viewDescriptor) => {
71 operations.push({
72 shadowNodeWrapper: viewDescriptor.shadowNodeWrapper,
73 updates,
74 });
75 if (operations.length === 1) {
76 queueMicrotask(this.flush);
77 }
78 });
79 },
80 flush(this: void) {
81 global._updatePropsFabric!(operations);
82 operations.length = 0;
83 },
84 };
85 }
86 : () => {
87 'worklet';
88 // Paper
89 const operations: {
90 tag: number;
91 name: string;
92 updates: StyleProps | AnimatedStyle<any>;
93 }[] = [];
94 return {
95 update(
96 viewDescriptors: SharedValue<Descriptor[]>,
97 updates: StyleProps | AnimatedStyle<any>
98 ) {
99 viewDescriptors.value.forEach((viewDescriptor) => {
100 operations.push({
101 tag: viewDescriptor.tag,
102 name: viewDescriptor.name || 'RCTView',
103 updates,
104 });
105 if (operations.length === 1) {
106 queueMicrotask(this.flush);
107 }
108 });
109 },
110 flush(this: void) {
111 global._updatePropsPaper!(operations);
112 operations.length = 0;
113 },
114 };
115 };
116
117if (shouldBeUseWeb()) {
118 const maybeThrowError = () => {
119 // Jest attempts to access a property of this object to check if it is a Jest mock
120 // so we can't throw an error in the getter.
121 if (!isJest()) {
122 throw new Error(
123 '[Reanimated] `UpdatePropsManager` is not available on non-native platform.'
124 );
125 }
126 };
127 global.UpdatePropsManager = new Proxy({} as UpdatePropsManager, {
128 get: maybeThrowError,
129 set: () => {
130 maybeThrowError();
131 return false;
132 },
133 });
134} else {
135 runOnUIImmediately(() => {
136 'worklet';
137 global.UpdatePropsManager = createUpdatePropsManager();
138 })();
139}
140
141export interface UpdatePropsManager {
142 update(
143 viewDescriptors: SharedValue<Descriptor[]>,
144 updates: StyleProps | AnimatedStyle<any>
145 ): void;
146 flush(): void;
147}