react-native-reanimated 3.0.0-rc.7 → 3.0.0-rc.8

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (177) hide show
  1. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.cpp +32 -37
  2. package/Common/cpp/AnimatedSensor/AnimatedSensorModule.h +4 -3
  3. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.cpp +76 -0
  4. package/Common/cpp/LayoutAnimations/LayoutAnimationsManager.h +41 -0
  5. package/Common/cpp/NativeModules/NativeReanimatedModule.cpp +161 -165
  6. package/Common/cpp/NativeModules/NativeReanimatedModule.h +36 -34
  7. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.cpp +22 -34
  8. package/Common/cpp/NativeModules/NativeReanimatedModuleSpec.h +13 -14
  9. package/Common/cpp/SharedItems/RuntimeManager.h +1 -34
  10. package/Common/cpp/SharedItems/Shareables.cpp +142 -0
  11. package/Common/cpp/SharedItems/Shareables.h +455 -0
  12. package/Common/cpp/Tools/PlatformDepMethodsHolder.h +6 -0
  13. package/Common/cpp/Tools/RuntimeDecorator.cpp +54 -26
  14. package/Common/cpp/Tools/RuntimeDecorator.h +11 -2
  15. package/Common/cpp/Tools/Scheduler.cpp +10 -0
  16. package/Common/cpp/Tools/SingleInstanceChecker.h +1 -1
  17. package/android/src/main/cpp/LayoutAnimations.cpp +6 -16
  18. package/android/src/main/cpp/LayoutAnimations.h +4 -4
  19. package/android/src/main/cpp/NativeProxy.cpp +50 -63
  20. package/ios/native/NativeProxy.mm +26 -42
  21. package/lib/commonjs/createAnimatedComponent.js +7 -11
  22. package/lib/commonjs/createAnimatedComponent.js.map +1 -1
  23. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  24. package/lib/commonjs/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  25. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js +27 -55
  26. package/lib/commonjs/reanimated2/ViewDescriptorsSet.js.map +1 -1
  27. package/lib/commonjs/reanimated2/WorkletEventHandler.js +5 -3
  28. package/lib/commonjs/reanimated2/WorkletEventHandler.js.map +1 -1
  29. package/lib/commonjs/reanimated2/animation/util.js.map +1 -1
  30. package/lib/commonjs/reanimated2/commonTypes.js +7 -0
  31. package/lib/commonjs/reanimated2/commonTypes.js.map +1 -1
  32. package/lib/commonjs/reanimated2/core.js +111 -209
  33. package/lib/commonjs/reanimated2/core.js.map +1 -1
  34. package/lib/commonjs/reanimated2/globals.d.js.map +1 -1
  35. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js +15 -9
  36. package/lib/commonjs/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  37. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js +18 -8
  38. package/lib/commonjs/reanimated2/hook/useAnimatedRef.js.map +1 -1
  39. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js +27 -36
  40. package/lib/commonjs/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  41. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js +24 -50
  42. package/lib/commonjs/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  43. package/lib/commonjs/reanimated2/hook/useSharedValue.js +3 -2
  44. package/lib/commonjs/reanimated2/hook/useSharedValue.js.map +1 -1
  45. package/lib/commonjs/reanimated2/hook/utils.js.map +1 -1
  46. package/lib/commonjs/reanimated2/interpolateColor.js.map +1 -1
  47. package/lib/commonjs/reanimated2/jestUtils.js +8 -33
  48. package/lib/commonjs/reanimated2/jestUtils.js.map +1 -1
  49. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js +16 -100
  50. package/lib/commonjs/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  51. package/lib/commonjs/reanimated2/js-reanimated/index.js +5 -0
  52. package/lib/commonjs/reanimated2/js-reanimated/index.js.map +1 -1
  53. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  54. package/lib/commonjs/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  55. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js +91 -0
  56. package/lib/commonjs/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  57. package/lib/commonjs/reanimated2/layoutReanimation/index.js +1 -1
  58. package/lib/commonjs/reanimated2/layoutReanimation/index.js.map +1 -1
  59. package/lib/commonjs/reanimated2/mappers.js +186 -0
  60. package/lib/commonjs/reanimated2/mappers.js.map +1 -0
  61. package/lib/commonjs/reanimated2/mutables.js +173 -0
  62. package/lib/commonjs/reanimated2/mutables.js.map +1 -0
  63. package/lib/commonjs/reanimated2/shareables.js +153 -0
  64. package/lib/commonjs/reanimated2/shareables.js.map +1 -0
  65. package/lib/commonjs/reanimated2/threads.js +58 -0
  66. package/lib/commonjs/reanimated2/threads.js.map +1 -0
  67. package/lib/commonjs/reanimated2/time.js +55 -0
  68. package/lib/commonjs/reanimated2/time.js.map +1 -0
  69. package/lib/commonjs/reanimated2/valueSetter.js +81 -0
  70. package/lib/commonjs/reanimated2/valueSetter.js.map +1 -0
  71. package/lib/module/createAnimatedComponent.js +7 -12
  72. package/lib/module/createAnimatedComponent.js.map +1 -1
  73. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js +24 -25
  74. package/lib/module/reanimated2/NativeReanimated/NativeReanimated.js.map +1 -1
  75. package/lib/module/reanimated2/ViewDescriptorsSet.js +27 -53
  76. package/lib/module/reanimated2/ViewDescriptorsSet.js.map +1 -1
  77. package/lib/module/reanimated2/WorkletEventHandler.js +4 -3
  78. package/lib/module/reanimated2/WorkletEventHandler.js.map +1 -1
  79. package/lib/module/reanimated2/animation/util.js.map +1 -1
  80. package/lib/module/reanimated2/commonTypes.js +7 -0
  81. package/lib/module/reanimated2/commonTypes.js.map +1 -1
  82. package/lib/module/reanimated2/core.js +66 -188
  83. package/lib/module/reanimated2/core.js.map +1 -1
  84. package/lib/module/reanimated2/globals.d.js.map +1 -1
  85. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js +16 -6
  86. package/lib/module/reanimated2/hook/useAnimatedKeyboard.js.map +1 -1
  87. package/lib/module/reanimated2/hook/useAnimatedRef.js +17 -8
  88. package/lib/module/reanimated2/hook/useAnimatedRef.js.map +1 -1
  89. package/lib/module/reanimated2/hook/useAnimatedSensor.js +29 -34
  90. package/lib/module/reanimated2/hook/useAnimatedSensor.js.map +1 -1
  91. package/lib/module/reanimated2/hook/useAnimatedStyle.js +26 -52
  92. package/lib/module/reanimated2/hook/useAnimatedStyle.js.map +1 -1
  93. package/lib/module/reanimated2/hook/useSharedValue.js +3 -2
  94. package/lib/module/reanimated2/hook/useSharedValue.js.map +1 -1
  95. package/lib/module/reanimated2/hook/utils.js.map +1 -1
  96. package/lib/module/reanimated2/interpolateColor.js.map +1 -1
  97. package/lib/module/reanimated2/jestUtils.js +8 -31
  98. package/lib/module/reanimated2/jestUtils.js.map +1 -1
  99. package/lib/module/reanimated2/js-reanimated/JSReanimated.js +16 -95
  100. package/lib/module/reanimated2/js-reanimated/JSReanimated.js.map +1 -1
  101. package/lib/module/reanimated2/js-reanimated/index.js +5 -0
  102. package/lib/module/reanimated2/js-reanimated/index.js.map +1 -1
  103. package/lib/module/reanimated2/layoutReanimation/animationBuilder/commonTypes.js.map +1 -1
  104. package/lib/module/reanimated2/layoutReanimation/animationBuilder/index.js.map +1 -1
  105. package/lib/module/reanimated2/layoutReanimation/animationsManager.js +84 -0
  106. package/lib/module/reanimated2/layoutReanimation/animationsManager.js.map +1 -0
  107. package/lib/module/reanimated2/layoutReanimation/index.js +1 -1
  108. package/lib/module/reanimated2/layoutReanimation/index.js.map +1 -1
  109. package/lib/module/reanimated2/mappers.js +173 -0
  110. package/lib/module/reanimated2/mappers.js.map +1 -0
  111. package/lib/module/reanimated2/mutables.js +149 -0
  112. package/lib/module/reanimated2/mutables.js.map +1 -0
  113. package/lib/module/reanimated2/shareables.js +133 -0
  114. package/lib/module/reanimated2/shareables.js.map +1 -0
  115. package/lib/module/reanimated2/threads.js +45 -0
  116. package/lib/module/reanimated2/threads.js.map +1 -0
  117. package/lib/module/reanimated2/time.js +37 -0
  118. package/lib/module/reanimated2/time.js.map +1 -0
  119. package/lib/module/reanimated2/valueSetter.js +66 -0
  120. package/lib/module/reanimated2/valueSetter.js.map +1 -0
  121. package/package.json +1 -1
  122. package/plugin.js +31 -15
  123. package/src/createAnimatedComponent.tsx +8 -24
  124. package/src/reanimated2/NativeReanimated/NativeReanimated.ts +34 -53
  125. package/src/reanimated2/ViewDescriptorsSet.ts +30 -69
  126. package/src/reanimated2/WorkletEventHandler.ts +4 -11
  127. package/src/reanimated2/animation/util.ts +1 -0
  128. package/src/reanimated2/commonTypes.ts +29 -4
  129. package/src/reanimated2/core.ts +103 -230
  130. package/src/reanimated2/globals.d.ts +26 -26
  131. package/src/reanimated2/hook/useAnimatedKeyboard.ts +18 -9
  132. package/src/reanimated2/hook/useAnimatedRef.ts +27 -9
  133. package/src/reanimated2/hook/useAnimatedSensor.ts +33 -31
  134. package/src/reanimated2/hook/useAnimatedStyle.ts +26 -64
  135. package/src/reanimated2/hook/useSharedValue.ts +6 -3
  136. package/src/reanimated2/hook/utils.ts +1 -1
  137. package/src/reanimated2/interpolateColor.ts +2 -2
  138. package/src/reanimated2/jestUtils.ts +14 -29
  139. package/src/reanimated2/js-reanimated/JSReanimated.ts +19 -93
  140. package/src/reanimated2/js-reanimated/index.ts +4 -0
  141. package/src/reanimated2/layoutReanimation/animationBuilder/commonTypes.ts +7 -0
  142. package/src/reanimated2/layoutReanimation/animationBuilder/index.ts +1 -0
  143. package/src/reanimated2/layoutReanimation/animationsManager.ts +94 -0
  144. package/src/reanimated2/layoutReanimation/index.ts +1 -1
  145. package/src/reanimated2/mappers.ts +179 -0
  146. package/src/reanimated2/mutables.ts +151 -0
  147. package/src/reanimated2/shareables.ts +121 -0
  148. package/src/reanimated2/threads.ts +46 -0
  149. package/src/reanimated2/time.ts +30 -0
  150. package/src/reanimated2/valueSetter.ts +64 -0
  151. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.cpp +0 -127
  152. package/Common/cpp/LayoutAnimations/LayoutAnimationsProxy.h +0 -56
  153. package/Common/cpp/Registries/MapperRegistry.cpp +0 -147
  154. package/Common/cpp/Registries/MapperRegistry.h +0 -29
  155. package/Common/cpp/Registries/WorkletsCache.cpp +0 -45
  156. package/Common/cpp/Registries/WorkletsCache.h +0 -24
  157. package/Common/cpp/SharedItems/FrozenObject.cpp +0 -35
  158. package/Common/cpp/SharedItems/FrozenObject.h +0 -36
  159. package/Common/cpp/SharedItems/HostFunctionHandler.h +0 -28
  160. package/Common/cpp/SharedItems/MutableValue.cpp +0 -134
  161. package/Common/cpp/SharedItems/MutableValue.h +0 -54
  162. package/Common/cpp/SharedItems/MutableValueSetterProxy.cpp +0 -47
  163. package/Common/cpp/SharedItems/MutableValueSetterProxy.h +0 -25
  164. package/Common/cpp/SharedItems/RemoteObject.cpp +0 -45
  165. package/Common/cpp/SharedItems/RemoteObject.h +0 -35
  166. package/Common/cpp/SharedItems/ShareableValue.cpp +0 -529
  167. package/Common/cpp/SharedItems/ShareableValue.h +0 -63
  168. package/Common/cpp/SharedItems/ValueWrapper.h +0 -183
  169. package/Common/cpp/Tools/JSIStoreValueUser.cpp +0 -55
  170. package/Common/cpp/Tools/JSIStoreValueUser.h +0 -37
  171. package/Common/cpp/Tools/Mapper.cpp +0 -86
  172. package/Common/cpp/Tools/Mapper.h +0 -51
  173. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -58
  174. package/lib/commonjs/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  175. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js +0 -52
  176. package/lib/module/reanimated2/layoutReanimation/LayoutAnimationRepository.js.map +0 -1
  177. package/src/reanimated2/layoutReanimation/LayoutAnimationRepository.ts +0 -49
