@bugsee/react-native 0.0.0-stage → 7.0.0-beta2

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 (250) hide show
  1. package/BugseeReactNative.podspec +95 -0
  2. package/LICENSE +21 -0
  3. package/README.md +557 -2
  4. package/android/build.gradle +162 -0
  5. package/android/consumer-rules.pro +2 -0
  6. package/android/src/main/AndroidManifest.xml +33 -0
  7. package/android/src/main/java/com/bugsee/reactnative/AppearanceBridge.java +82 -0
  8. package/android/src/main/java/com/bugsee/reactnative/AttributeBridge.java +209 -0
  9. package/android/src/main/java/com/bugsee/reactnative/BreadcrumbFilterBridge.java +443 -0
  10. package/android/src/main/java/com/bugsee/reactnative/BridgeJson.java +168 -0
  11. package/android/src/main/java/com/bugsee/reactnative/BugseeModule.java +1869 -0
  12. package/android/src/main/java/com/bugsee/reactnative/BugseeOptionEnums.java +136 -0
  13. package/android/src/main/java/com/bugsee/reactnative/BugseePackage.java +42 -0
  14. package/android/src/main/java/com/bugsee/reactnative/BugseeReactNativeWrapper.java +200 -0
  15. package/android/src/main/java/com/bugsee/reactnative/BugseeStatusMapper.java +66 -0
  16. package/android/src/main/java/com/bugsee/reactnative/BugseeTokens.java +22 -0
  17. package/android/src/main/java/com/bugsee/reactnative/ConsoleEchoDedup.java +202 -0
  18. package/android/src/main/java/com/bugsee/reactnative/CreatedReports.java +127 -0
  19. package/android/src/main/java/com/bugsee/reactnative/DataRequestBridge.java +375 -0
  20. package/android/src/main/java/com/bugsee/reactnative/ExceptionBridge.java +88 -0
  21. package/android/src/main/java/com/bugsee/reactnative/ExceptionExecutors.java +33 -0
  22. package/android/src/main/java/com/bugsee/reactnative/LogFilterBridge.java +375 -0
  23. package/android/src/main/java/com/bugsee/reactnative/NetworkEvents.java +227 -0
  24. package/android/src/main/java/com/bugsee/reactnative/NetworkFilterBridge.java +498 -0
  25. package/android/src/main/java/com/bugsee/reactnative/PluginEndpoint.java +48 -0
  26. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWebException.java +19 -0
  27. package/android/src/main/java/com/bugsee/reactnative/ReactNativeWrapperInitProvider.java +61 -0
  28. package/android/src/main/java/com/bugsee/reactnative/ReactRootOriginTracker.java +867 -0
  29. package/android/src/main/java/com/bugsee/reactnative/ReportArgs.java +58 -0
  30. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerBridge.java +336 -0
  31. package/android/src/main/java/com/bugsee/reactnative/ReportHandlerDeadlines.java +57 -0
  32. package/android/src/main/java/com/bugsee/reactnative/ReportOps.java +287 -0
  33. package/android/src/main/java/com/bugsee/reactnative/SecureRectanglePulls.java +113 -0
  34. package/android/src/main/java/com/bugsee/reactnative/SecureRectangleStore.java +692 -0
  35. package/android/src/main/java/com/bugsee/reactnative/SpanHandles.java +143 -0
  36. package/android/src/main/java/com/bugsee/reactnative/WrapperChannelHolder.java +137 -0
  37. package/android/src/main/java/com/bugsee/reactnative/WrapperEventBus.java +110 -0
  38. package/android/src/main/java/com/bugsee/reactnative/WrapperRegistrar.java +54 -0
  39. package/app.plugin.js +4 -0
  40. package/ios/BGSRNConsoleCapture.h +52 -0
  41. package/ios/BGSRNConsoleCapture.mm +344 -0
  42. package/ios/BugseeModule.h +25 -0
  43. package/ios/BugseeModule.mm +2728 -0
  44. package/ios/Package.swift +71 -0
  45. package/ios/Support/Package.resolved +14 -0
  46. package/ios/Support/Package.swift +44 -0
  47. package/ios/Support/Sources/BugseeRNSupport/BGSRNAttributes.m +52 -0
  48. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReportOps.m +236 -0
  49. package/ios/Support/Sources/BugseeRNSupport/BGSRNCreatedReports.m +184 -0
  50. package/ios/Support/Sources/BugseeRNSupport/BGSRNDataRequestBridge.m +431 -0
  51. package/ios/Support/Sources/BugseeRNSupport/BGSRNErrorMessage.m +40 -0
  52. package/ios/Support/Sources/BugseeRNSupport/BGSRNEventBus.m +70 -0
  53. package/ios/Support/Sources/BugseeRNSupport/BGSRNExceptions.m +63 -0
  54. package/ios/Support/Sources/BugseeRNSupport/BGSRNGuardedEmit.mm +32 -0
  55. package/ios/Support/Sources/BugseeRNSupport/BGSRNJSON.m +40 -0
  56. package/ios/Support/Sources/BugseeRNSupport/BGSRNLaunchHold.m +147 -0
  57. package/ios/Support/Sources/BugseeRNSupport/BGSRNMainThread.m +23 -0
  58. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkEvents.m +159 -0
  59. package/ios/Support/Sources/BugseeRNSupport/BGSRNNetworkFilter.m +155 -0
  60. package/ios/Support/Sources/BugseeRNSupport/BGSRNPluginEndpoint.m +21 -0
  61. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactRootOriginTracker.m +63 -0
  62. package/ios/Support/Sources/BugseeRNSupport/BGSRNReactWindow.m +251 -0
  63. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportArgs.m +47 -0
  64. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportDeadlines.m +8 -0
  65. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportHandlerBridge.m +308 -0
  66. package/ios/Support/Sources/BugseeRNSupport/BGSRNReportOps.m +345 -0
  67. package/ios/Support/Sources/BugseeRNSupport/BGSRNSecureRectangles.m +508 -0
  68. package/ios/Support/Sources/BugseeRNSupport/BGSRNSettleOnce.m +62 -0
  69. package/ios/Support/Sources/BugseeRNSupport/BGSRNSpanHandles.m +136 -0
  70. package/ios/Support/Sources/BugseeRNSupport/BGSRNStatusMapper.m +18 -0
  71. package/ios/Support/Sources/BugseeRNSupport/BGSRNTokens.m +9 -0
  72. package/ios/Support/Sources/BugseeRNSupport/BGSRNValues.m +5 -0
  73. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapper.m +54 -0
  74. package/ios/Support/Sources/BugseeRNSupport/BGSRNWrapperChannelHolder.m +59 -0
  75. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNAttributes.h +63 -0
  76. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReportOps.h +51 -0
  77. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNCreatedReports.h +81 -0
  78. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNDataRequestBridge.h +118 -0
  79. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNErrorMessage.h +30 -0
  80. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNEventBus.h +45 -0
  81. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNExceptions.h +31 -0
  82. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNGuardedEmit.h +31 -0
  83. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNJSON.h +25 -0
  84. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNLaunchHold.h +86 -0
  85. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNMainThread.h +32 -0
  86. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkEvents.h +28 -0
  87. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNNetworkFilter.h +28 -0
  88. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNPluginEndpoint.h +17 -0
  89. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactRootOriginTracker.h +48 -0
  90. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReactWindow.h +135 -0
  91. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportArgs.h +17 -0
  92. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportDeadlines.h +37 -0
  93. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportHandlerBridge.h +94 -0
  94. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNReportOps.h +86 -0
  95. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSecureRectangles.h +198 -0
  96. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSettleOnce.h +25 -0
  97. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNSpanHandles.h +49 -0
  98. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNStatusMapper.h +26 -0
  99. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNTokens.h +14 -0
  100. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNValues.h +18 -0
  101. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapper.h +40 -0
  102. package/ios/Support/Sources/BugseeRNSupport/include/BGSRNWrapperChannelHolder.h +74 -0
  103. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNAttributesTests.m +194 -0
  104. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportOpsTests.m +274 -0
  105. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNCreatedReportsTests.m +240 -0
  106. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNDataRequestBridgeTests.m +685 -0
  107. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNErrorMessageTests.m +102 -0
  108. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNEventBusTests.m +136 -0
  109. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNExceptionsTests.m +68 -0
  110. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.h +41 -0
  111. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNFakeReport.m +166 -0
  112. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNGuardedEmitTests.mm +85 -0
  113. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNJSONTests.m +281 -0
  114. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNLaunchHoldTests.m +230 -0
  115. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNMainThreadTests.m +97 -0
  116. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkEventsTests.m +99 -0
  117. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNNetworkFilterTests.m +134 -0
  118. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNPluginEndpointTests.m +38 -0
  119. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactRootOriginTrackerTests.m +210 -0
  120. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReactWindowTests.m +548 -0
  121. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportArgsTests.m +51 -0
  122. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportDeadlinesTests.m +22 -0
  123. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportHandlerBridgeTests.m +382 -0
  124. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNReportOpsTests.m +295 -0
  125. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSecureRectanglesTests.m +568 -0
  126. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSettleOnceTests.m +134 -0
  127. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNSpanHandlesTests.m +153 -0
  128. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNStatusMapperTests.m +81 -0
  129. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNValuesTests.m +32 -0
  130. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperChannelHolderTests.m +205 -0
  131. package/ios/Support/Tests/BugseeRNSupportTests/BGSRNWrapperTests.m +66 -0
  132. package/ios/react-native-spm-prefix.h +10 -0
  133. package/native-versions.json +10 -0
  134. package/package.json +62 -3
  135. package/plugin/build/bundle-phase.d.ts +41 -0
  136. package/plugin/build/bundle-phase.js +116 -0
  137. package/plugin/build/bundle-phase.js.map +1 -0
  138. package/plugin/build/dsym-script.d.ts +32 -0
  139. package/plugin/build/dsym-script.js +159 -0
  140. package/plugin/build/dsym-script.js.map +1 -0
  141. package/plugin/build/endpoint.d.ts +30 -0
  142. package/plugin/build/endpoint.js +69 -0
  143. package/plugin/build/endpoint.js.map +1 -0
  144. package/plugin/build/gradle.d.ts +65 -0
  145. package/plugin/build/gradle.js +1066 -0
  146. package/plugin/build/gradle.js.map +1 -0
  147. package/plugin/build/index.d.ts +85 -0
  148. package/plugin/build/index.js +217 -0
  149. package/plugin/build/index.js.map +1 -0
  150. package/plugin/build/manifest.d.ts +48 -0
  151. package/plugin/build/manifest.js +69 -0
  152. package/plugin/build/manifest.js.map +1 -0
  153. package/plugin/build/native-versions.baked.json +4 -0
  154. package/plugin/build/native-versions.d.ts +10 -0
  155. package/plugin/build/native-versions.js +27 -0
  156. package/plugin/build/native-versions.js.map +1 -0
  157. package/plugin/build/pbx-string.d.ts +8 -0
  158. package/plugin/build/pbx-string.js +31 -0
  159. package/plugin/build/pbx-string.js.map +1 -0
  160. package/plugin/build/podfile.d.ts +3 -0
  161. package/plugin/build/podfile.js +28 -0
  162. package/plugin/build/podfile.js.map +1 -0
  163. package/plugin/build/properties.d.ts +19 -0
  164. package/plugin/build/properties.js +41 -0
  165. package/plugin/build/properties.js.map +1 -0
  166. package/plugin/build/scheme.d.ts +20 -0
  167. package/plugin/build/scheme.js +107 -0
  168. package/plugin/build/scheme.js.map +1 -0
  169. package/react-native.config.js +26 -0
  170. package/scripts/bugsee-sourcemaps.gradle +201 -0
  171. package/scripts/bugsee-xcode.sh +83 -0
  172. package/scripts/compose-then-inject.js +113 -0
  173. package/scripts/hermes-sourcemaps.js +634 -0
  174. package/scripts/hermesc-preserve-js.cmd +6 -0
  175. package/scripts/hermesc-preserve-js.js +200 -0
  176. package/scripts/hermesc-preserve-js.sh +7 -0
  177. package/src/NativeBugsee.ts +622 -0
  178. package/src/apm/notify.ts +58 -0
  179. package/src/apm/span.ts +403 -0
  180. package/src/apm/types.ts +57 -0
  181. package/src/appearance/report.ts +171 -0
  182. package/src/architecture.ts +48 -0
  183. package/src/attributes/errors.ts +39 -0
  184. package/src/attributes/validate.ts +150 -0
  185. package/src/breadcrumbs/add.ts +58 -0
  186. package/src/breadcrumbs/data.ts +33 -0
  187. package/src/breadcrumbs/filter.ts +215 -0
  188. package/src/breadcrumbs/types.ts +64 -0
  189. package/src/bridge/json.ts +110 -0
  190. package/src/console/capture.ts +180 -0
  191. package/src/console/dedup.ts +175 -0
  192. package/src/data/validate.ts +259 -0
  193. package/src/errorName.ts +32 -0
  194. package/src/exceptions/ErrorBoundary.tsx +92 -0
  195. package/src/exceptions/RootErrorReporter.tsx +61 -0
  196. package/src/exceptions/debugIds.ts +118 -0
  197. package/src/exceptions/handlers.ts +236 -0
  198. package/src/exceptions/options.ts +85 -0
  199. package/src/exceptions/payload.ts +456 -0
  200. package/src/exceptions/report.ts +170 -0
  201. package/src/exceptions/sha1.ts +148 -0
  202. package/src/exceptions/stack.ts +355 -0
  203. package/src/index.ts +962 -0
  204. package/src/logs/filter.ts +90 -0
  205. package/src/logs/log.ts +36 -0
  206. package/src/network/add.ts +165 -0
  207. package/src/network/filter.ts +198 -0
  208. package/src/options/AndroidLaunchOptions.ts +78 -0
  209. package/src/options/BugseeLaunchOptions.ts +190 -0
  210. package/src/options/IOSLaunchOptions.ts +57 -0
  211. package/src/options/android-options-manifest.json +646 -0
  212. package/src/options/createDefaultLaunchOptions.ts +29 -0
  213. package/src/options/endpoint.ts +58 -0
  214. package/src/options/enums.ts +104 -0
  215. package/src/options/ios-enums.json +19 -0
  216. package/src/options/keys.ts +39 -0
  217. package/src/options/option-enums.json +34 -0
  218. package/src/options/option-keys.json +107 -0
  219. package/src/report/BugseeReport.ts +264 -0
  220. package/src/report/CreatedReport.ts +127 -0
  221. package/src/report/dispatcher.ts +95 -0
  222. package/src/report/errors.ts +48 -0
  223. package/src/report/fields.ts +40 -0
  224. package/src/report/types.ts +157 -0
  225. package/src/report/validate.ts +197 -0
  226. package/src/secure/BugseeSecure.tsx +202 -0
  227. package/src/secure/ancestry.ts +622 -0
  228. package/src/secure/drivers.ts +342 -0
  229. package/src/secure/followEvents.ts +57 -0
  230. package/src/secure/hold.ts +99 -0
  231. package/src/secure/measureLoop.ts +323 -0
  232. package/src/secure/nativeAnimation.ts +1005 -0
  233. package/src/secure/rectangles.ts +93 -0
  234. package/src/secure/registry.ts +237 -0
  235. package/src/secure/surface.ts +83 -0
  236. package/src/secure/tracker.ts +723 -0
  237. package/src/secure/transition.ts +488 -0
  238. package/src/secure/unit.ts +22 -0
  239. package/src/status.ts +16 -0
  240. package/src/version.ts +9 -0
  241. package/src/viewtree/anchor.tsx +105 -0
  242. package/src/viewtree/constants.ts +20 -0
  243. package/src/viewtree/fiber.ts +251 -0
  244. package/src/viewtree/requests.ts +347 -0
  245. package/src/viewtree/walk.ts +902 -0
  246. package/src/wrapper/channel.ts +33 -0
  247. package/src/wrapper/collect.ts +52 -0
  248. package/src/wrapper/events.ts +45 -0
  249. package/src/wrapper/identity.ts +54 -0
  250. package/src/wrapper/lifecycle-events.ts +89 -0