@@ -1,4 +1,4 @@
1
- import './LayoutAnimationRepository';
1
+ import './animationsManager';
2
2
  export * from './animationBuilder';
3
3
  export * from './defaultAnimations';
4
4
  export * from './defaultTransitions';
@@ -0,0 +1,179 @@
1
+ import { SharedValue } from './commonTypes';
2
+ import { runOnUI } from './threads';
3
+
4
+ export type Mapper = {
5
+ id: number;
6
+ dirty: boolean;
7
+ worklet: () => void;
8
+ inputs: SharedValue<any>[];
9
+ outputs?: SharedValue<any>[];
10
+ };
11
+
12
+ export function createMapperRegistry() {
13
+ 'worklet';
14
+ const mappers = new Map();
15
+ let sortedMappers: Mapper[] = [];
16
+
17
+ let frameRequested = false;
18
+
19
+ function updateMappersOrder() {
20
+ // sort mappers topologically
21
+ // the algorithm here takes adventage of a fact that the topological order
22
+ // of a transposed graph is a reverse topological order of the original graph
23
+ // The graph in our case consists of mappers and an edge between two mappers
24
+ // A and B exists if there is a shared value that's on A's output lists and on
25
+ // B's input list.
26
+ //
27
+ // We don't need however to calculate that graph as it is easier to work with
28
+ // the transposed version of it that can be calculated ad-hoc. For the transposed
29
+ // version to be traversed we use "pre" map that maps share value to mappers that
30
+ // output that shared value. Then we can infer all the outgoing edges for a given
31
+ // mapper simply by scanning it's input list and checking if any of the shared values
32
+ // from that list exists in the "pre" map. If they do, then we have an edge between
33
+ // that mapper and the mappers from the "pre" list for the given shared value.
34
+ //
35
+ // For topological sorting we use a dfs-based approach that requires the graph to
36
+ // be traversed in dfs order and each node after being processed lands at the
37
+ // beginning of the topological order list. Since we traverse a transposed graph,
38
+ // instead of reversing that order we can use a normal array and push processed
39
+ // mappers to the end. There is no need to reverse that array after we are done.
40
+ const pre = new Map(); // map from sv -> mapper that outputs that sv
41
+ mappers.forEach((mapper) => {
42
+ if (mapper.outputs) {
43
+ for (const output of mapper.outputs) {
44
+ const preMappers = pre.get(output);
45
+ if (preMappers === undefined) {
46
+ pre.set(output, [mapper]);
47
+ } else {
48
+ preMappers.push(mapper);
49
+ }
50
+ }
51
+ }
52
+ });
53
+ const visited = new Set();
54
+ const newOrder: Mapper[] = [];
55
+ function dfs(mapper: Mapper) {
56
+ visited.add(mapper);
57
+ for (const input of mapper.inputs) {
58
+ const preMappers = pre.get(input);
59
+ if (preMappers) {
60
+ for (const preMapper of preMappers) {
61
+ if (!visited.has(preMapper)) {
62
+ dfs(preMapper);
63
+ }
64
+ }
65
+ }
66
+ }
67
+ newOrder.push(mapper);
68
+ }
69
+ mappers.forEach((mapper) => {
70
+ if (!visited.has(mapper)) {
71
+ dfs(mapper);
72
+ }
73
+ });
74
+ sortedMappers = newOrder;
75
+ }
76
+
77
+ function mapperFrame() {
78
+ frameRequested = false;
79
+ if (mappers.size !== sortedMappers.length) {
80
+ updateMappersOrder();
81
+ }
82
+ for (const mapper of sortedMappers) {
83
+ if (mapper.dirty) {
84
+ mapper.dirty = false;
85
+ mapper.worklet();
86
+ }
87
+ }
88
+ }
89
+
90
+ function maybeRequestUpdates() {
91
+ if (!frameRequested) {
92
+ requestAnimationFrame(mapperFrame);
93
+ frameRequested = true;
94
+ }
95
+ }
96
+
97
+ function extractInputs(
98
+ inputs: any,
99
+ resultArray: SharedValue<any>[]
100
+ ): SharedValue<any>[] {
101
+ if (Array.isArray(inputs)) {
102
+ for (const input of inputs) {
103
+ input && extractInputs(input, resultArray);
104
+ }
105
+ } else if (inputs.addListener) {
106
+ resultArray.push(inputs);
107
+ } else if (typeof inputs === 'object') {
108
+ for (const element of Object.values(inputs)) {
109
+ element && extractInputs(element, resultArray);
110
+ }
111
+ }
112
+ return resultArray;
113
+ }
114
+
115
+ return {
116
+ start: (
117
+ mapperID: number,
118
+ worklet: () => void,
119
+ inputs: SharedValue<any>[],
120
+ outputs?: SharedValue<any>[]
121
+ ) => {
122
+ const mapper = {
123
+ id: mapperID,
124
+ dirty: true,
125
+ worklet,
126
+ inputs: extractInputs(inputs, []),
127
+ outputs,
128
+ };
129
+ mappers.set(mapper.id, mapper);
130
+ sortedMappers = [];
131
+ for (const sv of mapper.inputs) {
132
+ sv.addListener(mapper.id, () => {
133
+ mapper.dirty = true;
134
+ maybeRequestUpdates();
135
+ });
136
+ }
137
+ maybeRequestUpdates();
138
+ },
139
+ stop: (mapperID: number) => {
140
+ const mapper = mappers.get(mapperID);
141
+ if (mapper) {
142
+ mappers.delete(mapper.id);
143
+ sortedMappers = [];
144
+ for (const sv of mapper.inputs) {
145
+ sv.removeListener(mapper.id);
146
+ }
147
+ }
148
+ },
149
+ };
150
+ }
151
+
152
+ let MAPPER_ID = 9999;
153
+
154
+ export function startMapper(
155
+ worklet: () => void,
156
+ inputs: any[] = [],
157
+ outputs: any[] = []
158
+ ): number {
159
+ const mapperID = (MAPPER_ID += 1);
160
+
161
+ runOnUI(() => {
162
+ 'worklet';
163
+ let mapperRegistry = global.__mapperRegistry;
164
+ if (mapperRegistry === undefined) {
165
+ mapperRegistry = global.__mapperRegistry = createMapperRegistry();
166
+ }
167
+ mapperRegistry.start(mapperID, worklet, inputs, outputs);
168
+ })();
169
+
170
+ return mapperID;
171
+ }
172
+
173
+ export function stopMapper(mapperID: number): void {
174
+ runOnUI(() => {
175
+ 'worklet';
176
+ const mapperRegistry = global.__mapperRegistry;
177
+ mapperRegistry?.stop(mapperID);
178
+ })();
179
+ }
@@ -0,0 +1,151 @@
1
+ import NativeReanimatedModule from './NativeReanimated';
2
+ import { SharedValue, ShareableSyncDataHolderRef } from './commonTypes';
3
+ import {
4
+ makeShareableCloneOnUIRecursive,
5
+ makeShareableCloneRecursive,
6
+ registerShareableMapping,
7
+ } from './shareables';
8
+ import { runOnUI } from './threads';
9
+ import { valueSetter } from './valueSetter';
10
+ export { stopMapper } from './mappers';
11
+
12
+ export function makeUIMutable<T>(
13
+ initial: T,
14
+ syncDataHolder?: ShareableSyncDataHolderRef<T>
15
+ ) {
16
+ 'worklet';
17
+
18
+ const listeners = new Map();
19
+ let value = initial;
20
+
21
+ const self = {
22
+ set value(newValue) {
23
+ valueSetter(self, newValue);
24
+ },
25
+ get value() {
26
+ return value;
27
+ },
28
+ /**
29
+ * _value prop should only be accessed by the valueSetter implementation
30
+ * which may make the decision about updating the mutable value depending
31
+ * on the provided new value. All other places should only attempt to modify
32
+ * the mutable by assigning to value prop directly.
33
+ */
34
+ set _value(newValue: T) {
35
+ value = newValue;
36
+ if (syncDataHolder) {
37
+ _updateDataSynchronously(
38
+ syncDataHolder,
39
+ makeShareableCloneOnUIRecursive(newValue)
40
+ );
41
+ }
42
+ listeners.forEach((listener) => {
43
+ listener(newValue);
44
+ });
45
+ },
46
+ get _value(): T {
47
+ return value;
48
+ },
49
+ addListener: (id: number, listener: (newValue: T) => void) => {
50
+ listeners.set(id, listener);
51
+ },
52
+ removeListener: (id: number) => {
53
+ listeners.delete(id);
54
+ },
55
+ _animation: null,
56
+ };
57
+ return self;
58
+ }
59
+
60
+ export function makeMutable<T>(
61
+ initial: T,
62
+ oneWayReadsOnly = false
63
+ ): SharedValue<T> {
64
+ let value: T = initial;
65
+ let syncDataHolder: ShareableSyncDataHolderRef<T> | undefined;
66
+ if (!oneWayReadsOnly && NativeReanimatedModule.native) {
67
+ // updates are always synchronous when running on web or in Jest environment
68
+ syncDataHolder = NativeReanimatedModule.makeSynchronizedDataHolder(
69
+ makeShareableCloneRecursive(value)
70
+ );
71
+ registerShareableMapping(syncDataHolder);
72
+ }
73
+ const handle = makeShareableCloneRecursive({
74
+ __init: () => {
75
+ 'worklet';
76
+ return makeUIMutable(initial, syncDataHolder);
77
+ },
78
+ });
79
+ // listeners can only work on JS thread on Web and jest environments
80
+ const listeners = NativeReanimatedModule.native ? undefined : new Map();
81
+ const mutable = {
82
+ set value(newValue) {
83
+ if (NativeReanimatedModule.native) {
84
+ runOnUI(() => {
85
+ 'worklet';
86
+ mutable.value = newValue;
87
+ })();
88
+ } else {
89
+ valueSetter(mutable, newValue);
90
+ }
91
+ },
92
+ get value() {
93
+ if (syncDataHolder) {
94
+ return NativeReanimatedModule.getDataSynchronously(syncDataHolder);
95
+ }
96
+ return value;
97
+ },
98
+ set _value(newValue: T) {
99
+ if (NativeReanimatedModule.native) {
100
+ throw new Error(
101
+ 'Setting `_value` directly is only possible on the UI runtime'
102
+ );
103
+ }
104
+ value = newValue;
105
+ listeners!.forEach((listener) => {
106
+ listener(newValue);
107
+ });
108
+ },
109
+ get _value(): T {
110
+ if (NativeReanimatedModule.native) {
111
+ throw new Error(
112
+ 'Reading from `_value` directly is only possible on the UI runtime'
113
+ );
114
+ }
115
+ return value;
116
+ },
117
+ modify: (modifier: (value: T) => T) => {
118
+ runOnUI(() => {
119
+ 'worklet';
120
+ mutable.value = modifier(mutable.value);
121
+ })();
122
+ },
123
+ addListener: (id: number, listener: (value: T) => void) => {
124
+ if (NativeReanimatedModule.native) {
125
+ throw new Error('adding listeners is only possible on the UI runtime');
126
+ }
127
+ listeners!.set(id, listener);
128
+ },
129
+ removeListener: (id: number) => {
130
+ if (NativeReanimatedModule.native) {
131
+ throw new Error(
132
+ 'removing listeners is only possible on the UI runtime'
133
+ );
134
+ }
135
+ listeners!.delete(id);
136
+ },
137
+ };
138
+ registerShareableMapping(mutable, handle);
139
+ return mutable;
140
+ }
141
+
142
+ export function makeRemote<T extends object>(initial: T = {} as T): T {
143
+ const handle = makeShareableCloneRecursive({
144
+ __init: () => {
145
+ 'worklet';
146
+ return initial;
147
+ },
148
+ });
149
+ registerShareableMapping(initial, handle);
150
+ return initial;
151
+ }
@@ -0,0 +1,121 @@
1
+ import NativeReanimatedModule from './NativeReanimated';
2
+ import { ShareableRef } from './commonTypes';
3
+ import { shouldBeUseWeb } from './PlatformChecker';
4
+
5
+ // for web/chrome debugger/jest environments this file provides a stub implementation
6
+ // where no shareable references are used. Instead, the objects themselves are used
7
+ // instead of shareable references, because of the fact that we don't have to deal with
8
+ // runnning the code on separate VMs.
9
+ const USE_STUB_IMPLEMENTATION = shouldBeUseWeb();
10
+
11
+ const _shareableCache = new WeakMap<
12
+ Record<string, unknown>,
13
+ ShareableRef<any> | symbol
14
+ >();
15
+ // the below symbol is used to represent a mapping from the value to itself
16
+ // this is used to allow for a converted shareable to be passed to makeShareableClone
17
+ const _shareableFlag = Symbol('shareable flag');
18
+
19
+ export function registerShareableMapping(
20
+ shareable: any,
21
+ shareableRef?: ShareableRef<any>
22
+ ): void {
23
+ if (USE_STUB_IMPLEMENTATION) {
24
+ return;
25
+ }
26
+ _shareableCache.set(shareable, shareableRef || _shareableFlag);
27
+ }
28
+
29
+ export function makeShareableShadowNodeWrapper<T>(shadowNodeWrapper: T): T {
30
+ const adoptedSNW =
31
+ NativeReanimatedModule.makeShareableClone(shadowNodeWrapper);
32
+ registerShareableMapping(shadowNodeWrapper, adoptedSNW);
33
+ return shadowNodeWrapper;
34
+ }
35
+
36
+ export function makeShareableCloneRecursive<T>(value: any): ShareableRef<T> {
37
+ if (USE_STUB_IMPLEMENTATION) {
38
+ return value;
39
+ }
40
+ // This one actually may be worth to be moved to c++, we also need similar logic to run on the UI thread
41
+ const type = typeof value;
42
+ if ((type === 'object' || type === 'function') && value !== null) {
43
+ const cached = _shareableCache.get(value);
44
+ if (cached === _shareableFlag) {
45
+ return value;
46
+ } else if (cached !== undefined) {
47
+ return cached as ShareableRef<T>;
48
+ } else {
49
+ let toAdapt: any;
50
+ if (Array.isArray(value)) {
51
+ toAdapt = value.map((element) => makeShareableCloneRecursive(element));
52
+ } else if (type === 'function' && value.__workletHash === undefined) {
53
+ // this is a remote function
54
+ toAdapt = value;
55
+ } else {
56
+ toAdapt = {};
57
+ for (const [key, element] of Object.entries(value)) {
58
+ toAdapt[key] = makeShareableCloneRecursive(element);
59
+ }
60
+ }
61
+ if (__DEV__) {
62
+ // we freeze objects that are transformed to shareable. This should help
63
+ // detect issues when someone modifies data after it's been converted to
64
+ // shareable. Meaning that they may be doing a faulty assumption in their
65
+ // code expecting that the updates are going to automatically populate to
66
+ // the object sent to the UI thread. If the user really wants some objects
67
+ // to be mutable they should use share values instead.
68
+ Object.freeze(value);
69
+ }
70
+ const adopted = NativeReanimatedModule.makeShareableClone(toAdapt);
71
+ _shareableCache.set(value, adopted);
72
+ _shareableCache.set(adopted, _shareableFlag);
73
+ return adopted;
74
+ }
75
+ }
76
+ return NativeReanimatedModule.makeShareableClone(value);
77
+ }
78
+
79
+ export function makeShareableCloneOnUIRecursive<T>(value: T): ShareableRef<T> {
80
+ 'worklet';
81
+ if (USE_STUB_IMPLEMENTATION) {
82
+ // @ts-ignore web is an interesting place where we don't run a secondary VM on the UI thread
83
+ // see more details in the comment where USE_STUB_IMPLEMENTATION is defined.
84
+ return value;
85
+ }
86
+ function cloneRecursive<T>(value: T): ShareableRef<T> {
87
+ const type = typeof value;
88
+ if ((type === 'object' || type === 'function') && value !== null) {
89
+ let toAdapt: any;
90
+ if (Array.isArray(value)) {
91
+ toAdapt = value.map((element) => cloneRecursive(element));
92
+ } else {
93
+ toAdapt = {};
94
+ for (const [key, element] of Object.entries(value)) {
95
+ toAdapt[key] = cloneRecursive(element);
96
+ }
97
+ }
98
+ if (__DEV__) {
99
+ // See the reasoning behind freezing in the other comment above.
100
+ Object.freeze(value);
101
+ }
102
+ return _makeShareableClone(toAdapt);
103
+ }
104
+ return _makeShareableClone(value);
105
+ }
106
+ return cloneRecursive(value);
107
+ }
108
+
109
+ export function makeShareable<T>(value: T): T {
110
+ if (USE_STUB_IMPLEMENTATION) {
111
+ return value;
112
+ }
113
+ const handle = makeShareableCloneRecursive({
114
+ __init: () => {
115
+ 'worklet';
116
+ return value;
117
+ },
118
+ });
119
+ registerShareableMapping(value, handle);
120
+ return value;
121
+ }
@@ -0,0 +1,46 @@
1
+ import NativeReanimatedModule from './NativeReanimated';
2
+ import { ComplexWorkletFunction } from './commonTypes';
3
+ import {
4
+ makeShareableCloneOnUIRecursive,
5
+ makeShareableCloneRecursive,
6
+ } from './shareables';
7
+
8
+ export function runOnUI<A extends any[], R>(
9
+ worklet: ComplexWorkletFunction<A, R>
10
+ ): (...args: A) => void {
11
+ if (__DEV__) {
12
+ if (worklet.__workletHash === undefined) {
13
+ throw new Error('runOnUI() can only be used on worklets');
14
+ }
15
+ }
16
+ return (...args) => {
17
+ NativeReanimatedModule.scheduleOnUI(
18
+ makeShareableCloneRecursive(() => {
19
+ 'worklet';
20
+ return worklet(...args);
21
+ })
22
+ );
23
+ };
24
+ }
25
+
26
+ export function runOnJS<A extends any[], R>(
27
+ fun: ComplexWorkletFunction<A, R>
28
+ ): (...args: A) => void {
29
+ 'worklet';
30
+ if (fun.__remoteFunction) {
31
+ // in development mode the function provided as `fun` throws an error message
32
+ // such that when someone accidently calls it directly on the UI runtime, they
33
+ // see that they should use `runOnJS` instead. To facilitate that we purt the
34
+ // reference to the original remote function in the `__remoteFunction` property.
35
+ fun = fun.__remoteFunction;
36
+ }
37
+ if (!_WORKLET) {
38
+ return fun;
39
+ }
40
+ return (...args) => {
41
+ _scheduleOnJS(
42
+ fun,
43
+ args.length > 0 ? makeShareableCloneOnUIRecursive(args) : undefined
44
+ );
45
+ };
46
+ }
@@ -0,0 +1,30 @@
1
+ import NativeReanimatedModule from './NativeReanimated';
2
+ import { Platform } from 'react-native';
3
+ import { nativeShouldBeMock } from './PlatformChecker';
4
+ export { stopMapper } from './mappers';
5
+
6
+ let _getTimestamp: () => number;
7
+ if (nativeShouldBeMock()) {
8
+ _getTimestamp = () => {
9
+ return NativeReanimatedModule.getTimestamp();
10
+ };
11
+ } else {
12
+ _getTimestamp = () => {
13
+ 'worklet';
14
+ if (_frameTimestamp) {
15
+ return _frameTimestamp;
16
+ }
17
+ if (_eventTimestamp) {
18
+ return _eventTimestamp;
19
+ }
20
+ return _getCurrentTime();
21
+ };
22
+ }
23
+
24
+ export function getTimestamp(): number {
25
+ 'worklet';
26
+ if (Platform.OS === 'web') {
27
+ return NativeReanimatedModule.getTimestamp();
28
+ }
29
+ return _getTimestamp();
30
+ }
@@ -0,0 +1,64 @@
1
+ import { AnimationObject, AnimatableValue } from './commonTypes';
2
+ import { Descriptor } from './hook/commonTypes';
3
+ import { getTimestamp } from './time';
4
+ export { stopMapper } from './mappers';
5
+
6
+ export function valueSetter(sv: any, value: any): void {
7
+ 'worklet';
8
+ const previousAnimation = sv._animation;
9
+ if (previousAnimation) {
10
+ previousAnimation.cancelled = true;
11
+ sv._animation = null;
12
+ }
13
+ if (
14
+ typeof value === 'function' ||
15
+ (value !== null &&
16
+ typeof value === 'object' &&
17
+ (value as AnimationObject).onFrame !== undefined)
18
+ ) {
19
+ const animation: AnimationObject =
20
+ typeof value === 'function'
21
+ ? (value as () => AnimationObject)()
22
+ : (value as AnimationObject);
23
+ // prevent setting again to the same value
24
+ // and triggering the mappers that treat this value as an input
25
+ // this happens when the animation's target value(stored in animation.current until animation.onStart is called) is set to the same value as a current one(this._value)
26
+ // built in animations that are not higher order(withTiming, withSpring) hold target value in .current
27
+ if (sv._value === animation.current && !animation.isHigherOrder) {
28
+ animation.callback && animation.callback(true);
29
+ return;
30
+ }
31
+ // animated set
32
+ const initializeAnimation = (timestamp: number) => {
33
+ animation.onStart(animation, sv.value, timestamp, previousAnimation);
34
+ };
35
+ const currentTimestamp = getTimestamp();
36
+ initializeAnimation(currentTimestamp);
37
+ const step = (timestamp: number) => {
38
+ if (animation.cancelled) {
39
+ animation.callback && animation.callback(false /* finished */);
40
+ return;
41
+ }
42
+ const finished = animation.onFrame(animation, timestamp);
43
+ animation.finished = true;
44
+ animation.timestamp = timestamp;
45
+ sv._value = animation.current;
46
+ if (finished) {
47
+ animation.callback && animation.callback(true /* finished */);
48
+ } else {
49
+ requestAnimationFrame(step);
50
+ }
51
+ };
52
+
53
+ sv._animation = animation;
54
+
55
+ step(currentTimestamp);
56
+ } else {
57
+ // prevent setting again to the same value
58
+ // and triggering the mappers that treat this value as an input
59
+ if (sv._value === value) {
60
+ return;
61
+ }
62
+ sv._value = value as Descriptor | AnimatableValue;
63
+ }
64
+ }