@@ -0,0 +1,1005 @@
1
+ import {
2
+ SCROLL_HOST_TYPES,
3
+ clipToAncestors,
4
+ contentOf,
5
+ isReanimatedComponent,
6
+ measureFiber,
7
+ passMemo,
8
+ styleValue,
9
+ } from './ancestry';
10
+ import { eventsOn, isNode, knownDriven, noteAnimating } from './drivers';
11
+ import type { Attachment } from './drivers';
12
+ import './followEvents';
13
+ import { WHOLE_SURFACE, keyOf, platformOS } from './transition';
14
+ import type { Ancestry } from './ancestry';
15
+ import { FiberTag } from '../viewtree/fiber';
16
+ import type { FiberLike, WindowRect } from '../viewtree/fiber';
17
+
18
+ /**
19
+ * Where native-driven transforms above a `<BugseeSecure>` can draw it.
20
+ *
21
+ * With `useNativeDriver: true` an `Animated` value is applied natively, every
22
+ * frame, and never reaches the shadow tree as it moves: `measureInWindow`
23
+ * keeps reading what React last rendered (or, with React Native's C++
24
+ * Animated, the value the last animation ended at), while the view is drawn
25
+ * somewhere else. What JS does know is the `Animated` nodes themselves, in
26
+ * the props of the `Animated` component that renders the transformed host.
27
+ * From them this module bounds every value a node can take until the next
28
+ * re-measure, by interval arithmetic:
29
+ * - an `Animated.Value` in a timing animation: its whole path, easing
30
+ * overshoot included; still: its value, but only when JS provably has it.
31
+ * Natively, only animations (which JS starts, and which hand JS their end
32
+ * value) and native events (a scroll, a gesture, whose values JS never
33
+ * sees) move a value; `drivers.ts` follows which events are attached to
34
+ * which value, and which values were made native while it followed. A
35
+ * value an event is attached to is wherever the event takes it, animated
36
+ * or not: on Android, a ScrollView's `contentOffset` within the offsets
37
+ * that ScrollView can reach; anything else, anywhere. A value whose driver
38
+ * is not known (made native before the package loaded, or left where its
39
+ * last event took it, and not seen animating since) is anywhere too, and
40
+ * so is every native value where nothing can be followed;
41
+ * - `diffClamp` between its bounds; `add`, `subtract`, `multiply`, `divide`
42
+ * and `modulo` from their operands; an interpolation through its mapping,
43
+ * or, whatever its input, within its outputs (each segment as far as its
44
+ * easing takes it) when neither end runs on with its input: both clamp,
45
+ * or extend a flat last segment.
46
+ *
47
+ * A transform of translations and scales (in any order, about its
48
+ * `transformOrigin`) is then bounded exactly: the view's measured rectangle is
49
+ * taken back to the host's own frame under each value the shadow tree may
50
+ * hold (the rendered one, or the current one), and carried through every
51
+ * value the nodes can take. The result is joined with the measured rectangle,
52
+ * so a mask is only ever widened, never moved off the view.
53
+ *
54
+ * Anything else is unbounded and covers the whole surface, cut by the
55
+ * clipping ancestors above it: a rotation, skew or matrix; a node of a kind,
56
+ * or with fields, this module does not know (React Native renamed something);
57
+ * a spring or decay; a value a native event is attached to, or whose
58
+ * driver is not known, beyond what is said above; a Reanimated component
59
+ * whose style can move the view (an animated style that sets a transform, a
60
+ * shared value set inline, a CSS animation or transition: Reanimated keeps
61
+ * its values on the UI thread).
62
+ * Masking too much beats drawing the view where nothing masks it.
63
+ *
64
+ * A JS-driven transform at rest needs none of this: Fabric commits its
65
+ * values to the shadow tree, and `measureInWindow` follows it. While a JS
66
+ * animation runs on it (`_animation` or `_tracking` set on a root), though,
67
+ * it moves the view every frame with no wrapped call, and a step can be drawn
68
+ * before the frame that measures it: it is then bounded over the animation's
69
+ * path like a native one, and the view re-measured every frame; once it
70
+ * stops, back to the tick.
71
+ */
72
+
73
+ export interface Range {
74
+ lo: number;
75
+ hi: number;
76
+ }
77
+
78
+ /** A node's bound, and what about it calls for watching. */
79
+ interface NodeBound {
80
+ /** Every value it can take until the next re-measure; `undefined`: unbounded. */
81
+ readonly range: Range | undefined;
82
+ /** A timing animation drives it: its value changes every frame. */
83
+ readonly running: boolean;
84
+ /** The native `Animated.Value`s it is computed from. */
85
+ readonly roots: readonly AnimatedNodeLike[];
86
+ }
87
+
88
+ /** One `Animated` component's transform, as it can move the view. */
89
+ export interface MotionLevel {
90
+ /** Where the component is in `Ancestry.path`: clippers below it move with it. */
91
+ readonly index: number;
92
+ /** Every place `rect` (measured, in the window) can be drawn; `undefined`: anywhere. */
93
+ readonly move: (rect: WindowRect) => WindowRect | undefined;
94
+ }
95
+
96
+ /** What native-driven transforms above the view do to it. */
97
+ export interface NativeMotion {
98
+ /** Nearest first. */
99
+ readonly levels: readonly MotionLevel[];
100
+ /** A native animation is running: where the view can be keeps changing. */
101
+ readonly running: boolean;
102
+ /** The native values the bounds came from, to watch for an animation starting. */
103
+ readonly roots: readonly AnimatedNodeLike[];
104
+ }
105
+
106
+ /** How finely an easing or an interpolation is sampled. */
107
+ const SAMPLES = 20;
108
+ /** How much wider a sampled curve that turns back is taken to reach: a peak can fall between samples. */
109
+ const TURN_PAD = 0.1;
110
+
111
+ const TRANSLATE_AXES: Readonly<Record<string, 'x' | 'y'>> = { translateX: 'x', translateY: 'y' };
112
+ const SCALE_AXES: Readonly<Record<string, ReadonlyArray<'x' | 'y'>>> = {
113
+ scale: ['x', 'y'],
114
+ scaleX: ['x'],
115
+ scaleY: ['y'],
116
+ };
117
+ /** Functions that, at these values, do nothing; anything else they do is unbounded. */
118
+ const IDENTITY: Readonly<Record<string, unknown>> = {
119
+ rotate: '0deg',
120
+ rotateX: '0deg',
121
+ rotateY: '0deg',
122
+ rotateZ: '0deg',
123
+ skewX: '0deg',
124
+ skewY: '0deg',
125
+ };
126
+
127
+ export interface AnimatedNodeLike {
128
+ __getValue: () => unknown;
129
+ [field: string]: unknown;
130
+ }
131
+
132
+ /**
133
+ * Whether `node` may be driven natively. A node without the flag at all is
134
+ * of a kind this module does not know: taken as native.
135
+ */
136
+ function isNativeNode(value: unknown): value is AnimatedNodeLike {
137
+ return isNode(value) && (!('__isNative' in value) || value.__isNative === true);
138
+ }
139
+
140
+ function finiteNumber(value: unknown): value is number {
141
+ return Number.isFinite(value as number);
142
+ }
143
+
144
+ function hull(values: readonly number[]): Range {
145
+ return { lo: Math.min(...values), hi: Math.max(...values) };
146
+ }
147
+
148
+ /**
149
+ * `values` sampled in order along a curve, widened by {@link TURN_PAD} of its
150
+ * span when an extreme is not at an end: the curve turns back, and its peak
151
+ * can fall between samples.
152
+ */
153
+ function sampled(values: readonly unknown[]): Range {
154
+ const range = hull(values as number[]);
155
+ const ends = hull([values[0] as number, values[values.length - 1] as number]);
156
+ if (range.lo === ends.lo && range.hi === ends.hi) {
157
+ return range;
158
+ }
159
+ const pad = (range.hi - range.lo) * TURN_PAD;
160
+ return { lo: range.lo - pad, hi: range.hi + pad };
161
+ }
162
+
163
+ const UNBOUNDED: NodeBound = { range: undefined, running: false, roots: [] };
164
+
165
+ /**
166
+ * An `Animated.Value`: its running timing animation's path, or its value if
167
+ * JS provably has it. A native value is wherever the events attached to it
168
+ * take it (within a ScrollView's reach, for its offset; otherwise anywhere),
169
+ * and anywhere when what drives it is not known (`drivers.ts`).
170
+ */
171
+ function valueBound(node: AnimatedNodeLike): NodeBound {
172
+ // Fields this module depends on, by name: renamed, nothing here can be trusted.
173
+ if (!('_value' in node) || !('_offset' in node) || !('_animation' in node)) {
174
+ return UNBOUNDED;
175
+ }
176
+ const now = node.__getValue();
177
+ const offset = node._offset;
178
+ if (!finiteNumber(now) || !finiteNumber(offset) || node._tracking != null) {
179
+ return { ...UNBOUNDED, roots: [node] };
180
+ }
181
+ const animation = node._animation as Record<string, unknown> | null;
182
+ // Driven from JS, it is in the shadow tree already: what JS holds is the truth.
183
+ if (node.__isNative !== false) {
184
+ const events = eventsOn(node);
185
+ if (events === undefined || events.length > 0) {
186
+ // An event moves it as well as any animation, wherever the gesture or the scroll takes it.
187
+ const reach = events !== undefined && animation == null ? scrollRange(events) : undefined;
188
+ const range = reach === undefined ? undefined : hull([now, reach.lo + offset, reach.hi + offset]);
189
+ return { range, running: animation != null, roots: [node] };
190
+ }
191
+ if (animation != null) {
192
+ noteAnimating(node);
193
+ } else if (!knownDriven(node)) {
194
+ // Made native before the package loaded, or left where an event took it: anything may drive it.
195
+ return { range: undefined, running: false, roots: [node] };
196
+ }
197
+ }
198
+ if (animation == null) {
199
+ return { range: { lo: now, hi: now }, running: false, roots: [node] };
200
+ }
201
+ const { _fromValue: from, _toValue: to, _easing: easing } = animation;
202
+ if (!finiteNumber(from) || !finiteNumber(to) || typeof easing !== 'function') {
203
+ return { range: undefined, running: true, roots: [node] };
204
+ }
205
+ const values = [now];
206
+ const path: number[] = [];
207
+ for (let step = 0; step <= SAMPLES; step += 1) {
208
+ const eased = (easing as (t: number) => unknown)(step / SAMPLES);
209
+ if (!finiteNumber(eased)) {
210
+ return { range: undefined, running: true, roots: [node] };
211
+ }
212
+ path.push(offset + from + eased * (to - from));
213
+ }
214
+ const along = sampled(path);
215
+ values.push(along.lo, along.hi);
216
+ return { range: hull(values), running: true, roots: [node] };
217
+ }
218
+
219
+ /**
220
+ * How far past either end of its content an Android ScrollView can be
221
+ * scrolled: a fling runs on past an end by the overfling distance before it
222
+ * springs back; a drag, a fling's end, `scrollTo` and `contentOffset` are
223
+ * held within the content. The overfling distance is AOSP's
224
+ * `ViewConfiguration` (6 dp), which JS cannot read: an assumption, taken
225
+ * about ten times over for a device whose framework changed it. Only a
226
+ * ScrollView whose hosts (itself and above) are scaled by 1 or -1 is
227
+ * bounded: under any other scale its offsets are not in window units.
228
+ */
229
+ export const SCROLL_OVERRUN = 64;
230
+
231
+ /**
232
+ * Where native scroll events can set a value they are attached to: within
233
+ * the reach of each ScrollView, for its `contentOffset`, measured now; or
234
+ * `undefined` when any of them is something else, or cannot be measured.
235
+ * On Android only: an iOS ScrollView bounces past its ends, and its
236
+ * `scrollTo` goes wherever it is told.
237
+ */
238
+ function scrollRange(events: readonly Attachment[]): Range | undefined {
239
+ if (platformOS() !== 'android') {
240
+ return undefined;
241
+ }
242
+ let range: Range | undefined;
243
+ for (const event of events) {
244
+ const reach = scrollReach(event);
245
+ if (reach === undefined) {
246
+ return undefined;
247
+ }
248
+ range = hull([reach.lo, reach.hi, range?.lo ?? reach.lo, range?.hi ?? reach.hi]);
249
+ }
250
+ return range;
251
+ }
252
+
253
+ function scrollReach(attachment: Attachment): Range | undefined {
254
+ const { event, path } = attachment;
255
+ const along = path[2] === 'x' ? 'width' : path[2] === 'y' ? 'height' : undefined;
256
+ if (event !== 'onScroll' || path.length !== 3 || path[0] !== 'nativeEvent' || path[1] !== 'contentOffset') {
257
+ return undefined;
258
+ }
259
+ const scroller = scrollHostOf(attachment);
260
+ // Android renders a vertical ScrollView's content through the `View` component: its host is further down.
261
+ const content = scroller === undefined ? undefined : contentOf(scroller);
262
+ if (along === undefined || content === undefined || scaledFrom(scroller as FiberLike)) {
263
+ return undefined;
264
+ }
265
+ const viewport = measureFiber(scroller as FiberLike);
266
+ const inner = measureFiber(content);
267
+ if (viewport == null || inner == null) {
268
+ return undefined;
269
+ }
270
+ return { lo: -SCROLL_OVERRUN, hi: Math.max(0, inner[along] - viewport[along]) + SCROLL_OVERRUN };
271
+ }
272
+
273
+ /** How far up from a ScrollView its hosts are read for a scale; a cycle in a malformed tree must end. */
274
+ const MAX_ANCESTORS = 10_000;
275
+
276
+ /**
277
+ * Whether `scroller`, or a host above it, has a committed transform that
278
+ * scales by anything but 1 or -1 (or by what cannot be read): its offsets,
279
+ * in its own units, are then not the window units it is measured in.
280
+ */
281
+ function scaledFrom(scroller: FiberLike): boolean {
282
+ let fiber: FiberLike | null = scroller;
283
+ // One fiber more or less before giving up on a malformed tree changes nothing.
284
+ // Stryker disable next-line EqualityOperator
285
+ for (let depth = 0; fiber != null && depth < MAX_ANCESTORS; depth += 1) {
286
+ if (fiber.tag === FiberTag.HostComponent && scalesBy(fiber.memoizedProps, (by) => Math.abs(by as number) !== 1)) {
287
+ return true;
288
+ }
289
+ fiber = fiber.return;
290
+ }
291
+ return false;
292
+ }
293
+
294
+ /** The ScrollView host each attachment was made to (`null`: none found), looked for once. */
295
+ const scrollHosts = new WeakMap<Attachment, FiberLike | null>();
296
+
297
+ /** The fiber root the running measure searches for a host by its tag; `null` outside one. */
298
+ let searchRoot: { current?: unknown } | null = null;
299
+
300
+ function scrollHostOf(attachment: Attachment): FiberLike | undefined {
301
+ let host = scrollHosts.get(attachment);
302
+ // Looking again finds the same host, remembered by its tag: only work.
303
+ // Stryker disable next-line ConditionalExpression
304
+ if (host === undefined) {
305
+ const { view } = attachment;
306
+ const handle = (view as { __internalInstanceHandle?: unknown } | null | undefined)?.__internalInstanceHandle;
307
+ // An `Animated.ScrollView` attaches its events to its scrollable node's tag; RN's own ScrollView, to its host.
308
+ const tag = typeof view === 'number' ? view : (view as { __nativeTag?: unknown } | null | undefined)?.__nativeTag;
309
+ // No host has no tag: looking for none finds none.
310
+ // Stryker disable next-line ConditionalExpression
311
+ const found = isFiber(handle) ? handle : typeof tag === 'number' ? hostWithTag(tag) : null;
312
+ if (found === undefined) {
313
+ // No tree to look in yet: looked for again next time.
314
+ return undefined;
315
+ }
316
+ host = found !== null && SCROLL_HOST_TYPES.has(found.type) ? found : null;
317
+ // As above.
318
+ // Stryker disable next-line CallExpression
319
+ scrollHosts.set(attachment, host);
320
+ }
321
+ return host ?? undefined;
322
+ }
323
+
324
+ function isFiber(value: unknown): value is FiberLike {
325
+ return value !== null && typeof value === 'object' && typeof (value as { tag?: unknown }).tag === 'number';
326
+ }
327
+
328
+ /** A host's native tag: Fabric's `canonical.nativeTag`, or Paper's `_nativeTag`; `null` when it has neither. */
329
+ function tagOf(fiber: FiberLike): unknown {
330
+ const node = fiber.stateNode as { canonical?: { nativeTag?: unknown }; _nativeTag?: unknown } | null | undefined;
331
+ return node?.canonical?.nativeTag ?? node?._nativeTag ?? null;
332
+ }
333
+
334
+ /** Hosts found by tag, while they still carry it. */
335
+ const tagged = new Map<number, FiberLike>();
336
+ /** How many hosts are remembered by tag; past it, they are looked for again. */
337
+ const TAGGED_MAX = 32;
338
+ /** A cycle in a malformed tree must not spin forever. */
339
+ const MAX_SEARCH = 100_000;
340
+
341
+ /**
342
+ * The committed host with native tag `tag` in the tree being measured;
343
+ * `null` when it is not there, `undefined` when there is no tree to look in.
344
+ */
345
+ function hostWithTag(tag: number): FiberLike | null | undefined {
346
+ const known = tagged.get(tag);
347
+ if (known !== undefined && tagOf(known) === tag) {
348
+ return known;
349
+ }
350
+ const top = searchRoot?.current;
351
+ if (!isFiber(top)) {
352
+ return undefined;
353
+ }
354
+ const pending: FiberLike[] = [top];
355
+ // One fiber more or less before giving up on a malformed tree changes nothing.
356
+ // Stryker disable next-line EqualityOperator
357
+ for (let visited = 0; pending.length > 0 && visited < MAX_SEARCH; visited += 1) {
358
+ const fiber = pending.pop() as FiberLike;
359
+ if (fiber.tag === FiberTag.HostComponent && tagOf(fiber) === tag) {
360
+ // Bounds the memory alone: forgetting them all, or one more or less, only costs searching again.
361
+ // Stryker disable next-line ConditionalExpression,EqualityOperator,BlockStatement,CallExpression
362
+ if (tagged.size >= TAGGED_MAX) {
363
+ // Stryker disable next-line CallExpression -- as above
364
+ tagged.clear();
365
+ }
366
+ tagged.set(tag, fiber);
367
+ return fiber;
368
+ }
369
+ for (let child = fiber.child; child != null; child = child.sibling) {
370
+ pending.push(child);
371
+ }
372
+ }
373
+ return null;
374
+ }
375
+
376
+ /**
377
+ * How an interpolation extends past an end of its input range, as React
378
+ * Native reads its config: anything but `clamp` and `identity` (`undefined`
379
+ * included, its default) extends.
380
+ */
381
+ function extrapolation(config: Record<string, unknown>, side: 'extrapolateLeft' | 'extrapolateRight'): unknown {
382
+ return config[side] !== undefined ? config[side] : config.extrapolate;
383
+ }
384
+
385
+ /** Where an easing takes a segment, from its start (0) to its end (1); `undefined` when it cannot be read. */
386
+ function easedSpan(easing: unknown): Range | undefined {
387
+ if (easing == null) {
388
+ return { lo: 0, hi: 1 };
389
+ }
390
+ if (typeof easing !== 'function') {
391
+ return undefined;
392
+ }
393
+ const values: unknown[] = [];
394
+ for (let step = 0; step <= SAMPLES; step += 1) {
395
+ values.push((easing as (t: number) => unknown)(step / SAMPLES));
396
+ }
397
+ return values.every(finiteNumber) ? sampled(values) : undefined;
398
+ }
399
+
400
+ /**
401
+ * Every output an interpolation can give, whatever its input, when neither
402
+ * end runs on with its input: an end that clamps, or that extends a flat
403
+ * last segment (React Native gives a segment whose two outputs are equal
404
+ * that output, eased or not); `undefined` otherwise. Within each segment,
405
+ * the whole span of its easing.
406
+ */
407
+ function outputHull(config: Record<string, unknown>): Range | undefined {
408
+ const outputs = config.outputRange;
409
+ if (!Array.isArray(outputs) || outputs.length < 2 || !outputs.every(finiteNumber)) {
410
+ return undefined;
411
+ }
412
+ const last = outputs.length - 1;
413
+ // An end that extends (React Native does for anything but these two) runs on unless its segment is flat.
414
+ const holds = (how: unknown, from: number, to: number): boolean =>
415
+ how === 'clamp' || (how !== 'identity' && from === to);
416
+ const eased = easedSpan(config.easing);
417
+ if (
418
+ eased === undefined ||
419
+ !holds(extrapolation(config, 'extrapolateLeft'), outputs[0] as number, outputs[1] as number) ||
420
+ !holds(extrapolation(config, 'extrapolateRight'), outputs[last - 1] as number, outputs[last] as number)
421
+ ) {
422
+ return undefined;
423
+ }
424
+ // Each segment, from its start: a flat one gives that output, eased or not; an end held gives its segment's.
425
+ const values = (outputs as number[]).slice(0, -1).flatMap((from, index) => {
426
+ const to = outputs[index + 1] as number;
427
+ return [from + eased.lo * (to - from), from + eased.hi * (to - from)];
428
+ });
429
+ return hull(values);
430
+ }
431
+
432
+ /**
433
+ * An interpolation: through its mapping; or, whatever its input, within its
434
+ * outputs when neither end runs on with its input (`outputHull`).
435
+ */
436
+ function interpolationBound(node: AnimatedNodeLike): NodeBound {
437
+ const input = nodeBound(node._parent as AnimatedNodeLike);
438
+ const config = (node._config ?? {}) as Record<string, unknown>;
439
+ const held = outputHull(config);
440
+ const interpolate = (node._getInterpolation as (() => unknown) | undefined)?.();
441
+ if (input.range === undefined || typeof interpolate !== 'function') {
442
+ return { ...input, range: held };
443
+ }
444
+ const { lo, hi } = input.range;
445
+ // Stryker disable next-line ArrayDeclaration -- the filter below drops anything that is not a number
446
+ const breakpoints = Array.isArray(config.inputRange) ? (config.inputRange as unknown[]) : [];
447
+ // The ends are sampled anyway.
448
+ // Stryker disable next-line EqualityOperator -- so taking them in as breakpoints too changes nothing
449
+ const samples = breakpoints.filter((point): point is number => finiteNumber(point) && point > lo && point < hi);
450
+ for (let step = 0; step <= SAMPLES; step += 1) {
451
+ samples.push(lo + ((hi - lo) * step) / SAMPLES);
452
+ }
453
+ samples.sort((a, b) => a - b);
454
+ const values = samples.map((sample) => (interpolate as (input: number) => unknown)(sample));
455
+ if (!values.every(finiteNumber)) {
456
+ return { ...input, range: held };
457
+ }
458
+ return { ...input, range: sampled(values as number[]) };
459
+ }
460
+
461
+ function join(...bounds: NodeBound[]): Pick<NodeBound, 'running' | 'roots'> {
462
+ return { running: bounds.some((bound) => bound.running), roots: bounds.flatMap((bound) => bound.roots) };
463
+ }
464
+
465
+ type Operation = (x: number, y: number) => number;
466
+
467
+ const divide: Operation = (x, y) => x / y;
468
+ const OPERATIONS: readonly Operation[] = [(x, y) => x + y, (x, y) => x - y, (x, y) => x * y, divide];
469
+
470
+ /**
471
+ * Which operations `node` (with operands `_a` and `_b`) can be: `divide` by
472
+ * its own field; otherwise those whose result on the operands' values now is
473
+ * the node's value now (`add`, `subtract` and `multiply` keep the same
474
+ * fields). Where several match (`x * 1` and `x / 1`), each is taken. None:
475
+ * a kind this module does not know.
476
+ */
477
+ function operationsOf(node: AnimatedNodeLike): readonly Operation[] {
478
+ if ('_warnedAboutDivideByZero' in node) {
479
+ return [divide];
480
+ }
481
+ const x = (node._a as AnimatedNodeLike).__getValue();
482
+ const y = (node._b as AnimatedNodeLike).__getValue();
483
+ const value = node.__getValue();
484
+ return OPERATIONS.filter((operation) => operation(x as number, y as number) === value);
485
+ }
486
+
487
+ /** `add`, `subtract`, `multiply`, `divide`: from the operands, by intervals. */
488
+ function binaryBound(node: AnimatedNodeLike): NodeBound {
489
+ const a = nodeBound(node._a as AnimatedNodeLike);
490
+ const b = nodeBound(node._b as AnimatedNodeLike);
491
+ const both = join(a, b);
492
+ const operations = isNode(node._a) && isNode(node._b) ? operationsOf(node) : [];
493
+ if (a.range === undefined || b.range === undefined || operations.length === 0) {
494
+ return { range: undefined, ...both };
495
+ }
496
+ const as = [a.range.lo, a.range.hi];
497
+ const bs = [b.range.lo, b.range.hi];
498
+ if (operations.includes(divide) && b.range.lo <= 0 && b.range.hi >= 0) {
499
+ return { range: undefined, ...both };
500
+ }
501
+ const results = as.flatMap((x) => bs.flatMap((y) => operations.map((operation) => operation(x, y))));
502
+ return { range: hull(results), ...both };
503
+ }
504
+
505
+ /**
506
+ * Every value `node` (a number or an `Animated` node) can take until the next
507
+ * re-measure, whether a timing animation drives it, and the native values it
508
+ * comes from. Computed once per node a measure pass.
509
+ */
510
+ export function nodeBound(node: unknown): NodeBound {
511
+ if (finiteNumber(node)) {
512
+ return { range: { lo: node, hi: node }, running: false, roots: [] };
513
+ }
514
+ if (!isNode(node)) {
515
+ return UNBOUNDED;
516
+ }
517
+ return passMemo(node, () => {
518
+ // Every kind told apart by the fields that are its own.
519
+ if ('_parent' in node) {
520
+ return interpolationBound(node);
521
+ }
522
+ if ('_min' in node) {
523
+ const input = nodeBound(node._a);
524
+ const { _min: min, _max: max } = node;
525
+ return { ...input, range: finiteNumber(min) && finiteNumber(max) ? { lo: min, hi: max } : undefined };
526
+ }
527
+ if ('_modulus' in node) {
528
+ const input = nodeBound(node._a);
529
+ const modulus = node._modulus;
530
+ return { ...input, range: finiteNumber(modulus) ? hull([0, modulus]) : undefined };
531
+ }
532
+ if ('_b' in node) {
533
+ return binaryBound(node);
534
+ }
535
+ return valueBound(node);
536
+ });
537
+ }
538
+
539
+ /** The transform functions of `props.style`, as `[name, value]`; `null` when there is no list. */
540
+ function transformOf(props: unknown): Array<[string, unknown]> | null {
541
+ const transform = styleValue((props as { style?: unknown } | null | undefined)?.style, 'transform');
542
+ if (!Array.isArray(transform)) {
543
+ return null;
544
+ }
545
+ return transform.flatMap((entry) =>
546
+ entry !== null && typeof entry === 'object' ? Object.entries(entry as Record<string, unknown>) : [['?', entry]],
547
+ ) as Array<[string, unknown]>;
548
+ }
549
+
550
+ /** One axis of an axis-aligned affine map: `q -> scale * q + shift`. */
551
+ interface Affine {
552
+ scale: number;
553
+ shift: number;
554
+ }
555
+
556
+ /** A transform origin on one axis: a fraction of the size plus a length. */
557
+ interface Origin {
558
+ fraction: number;
559
+ length: number;
560
+ }
561
+
562
+ const KEYWORDS: Readonly<Record<string, Origin>> = {
563
+ left: { fraction: 0, length: 0 },
564
+ top: { fraction: 0, length: 0 },
565
+ center: { fraction: 0.5, length: 0 },
566
+ right: { fraction: 1, length: 0 },
567
+ bottom: { fraction: 1, length: 0 },
568
+ };
569
+
570
+ function originPart(part: unknown): Origin | undefined {
571
+ const keyword = KEYWORDS[part as string];
572
+ if (keyword !== undefined) {
573
+ return keyword;
574
+ }
575
+ const match = /^(-?\d+(?:\.\d+)?)(%|px)?$/.exec(String(part));
576
+ if (match === null) {
577
+ return undefined;
578
+ }
579
+ const amount = Number(match[1]);
580
+ return match[2] === '%' ? { fraction: amount / 100, length: 0 } : { fraction: 0, length: amount };
581
+ }
582
+
583
+ /** The host's `transformOrigin`, per axis (the centre when unset); `undefined` when it cannot be read. */
584
+ export function originOf(props: unknown): { x: Origin; y: Origin } | undefined {
585
+ const origin = styleValue((props as { style?: unknown } | null | undefined)?.style, 'transformOrigin');
586
+ if (origin === undefined) {
587
+ return { x: KEYWORDS.center as Origin, y: KEYWORDS.center as Origin };
588
+ }
589
+ if (!Array.isArray(origin) && typeof origin !== 'string') {
590
+ return undefined;
591
+ }
592
+ const parts: unknown[] = Array.isArray(origin) ? origin : origin.trim().split(/\s+/);
593
+ // An empty list names no axis, as an unknown word does, below.
594
+ if (parts.length > 3) {
595
+ return undefined;
596
+ }
597
+ const [first, second = 'center'] = parts;
598
+ // CSS lets the keywords come either way round, and one alone name either axis.
599
+ const swapped = first === 'top' || first === 'bottom' || second === 'left' || second === 'right';
600
+ const x = originPart(swapped ? second : first);
601
+ const y = originPart(swapped ? first : second);
602
+ return x === undefined || y === undefined ? undefined : { x, y };
603
+ }
604
+
605
+ interface Step {
606
+ axis: 'x' | 'y';
607
+ /** A translation; otherwise a scale. */
608
+ shift: boolean;
609
+ bound: NodeBound;
610
+ /** The value the host was rendered with; `undefined`: not in its props. */
611
+ rendered: number | undefined;
612
+ /** The value its node holds in JS now. */
613
+ current: number | undefined;
614
+ }
615
+
616
+ /** The host an `Animated` component at `index` renders: the nearest host below it. */
617
+ function hostBelow(ancestry: Ancestry, fibers: readonly FiberLike[], index: number): FiberLike {
618
+ for (let below = index - 1; below >= 0; below -= 1) {
619
+ if ((fibers[below] as FiberLike).tag === FiberTag.HostComponent) {
620
+ return fibers[below] as FiberLike;
621
+ }
622
+ }
623
+ return ancestry.self as FiberLike;
624
+ }
625
+
626
+ /** Composes `steps` (innermost last, as CSS applies them) on one axis, each at `pick`'s value. */
627
+ function compose(steps: readonly Step[], axis: 'x' | 'y', pick: (step: Step) => number): Affine {
628
+ let map: Affine = { scale: 1, shift: 0 };
629
+ for (let index = steps.length - 1; index >= 0; index -= 1) {
630
+ const step = steps[index] as Step;
631
+ if (step.axis !== axis) {
632
+ continue;
633
+ }
634
+ const value = pick(step);
635
+ map =
636
+ step.shift
637
+ ? { scale: map.scale, shift: map.shift + value }
638
+ : { scale: map.scale * value, shift: map.shift * value };
639
+ }
640
+ return map;
641
+ }
642
+
643
+ /** `[lo, hi]` carried through `steps` on one axis, every value of every step. */
644
+ function sweep(lo: number, hi: number, steps: readonly Step[], axis: 'x' | 'y'): Range {
645
+ let range = { lo, hi };
646
+ for (let index = steps.length - 1; index >= 0; index -= 1) {
647
+ const step = steps[index] as Step;
648
+ if (step.axis !== axis) {
649
+ continue;
650
+ }
651
+ const by = step.bound.range as Range;
652
+ range =
653
+ step.shift
654
+ ? { lo: range.lo + by.lo, hi: range.hi + by.hi }
655
+ : hull([range.lo * by.lo, range.lo * by.hi, range.hi * by.lo, range.hi * by.hi]);
656
+ }
657
+ return range;
658
+ }
659
+
660
+ /**
661
+ * The motion of one `Animated` component's transform; `null` when it has no
662
+ * `Animated` node in it. Nodes none of which is native yet move nothing now,
663
+ * but the first animation with the native driver makes them native and
664
+ * commits nothing: they are only watched (and with nothing to watch, the view
665
+ * is re-measured every frame).
666
+ */
667
+ function levelOf(ancestry: Ancestry, fibers: readonly FiberLike[], index: number): NotNative | Level | Pending {
668
+ const fiber = fibers[index] as FiberLike;
669
+ if (isReanimatedComponent(fiber)) {
670
+ return reanimatedMoves(fiber.memoizedProps) ? UNBOUNDED_LEVEL : NOT_NATIVE;
671
+ }
672
+ const entries = transformOf(fiber.memoizedProps);
673
+ if (entries === null) {
674
+ return NOT_NATIVE;
675
+ }
676
+ const nodes = entries.filter(([, value]) => isNode(value));
677
+ if (nodes.length === 0) {
678
+ return NOT_NATIVE;
679
+ }
680
+ if (!nodes.some(([, value]) => isNativeNode(value))) {
681
+ const roots = nodes.flatMap(([, value]) => nodeBound(value).roots);
682
+ // Driven from JS, an animation moves the view every frame (Fabric commits each step) with no wrapped call, and
683
+ // a step can be drawn before the frame that measures it: while one runs on a root it is bounded over its path,
684
+ // as a native one is, and followed every frame; once it stops, back to the tick.
685
+ if (!roots.some((root) => root._animation != null || root._tracking != null)) {
686
+ return { running: roots.length === 0, roots, move: undefined };
687
+ }
688
+ }
689
+ const host = hostBelow(ancestry, fibers, index);
690
+ // By place: a function can come twice (`translateX` twice), each with its own value.
691
+ // A string in place of the list, or of an entry, names no transform function.
692
+ // Stryker disable next-line ArrayDeclaration
693
+ const rendered = transformOf(host.memoizedProps) ?? [];
694
+ const steps: Step[] = [];
695
+ const bounds: NodeBound[] = [];
696
+ let bounded = true;
697
+ for (const [place, [name, value]] of entries.entries()) {
698
+ const bound = nodeBound(value);
699
+ bounds.push(bound);
700
+ const axes = TRANSLATE_AXES[name] !== undefined ? [TRANSLATE_AXES[name]] : SCALE_AXES[name];
701
+ if (axes === undefined) {
702
+ bounded &&= name in IDENTITY && value === IDENTITY[name];
703
+ continue;
704
+ }
705
+ // Without this, sweeping a missing range throws, which reads as unbounded too.
706
+ // Stryker disable next-line AssignmentOperator,ConditionalExpression -- so its mutants are equivalent
707
+ bounded &&= bound.range !== undefined;
708
+ // Stryker disable next-line ArrayDeclaration -- as above
709
+ const [renderedName, at] = rendered[place] ?? [];
710
+ const now = isNode(value) ? value.__getValue() : value;
711
+ for (const axis of axes) {
712
+ steps.push({
713
+ axis,
714
+ shift: TRANSLATE_AXES[name] !== undefined,
715
+ bound,
716
+ rendered: renderedName === name && finiteNumber(at) ? at : undefined,
717
+ current: finiteNumber(now) ? now : undefined,
718
+ });
719
+ }
720
+ }
721
+ const { running, roots } = join(...bounds);
722
+ // Scaled up in the shadow tree: the levels inside it move the view by more than their own values.
723
+ const scaled = steps.some(
724
+ (step) => !step.shift && [step.rendered, step.current].some((at) => Math.abs(at ?? 1) > 1),
725
+ );
726
+ // The host is rendered with the component's style, `transformOrigin` included.
727
+ const origin = originOf(host.memoizedProps);
728
+ // Without the second test, reading a missing origin throws, which reads as unbounded too.
729
+ // Stryker disable next-line ConditionalExpression -- so its mutant is equivalent
730
+ if (!bounded || origin === undefined || scrolledBy(ancestry, roots)) {
731
+ return { running, roots, move: () => undefined, scaled };
732
+ }
733
+ return { running, roots, move: (rect) => moveThrough(rect, host, steps, origin), scaled };
734
+ }
735
+
736
+ /**
737
+ * Whether a value of a level is, or may be, the offset of a ScrollView whose
738
+ * content holds the view (a sticky header; or a transform around a
739
+ * ScrollView, driven by its own offset): the view then moves with that
740
+ * content as well, by the same offset, natively ahead of the shadow tree it is
741
+ * measured in, so bounding the transform alone does not bound where it is
742
+ * drawn. Unbounded: the whole surface, cut by the clipping ancestors above
743
+ * the level. Fails closed: a native value whose driver is not known (nothing
744
+ * followed, made native unfollowed, or a scroll whose ScrollView cannot be
745
+ * found) counts as such an offset whenever a ScrollView is above the view.
746
+ */
747
+ function scrolledBy(ancestry: Ancestry, roots: readonly AnimatedNodeLike[]): boolean {
748
+ let keys: Set<object> | undefined;
749
+ const holds = (scroller: FiberLike): boolean => (keys ??= new Set(ancestry.path.map(keyOf))).has(keyOf(scroller));
750
+ const underScroll = (): boolean =>
751
+ // Only a host has a ScrollView's (string) type: the tag only spares a lookup.
752
+ // Stryker disable next-line ConditionalExpression
753
+ ancestry.path.some((fiber) => fiber.tag === FiberTag.HostComponent && SCROLL_HOST_TYPES.has(fiber.type));
754
+ return roots.some((root) => {
755
+ // Driven from JS: it moves no content natively.
756
+ if (root.__isNative === false) {
757
+ return false;
758
+ }
759
+ const events = eventsOn(root);
760
+ if (events === undefined || (events.length === 0 && !knownDriven(root))) {
761
+ return underScroll();
762
+ }
763
+ return events.some((attachment) => {
764
+ const scroller = scrollHostOf(attachment);
765
+ if (scroller !== undefined) {
766
+ return holds(scroller);
767
+ }
768
+ // A scroll whose ScrollView is not found may be one of those above; a gesture, say, is no scroll.
769
+ return attachment.event === 'onScroll' && underScroll();
770
+ });
771
+ });
772
+ }
773
+
774
+ interface Level {
775
+ running: boolean;
776
+ roots: readonly AnimatedNodeLike[];
777
+ move: (rect: WindowRect) => WindowRect | undefined;
778
+ /** Its transform scales the view up in the shadow tree (by more than 1, or less than -1). */
779
+ scaled: boolean;
780
+ }
781
+ /** Nodes not native yet: watched, moving nothing. */
782
+ interface Pending {
783
+ running: boolean;
784
+ roots: readonly AnimatedNodeLike[];
785
+ move: undefined;
786
+ }
787
+ type NotNative = null;
788
+ const NOT_NATIVE: NotNative = null;
789
+ const UNBOUNDED_LEVEL: Level = { running: false, roots: [], move: () => undefined, scaled: false };
790
+
791
+ /**
792
+ * Every place `rect` (measured with the host's transform at whichever value
793
+ * the shadow tree holds) can be drawn as `steps` take every value they can.
794
+ */
795
+ function moveThrough(
796
+ rect: WindowRect,
797
+ host: FiberLike,
798
+ steps: readonly Step[],
799
+ origin: { x: Origin; y: Origin },
800
+ ): WindowRect | undefined {
801
+ const box = measureFiber(host);
802
+ // Without this, reading a missing box throws, which reads as unbounded too.
803
+ // Stryker disable next-line ConditionalExpression,BlockStatement -- so its mutants are equivalent
804
+ if (box == null) {
805
+ return undefined;
806
+ }
807
+ // What the shadow tree may hold: the value React rendered, or the one the last animation ended at.
808
+ const bases: Array<(step: Step) => number | undefined> = [(step) => step.rendered, (step) => step.current];
809
+ const axes: Range[] = [];
810
+ for (const axis of ['x', 'y'] as const) {
811
+ const start = axis === 'x' ? rect.x : rect.y;
812
+ const end = start + (axis === 'x' ? rect.width : rect.height);
813
+ const hostStart = axis === 'x' ? box.x : box.y;
814
+ const hostSize = axis === 'x' ? box.width : box.height;
815
+ const out = [start, end];
816
+ for (const base of bases) {
817
+ if (steps.some((step) => base(step) === undefined)) {
818
+ continue;
819
+ }
820
+ const map = compose(steps, axis, (step) => base(step) as number);
821
+ // A scale of nothing, or a mirror: no frame to go back to.
822
+ if (map.scale <= 0) {
823
+ continue;
824
+ }
825
+ const at = origin[axis];
826
+ // The origin, in the window: the host's measured box is its frame under `map` about it.
827
+ const pivot = hostStart + at.fraction * hostSize + map.scale * at.length - map.shift;
828
+ const local = [(start - pivot - map.shift) / map.scale, (end - pivot - map.shift) / map.scale];
829
+ const reach = sweep(Math.min(...local), Math.max(...local), steps, axis);
830
+ out.push(pivot + reach.lo, pivot + reach.hi);
831
+ }
832
+ // No value of the shadow tree to go back from: nothing is known.
833
+ if (out.length === 2) {
834
+ return undefined;
835
+ }
836
+ axes.push(hull(out));
837
+ }
838
+ const [x, y] = axes as [Range, Range];
839
+ return { x: x.lo, y: y.lo, width: x.hi - x.lo, height: y.hi - y.lo };
840
+ }
841
+
842
+ /** Whether `value` looks like a Reanimated shared value, which moves on the UI thread. */
843
+ function sharedValueLike(value: unknown): boolean {
844
+ const shared = value as { _isReanimatedSharedValue?: unknown; modify?: unknown } | null | undefined;
845
+ return (
846
+ shared != null &&
847
+ typeof shared === 'object' &&
848
+ (shared._isReanimatedSharedValue === true || typeof shared.modify === 'function')
849
+ );
850
+ }
851
+
852
+ /** CSS animations and transitions (Reanimated 4) can move the view without a commit. */
853
+ // No React Native style name has these words anywhere but at its start.
854
+ // Stryker disable next-line Regex
855
+ const CSS_MOTION = /^(transition|animation)/;
856
+
857
+ /**
858
+ * Whether a Reanimated component's style can move the view natively: an
859
+ * animated style that sets a transform, a shared value set inline (in the
860
+ * style or its transform), or a CSS animation or transition.
861
+ */
862
+ function reanimatedMoves(props: unknown): boolean {
863
+ const style = (props as { style?: unknown } | null | undefined)?.style;
864
+ const entries = Array.isArray(style) ? style.flat(Infinity) : [style];
865
+ return entries.some((entry) => {
866
+ const animated = entry as { viewDescriptors?: unknown; initial?: { value?: unknown } } | null | undefined;
867
+ if (animated == null || typeof animated !== 'object') {
868
+ return false;
869
+ }
870
+ if (!('viewDescriptors' in animated)) {
871
+ const fields = Object.entries(animated as Record<string, unknown>);
872
+ const transform = (animated as { transform?: unknown }).transform;
873
+ // Not a list: no shared value in it. (A string in its place is none either.)
874
+ // Stryker disable next-line ArrayDeclaration
875
+ const functions = Array.isArray(transform) ? transform.flatMap((step) => Object.values(Object(step))) : [];
876
+ return (
877
+ fields.some(([key, value]) => CSS_MOTION.test(key) || sharedValueLike(value)) || functions.some(sharedValueLike)
878
+ );
879
+ }
880
+ // Not readable: it may well move the view (a read that throws is read as unbounded, too).
881
+ // Stryker disable next-line OptionalChaining,ConditionalExpression,LogicalOperator -- so these are equivalent
882
+ const initial = animated.initial?.value;
883
+ // Stryker disable next-line ConditionalExpression,LogicalOperator -- as above
884
+ return initial == null || typeof initial !== 'object' || 'transform' in initial;
885
+ });
886
+ }
887
+
888
+ /**
889
+ * The native-driven transforms above the view, nearest first, or `null`
890
+ * when nothing above it is driven natively. Reads the props the last
891
+ * `isHidden` resolved as committed.
892
+ */
893
+ export function nativeMotion(ancestry: Ancestry): NativeMotion | null {
894
+ if (ancestry.animated.length === 0) {
895
+ return null;
896
+ }
897
+ const fibers = ancestry.resolved ?? ancestry.path;
898
+ // A ScrollView an event was attached to by its tag is looked for in the view's own tree.
899
+ searchRoot = ancestry.root;
900
+ const levels: MotionLevel[] = [];
901
+ let running = false;
902
+ const roots: AnimatedNodeLike[] = [];
903
+ // Where in the path a level the shadow tree scales up is, to unbound the levels inside it.
904
+ const scaledAt: number[] = [];
905
+ for (const index of ancestry.animated) {
906
+ let level: Level | Pending | null;
907
+ // Props and nodes are the app's: a getter on one can throw.
908
+ try {
909
+ level = levelOf(ancestry, fibers, index);
910
+ } catch {
911
+ level = UNBOUNDED_LEVEL;
912
+ }
913
+ if (level === null) {
914
+ continue;
915
+ }
916
+ running ||= level.running;
917
+ roots.push(...level.roots);
918
+ if (level.move === undefined) {
919
+ continue;
920
+ }
921
+ if (level.scaled) {
922
+ scaledAt.push(index);
923
+ }
924
+ const { move } = level;
925
+ levels.push({
926
+ index,
927
+ // An empty catch returns undefined all the same.
928
+ // Stryker disable all
929
+ move: (rect) => {
930
+ try {
931
+ return move(rect);
932
+ } catch {
933
+ return undefined;
934
+ }
935
+ },
936
+ // Stryker restore all
937
+ });
938
+ }
939
+ if (levels.length === 0 && roots.length === 0 && !running) {
940
+ return null;
941
+ }
942
+ // A level inside a scale up (a level's, or any host's committed one) is bounded in units that scale
943
+ // does not apply to.
944
+ const outermost = Math.max(-1, ...scaledAt, scaledHostAbove(fibers, levels[0]?.index));
945
+ return {
946
+ levels: levels.map((level) => (level.index < outermost ? { index: level.index, move: () => undefined } : level)),
947
+ running,
948
+ roots,
949
+ };
950
+ }
951
+
952
+ /** Whether a host's committed transform has a scale (its value now) for which `scales` holds. */
953
+ function scalesBy(props: unknown, scales: (by: unknown) => boolean): boolean {
954
+ return (transformOf(props) ?? []).some(
955
+ ([name, value]) => SCALE_AXES[name] !== undefined && scales(isNode(value) ? value.__getValue() : value),
956
+ );
957
+ }
958
+
959
+ /** Whether a host's committed transform scales by more than 1 (or less than -1), or by what cannot be read. */
960
+ function hostScalesUp(props: unknown): boolean {
961
+ return scalesBy(props, (by) => !(Math.abs(by as number) <= 1));
962
+ }
963
+
964
+ /** The outermost host above `index` in the path whose committed transform scales up; -1 when none. */
965
+ function scaledHostAbove(fibers: readonly FiberLike[], index: number | undefined): number {
966
+ if (index === undefined) {
967
+ return -1;
968
+ }
969
+ for (let above = fibers.length - 1; above > index; above -= 1) {
970
+ const fiber = fibers[above] as FiberLike;
971
+ if (fiber.tag === FiberTag.HostComponent && hostScalesUp(fiber.memoizedProps)) {
972
+ return above;
973
+ }
974
+ }
975
+ return -1;
976
+ }
977
+
978
+
979
+ /**
980
+ * Where the view, measured at `self`, can be drawn (`drawn`, before any
981
+ * clipping) and what of that is on screen (`visible`): each native transform
982
+ * level, nearest first, moves what the clippers below it left, and the
983
+ * clippers above it cut that. A level that cannot be bounded draws it anywhere
984
+ * on its surface.
985
+ */
986
+ export function throughMotion(
987
+ self: WindowRect,
988
+ ancestry: Ancestry,
989
+ motion: NativeMotion,
990
+ ): { drawn: WindowRect; visible: WindowRect | null } {
991
+ let drawn = self;
992
+ let visible: WindowRect | null = self;
993
+ let above = -1;
994
+ for (const level of motion.levels) {
995
+ drawn = level.move(drawn) ?? WHOLE_SURFACE;
996
+ if (visible !== null) {
997
+ visible = clipToAncestors(visible, ancestry, above, level.index);
998
+ }
999
+ if (visible !== null) {
1000
+ visible = level.move(visible) ?? WHOLE_SURFACE;
1001
+ }
1002
+ above = level.index;
1003
+ }
1004
+ return { drawn, visible: visible === null ? null : clipToAncestors(visible, ancestry, above) };
1005
+ }