@hanzogui/react-native-web-internals 2.0.0

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 (420) hide show
  1. package/LICENSE +21 -0
  2. package/dist/cjs/StyleSheet/__tests__/compiler-createReactDOMStyle-test.cjs +240 -0
  3. package/dist/cjs/StyleSheet/__tests__/compiler-test.cjs +407 -0
  4. package/dist/cjs/StyleSheet/__tests__/dom-createOrderedCSSStyleSheet-test.cjs +121 -0
  5. package/dist/cjs/StyleSheet/__tests__/index-test.cjs +404 -0
  6. package/dist/cjs/StyleSheet/__tests__/preprocess-test.cjs +108 -0
  7. package/dist/cjs/StyleSheet/__tests__/validate-test.cjs +36 -0
  8. package/dist/cjs/StyleSheet/compiler/createReactDOMStyle.cjs +87 -0
  9. package/dist/cjs/StyleSheet/compiler/hash.cjs +26 -0
  10. package/dist/cjs/StyleSheet/compiler/hyphenateStyleName.cjs +36 -0
  11. package/dist/cjs/StyleSheet/compiler/index.cjs +318 -0
  12. package/dist/cjs/StyleSheet/compiler/normalizeColor.cjs +40 -0
  13. package/dist/cjs/StyleSheet/compiler/normalizeValueWithProperty.cjs +43 -0
  14. package/dist/cjs/StyleSheet/compiler/resolveShadowValue.cjs +48 -0
  15. package/dist/cjs/StyleSheet/dom/createCSSStyleSheet.cjs +37 -0
  16. package/dist/cjs/StyleSheet/dom/createOrderedCSSStyleSheet.cjs +110 -0
  17. package/dist/cjs/StyleSheet/dom/index.cjs +67 -0
  18. package/dist/cjs/StyleSheet/index.cjs +57 -0
  19. package/dist/cjs/StyleSheet/preprocess.cjs +101 -0
  20. package/dist/cjs/StyleSheet/validate.cjs +69 -0
  21. package/dist/cjs/TextAncestorContext.cjs +27 -0
  22. package/dist/cjs/colorProps.cjs +37 -0
  23. package/dist/cjs/index.cjs +103 -0
  24. package/dist/cjs/modules/AccessibilityUtil/__tests__/propsToAccessibilityComponent-test.cjs +50 -0
  25. package/dist/cjs/modules/AccessibilityUtil/__tests__/propsToAriaRole-test.cjs +40 -0
  26. package/dist/cjs/modules/AccessibilityUtil/index.cjs +33 -0
  27. package/dist/cjs/modules/AccessibilityUtil/isDisabled.cjs +26 -0
  28. package/dist/cjs/modules/AccessibilityUtil/propsToAccessibilityComponent.cjs +57 -0
  29. package/dist/cjs/modules/AccessibilityUtil/propsToAriaRole.cjs +47 -0
  30. package/dist/cjs/modules/AssetRegistry/index.cjs +33 -0
  31. package/dist/cjs/modules/ImageLoader/index.cjs +111 -0
  32. package/dist/cjs/modules/InteractionManager.cjs +184 -0
  33. package/dist/cjs/modules/Platform/__tests__/index-test.cjs +48 -0
  34. package/dist/cjs/modules/Platform/index.cjs +30 -0
  35. package/dist/cjs/modules/TextInputState/index.cjs +55 -0
  36. package/dist/cjs/modules/UIManager/__tests__/index-test.cjs +90 -0
  37. package/dist/cjs/modules/UIManager/index.cjs +60 -0
  38. package/dist/cjs/modules/canUseDOM.cjs +26 -0
  39. package/dist/cjs/modules/createDOMProps/__tests__/index-test.cjs +156 -0
  40. package/dist/cjs/modules/createDOMProps/index.cjs +179 -0
  41. package/dist/cjs/modules/createEventHandle/__tests__/index-test.cjs +344 -0
  42. package/dist/cjs/modules/createEventHandle/index.cjs +67 -0
  43. package/dist/cjs/modules/dismissKeyboard/index.cjs +29 -0
  44. package/dist/cjs/modules/forwardedProps/index.cjs +161 -0
  45. package/dist/cjs/modules/getBoundingClientRect/index.cjs +28 -0
  46. package/dist/cjs/modules/invariant.cjs +36 -0
  47. package/dist/cjs/modules/isSelectionValid/index.cjs +35 -0
  48. package/dist/cjs/modules/isWebColor/index.cjs +26 -0
  49. package/dist/cjs/modules/mergeRefs/__tests__/index-test.cjs +42 -0
  50. package/dist/cjs/modules/mergeRefs/index.cjs +42 -0
  51. package/dist/cjs/modules/modality/__tests__/index-test.cjs +32 -0
  52. package/dist/cjs/modules/modality/index.cjs +135 -0
  53. package/dist/cjs/modules/multiplyStyleLengthValue/__tests__/index-test.cjs +30 -0
  54. package/dist/cjs/modules/multiplyStyleLengthValue/index.cjs +35 -0
  55. package/dist/cjs/modules/normalizeColor/index.cjs +39 -0
  56. package/dist/cjs/modules/pick/index.cjs +30 -0
  57. package/dist/cjs/modules/processColor/__tests__/index-test.cjs +66 -0
  58. package/dist/cjs/modules/processColor/index.cjs +42 -0
  59. package/dist/cjs/modules/requestIdleCallback/index.cjs +44 -0
  60. package/dist/cjs/modules/setValueForStyles/dangerousStyleValue.cjs +35 -0
  61. package/dist/cjs/modules/setValueForStyles/index.cjs +35 -0
  62. package/dist/cjs/modules/unitlessNumbers/index.cjs +85 -0
  63. package/dist/cjs/modules/useElementLayout/index.cjs +39 -0
  64. package/dist/cjs/modules/useEvent/__tests__/index-test.cjs +408 -0
  65. package/dist/cjs/modules/useEvent/index.cjs +45 -0
  66. package/dist/cjs/modules/useHover/__tests__/index-test.cjs +273 -0
  67. package/dist/cjs/modules/useHover/index.cjs +97 -0
  68. package/dist/cjs/modules/useLayoutEffect/index.cjs +39 -0
  69. package/dist/cjs/modules/useLocale/index.cjs +66 -0
  70. package/dist/cjs/modules/useLocale/isLocaleRTL.cjs +83 -0
  71. package/dist/cjs/modules/useMergeRefs/__tests__/index-test.cjs +102 -0
  72. package/dist/cjs/modules/useMergeRefs/index.cjs +43 -0
  73. package/dist/cjs/modules/usePlatformMethods/index.cjs +35 -0
  74. package/dist/cjs/modules/useStable/__tests__/index-test.cjs +92 -0
  75. package/dist/cjs/modules/useStable/index.cjs +42 -0
  76. package/dist/cjs/styleTypes.cjs +16 -0
  77. package/dist/cjs/types.cjs +16 -0
  78. package/dist/esm/StyleSheet/__tests__/compiler-createReactDOMStyle-test.mjs +219 -0
  79. package/dist/esm/StyleSheet/__tests__/compiler-createReactDOMStyle-test.mjs.map +1 -0
  80. package/dist/esm/StyleSheet/__tests__/compiler-test.mjs +408 -0
  81. package/dist/esm/StyleSheet/__tests__/compiler-test.mjs.map +1 -0
  82. package/dist/esm/StyleSheet/__tests__/dom-createOrderedCSSStyleSheet-test.mjs +100 -0
  83. package/dist/esm/StyleSheet/__tests__/dom-createOrderedCSSStyleSheet-test.mjs.map +1 -0
  84. package/dist/esm/StyleSheet/__tests__/index-test.mjs +383 -0
  85. package/dist/esm/StyleSheet/__tests__/index-test.mjs.map +1 -0
  86. package/dist/esm/StyleSheet/__tests__/preprocess-test.mjs +109 -0
  87. package/dist/esm/StyleSheet/__tests__/preprocess-test.mjs.map +1 -0
  88. package/dist/esm/StyleSheet/__tests__/validate-test.mjs +37 -0
  89. package/dist/esm/StyleSheet/__tests__/validate-test.mjs.map +1 -0
  90. package/dist/esm/StyleSheet/compiler/createReactDOMStyle.mjs +63 -0
  91. package/dist/esm/StyleSheet/compiler/createReactDOMStyle.mjs.map +1 -0
  92. package/dist/esm/StyleSheet/compiler/hash.mjs +3 -0
  93. package/dist/esm/StyleSheet/compiler/hash.mjs.map +1 -0
  94. package/dist/esm/StyleSheet/compiler/hyphenateStyleName.mjs +13 -0
  95. package/dist/esm/StyleSheet/compiler/hyphenateStyleName.mjs.map +1 -0
  96. package/dist/esm/StyleSheet/compiler/index.mjs +292 -0
  97. package/dist/esm/StyleSheet/compiler/index.mjs.map +1 -0
  98. package/dist/esm/StyleSheet/compiler/normalizeColor.mjs +17 -0
  99. package/dist/esm/StyleSheet/compiler/normalizeColor.mjs.map +1 -0
  100. package/dist/esm/StyleSheet/compiler/normalizeValueWithProperty.mjs +20 -0
  101. package/dist/esm/StyleSheet/compiler/normalizeValueWithProperty.mjs.map +1 -0
  102. package/dist/esm/StyleSheet/compiler/resolveShadowValue.mjs +25 -0
  103. package/dist/esm/StyleSheet/compiler/resolveShadowValue.mjs.map +1 -0
  104. package/dist/esm/StyleSheet/dom/createCSSStyleSheet.mjs +14 -0
  105. package/dist/esm/StyleSheet/dom/createCSSStyleSheet.mjs.map +1 -0
  106. package/dist/esm/StyleSheet/dom/createOrderedCSSStyleSheet.mjs +87 -0
  107. package/dist/esm/StyleSheet/dom/createOrderedCSSStyleSheet.mjs.map +1 -0
  108. package/dist/esm/StyleSheet/dom/index.mjs +44 -0
  109. package/dist/esm/StyleSheet/dom/index.mjs.map +1 -0
  110. package/dist/esm/StyleSheet/index.mjs +33 -0
  111. package/dist/esm/StyleSheet/index.mjs.map +1 -0
  112. package/dist/esm/StyleSheet/preprocess.mjs +75 -0
  113. package/dist/esm/StyleSheet/preprocess.mjs.map +1 -0
  114. package/dist/esm/StyleSheet/validate.mjs +46 -0
  115. package/dist/esm/StyleSheet/validate.mjs.map +1 -0
  116. package/dist/esm/TextAncestorContext.mjs +4 -0
  117. package/dist/esm/TextAncestorContext.mjs.map +1 -0
  118. package/dist/esm/colorProps.mjs +14 -0
  119. package/dist/esm/colorProps.mjs.map +1 -0
  120. package/dist/esm/index.js +45 -0
  121. package/dist/esm/index.js.map +1 -0
  122. package/dist/esm/index.mjs +45 -0
  123. package/dist/esm/index.mjs.map +1 -0
  124. package/dist/esm/modules/AccessibilityUtil/__tests__/propsToAccessibilityComponent-test.mjs +29 -0
  125. package/dist/esm/modules/AccessibilityUtil/__tests__/propsToAccessibilityComponent-test.mjs.map +1 -0
  126. package/dist/esm/modules/AccessibilityUtil/__tests__/propsToAriaRole-test.mjs +19 -0
  127. package/dist/esm/modules/AccessibilityUtil/__tests__/propsToAriaRole-test.mjs.map +1 -0
  128. package/dist/esm/modules/AccessibilityUtil/index.mjs +10 -0
  129. package/dist/esm/modules/AccessibilityUtil/index.mjs.map +1 -0
  130. package/dist/esm/modules/AccessibilityUtil/isDisabled.mjs +3 -0
  131. package/dist/esm/modules/AccessibilityUtil/isDisabled.mjs.map +1 -0
  132. package/dist/esm/modules/AccessibilityUtil/propsToAccessibilityComponent.mjs +34 -0
  133. package/dist/esm/modules/AccessibilityUtil/propsToAccessibilityComponent.mjs.map +1 -0
  134. package/dist/esm/modules/AccessibilityUtil/propsToAriaRole.mjs +24 -0
  135. package/dist/esm/modules/AccessibilityUtil/propsToAriaRole.mjs.map +1 -0
  136. package/dist/esm/modules/AssetRegistry/index.mjs +9 -0
  137. package/dist/esm/modules/AssetRegistry/index.mjs.map +1 -0
  138. package/dist/esm/modules/ImageLoader/index.mjs +87 -0
  139. package/dist/esm/modules/ImageLoader/index.mjs.map +1 -0
  140. package/dist/esm/modules/InteractionManager.mjs +161 -0
  141. package/dist/esm/modules/InteractionManager.mjs.map +1 -0
  142. package/dist/esm/modules/Platform/__tests__/index-test.mjs +27 -0
  143. package/dist/esm/modules/Platform/__tests__/index-test.mjs.map +1 -0
  144. package/dist/esm/modules/Platform/index.mjs +7 -0
  145. package/dist/esm/modules/Platform/index.mjs.map +1 -0
  146. package/dist/esm/modules/TextInputState/index.mjs +32 -0
  147. package/dist/esm/modules/TextInputState/index.mjs.map +1 -0
  148. package/dist/esm/modules/UIManager/__tests__/index-test.mjs +69 -0
  149. package/dist/esm/modules/UIManager/__tests__/index-test.mjs.map +1 -0
  150. package/dist/esm/modules/UIManager/index.mjs +37 -0
  151. package/dist/esm/modules/UIManager/index.mjs.map +1 -0
  152. package/dist/esm/modules/canUseDOM.mjs +3 -0
  153. package/dist/esm/modules/canUseDOM.mjs.map +1 -0
  154. package/dist/esm/modules/createDOMProps/__tests__/index-test.mjs +135 -0
  155. package/dist/esm/modules/createDOMProps/__tests__/index-test.mjs.map +1 -0
  156. package/dist/esm/modules/createDOMProps/index.mjs +155 -0
  157. package/dist/esm/modules/createDOMProps/index.mjs.map +1 -0
  158. package/dist/esm/modules/createEventHandle/__tests__/index-test.mjs +323 -0
  159. package/dist/esm/modules/createEventHandle/__tests__/index-test.mjs.map +1 -0
  160. package/dist/esm/modules/createEventHandle/index.mjs +42 -0
  161. package/dist/esm/modules/createEventHandle/index.mjs.map +1 -0
  162. package/dist/esm/modules/dismissKeyboard/index.mjs +6 -0
  163. package/dist/esm/modules/dismissKeyboard/index.mjs.map +1 -0
  164. package/dist/esm/modules/forwardedProps/index.mjs +128 -0
  165. package/dist/esm/modules/forwardedProps/index.mjs.map +1 -0
  166. package/dist/esm/modules/getBoundingClientRect/index.mjs +5 -0
  167. package/dist/esm/modules/getBoundingClientRect/index.mjs.map +1 -0
  168. package/dist/esm/modules/invariant.mjs +12 -0
  169. package/dist/esm/modules/invariant.mjs.map +1 -0
  170. package/dist/esm/modules/isSelectionValid/index.mjs +12 -0
  171. package/dist/esm/modules/isSelectionValid/index.mjs.map +1 -0
  172. package/dist/esm/modules/isWebColor/index.mjs +3 -0
  173. package/dist/esm/modules/isWebColor/index.mjs.map +1 -0
  174. package/dist/esm/modules/mergeRefs/__tests__/index-test.mjs +21 -0
  175. package/dist/esm/modules/mergeRefs/__tests__/index-test.mjs.map +1 -0
  176. package/dist/esm/modules/mergeRefs/index.mjs +19 -0
  177. package/dist/esm/modules/mergeRefs/index.mjs.map +1 -0
  178. package/dist/esm/modules/modality/__tests__/index-test.mjs +33 -0
  179. package/dist/esm/modules/modality/__tests__/index-test.mjs.map +1 -0
  180. package/dist/esm/modules/modality/index.mjs +109 -0
  181. package/dist/esm/modules/modality/index.mjs.map +1 -0
  182. package/dist/esm/modules/multiplyStyleLengthValue/__tests__/index-test.mjs +9 -0
  183. package/dist/esm/modules/multiplyStyleLengthValue/__tests__/index-test.mjs.map +1 -0
  184. package/dist/esm/modules/multiplyStyleLengthValue/index.mjs +12 -0
  185. package/dist/esm/modules/multiplyStyleLengthValue/index.mjs.map +1 -0
  186. package/dist/esm/modules/normalizeColor/index.mjs +16 -0
  187. package/dist/esm/modules/normalizeColor/index.mjs.map +1 -0
  188. package/dist/esm/modules/pick/index.mjs +7 -0
  189. package/dist/esm/modules/pick/index.mjs.map +1 -0
  190. package/dist/esm/modules/processColor/__tests__/index-test.mjs +45 -0
  191. package/dist/esm/modules/processColor/__tests__/index-test.mjs.map +1 -0
  192. package/dist/esm/modules/processColor/index.mjs +8 -0
  193. package/dist/esm/modules/processColor/index.mjs.map +1 -0
  194. package/dist/esm/modules/requestIdleCallback/index.mjs +20 -0
  195. package/dist/esm/modules/requestIdleCallback/index.mjs.map +1 -0
  196. package/dist/esm/modules/setValueForStyles/dangerousStyleValue.mjs +12 -0
  197. package/dist/esm/modules/setValueForStyles/dangerousStyleValue.mjs.map +1 -0
  198. package/dist/esm/modules/setValueForStyles/index.mjs +12 -0
  199. package/dist/esm/modules/setValueForStyles/index.mjs.map +1 -0
  200. package/dist/esm/modules/unitlessNumbers/index.mjs +62 -0
  201. package/dist/esm/modules/unitlessNumbers/index.mjs.map +1 -0
  202. package/dist/esm/modules/useElementLayout/index.mjs +16 -0
  203. package/dist/esm/modules/useElementLayout/index.mjs.map +1 -0
  204. package/dist/esm/modules/useEvent/__tests__/index-test.mjs +387 -0
  205. package/dist/esm/modules/useEvent/__tests__/index-test.mjs.map +1 -0
  206. package/dist/esm/modules/useEvent/index.mjs +22 -0
  207. package/dist/esm/modules/useEvent/index.mjs.map +1 -0
  208. package/dist/esm/modules/useHover/__tests__/index-test.mjs +252 -0
  209. package/dist/esm/modules/useHover/__tests__/index-test.mjs.map +1 -0
  210. package/dist/esm/modules/useHover/index.mjs +74 -0
  211. package/dist/esm/modules/useHover/index.mjs.map +1 -0
  212. package/dist/esm/modules/useLayoutEffect/index.mjs +5 -0
  213. package/dist/esm/modules/useLayoutEffect/index.mjs.map +1 -0
  214. package/dist/esm/modules/useLocale/index.mjs +30 -0
  215. package/dist/esm/modules/useLocale/index.mjs.map +1 -0
  216. package/dist/esm/modules/useLocale/isLocaleRTL.mjs +60 -0
  217. package/dist/esm/modules/useLocale/isLocaleRTL.mjs.map +1 -0
  218. package/dist/esm/modules/useMergeRefs/__tests__/index-test.mjs +81 -0
  219. package/dist/esm/modules/useMergeRefs/__tests__/index-test.mjs.map +1 -0
  220. package/dist/esm/modules/useMergeRefs/index.mjs +9 -0
  221. package/dist/esm/modules/useMergeRefs/index.mjs.map +1 -0
  222. package/dist/esm/modules/usePlatformMethods/index.mjs +12 -0
  223. package/dist/esm/modules/usePlatformMethods/index.mjs.map +1 -0
  224. package/dist/esm/modules/useStable/__tests__/index-test.mjs +71 -0
  225. package/dist/esm/modules/useStable/__tests__/index-test.mjs.map +1 -0
  226. package/dist/esm/modules/useStable/index.mjs +8 -0
  227. package/dist/esm/modules/useStable/index.mjs.map +1 -0
  228. package/dist/esm/styleTypes.mjs +2 -0
  229. package/dist/esm/styleTypes.mjs.map +1 -0
  230. package/dist/esm/types.mjs +2 -0
  231. package/dist/esm/types.mjs.map +1 -0
  232. package/package.json +58 -0
  233. package/src/StyleSheet/__tests__/compiler-createReactDOMStyle-test.tsx +236 -0
  234. package/src/StyleSheet/__tests__/compiler-test.tsx +426 -0
  235. package/src/StyleSheet/__tests__/dom-createOrderedCSSStyleSheet-test.tsx +169 -0
  236. package/src/StyleSheet/__tests__/index-test.tsx +406 -0
  237. package/src/StyleSheet/__tests__/preprocess-test.tsx +107 -0
  238. package/src/StyleSheet/__tests__/validate-test.tsx +42 -0
  239. package/src/StyleSheet/compiler/createReactDOMStyle.tsx +215 -0
  240. package/src/StyleSheet/compiler/hash.tsx +1 -0
  241. package/src/StyleSheet/compiler/hyphenateStyleName.tsx +25 -0
  242. package/src/StyleSheet/compiler/index.tsx +478 -0
  243. package/src/StyleSheet/compiler/normalizeColor.tsx +35 -0
  244. package/src/StyleSheet/compiler/normalizeValueWithProperty.tsx +34 -0
  245. package/src/StyleSheet/compiler/resolveShadowValue.tsx +26 -0
  246. package/src/StyleSheet/dom/createCSSStyleSheet.tsx +42 -0
  247. package/src/StyleSheet/dom/createOrderedCSSStyleSheet.tsx +195 -0
  248. package/src/StyleSheet/dom/index.tsx +92 -0
  249. package/src/StyleSheet/index.tsx +99 -0
  250. package/src/StyleSheet/preprocess.tsx +119 -0
  251. package/src/StyleSheet/validate.tsx +90 -0
  252. package/src/TextAncestorContext.tsx +13 -0
  253. package/src/colorProps.tsx +12 -0
  254. package/src/index.tsx +58 -0
  255. package/src/modules/AccessibilityUtil/__tests__/propsToAccessibilityComponent-test.tsx +44 -0
  256. package/src/modules/AccessibilityUtil/__tests__/propsToAriaRole-test.tsx +25 -0
  257. package/src/modules/AccessibilityUtil/index.tsx +18 -0
  258. package/src/modules/AccessibilityUtil/isDisabled.tsx +13 -0
  259. package/src/modules/AccessibilityUtil/propsToAccessibilityComponent.tsx +54 -0
  260. package/src/modules/AccessibilityUtil/propsToAriaRole.tsx +37 -0
  261. package/src/modules/AssetRegistry/index.tsx +32 -0
  262. package/src/modules/ImageLoader/index.tsx +165 -0
  263. package/src/modules/InteractionManager.tsx +315 -0
  264. package/src/modules/Platform/__tests__/index-test.tsx +45 -0
  265. package/src/modules/Platform/index.tsx +15 -0
  266. package/src/modules/TextInputState/index.tsx +62 -0
  267. package/src/modules/UIManager/__tests__/index-test.tsx +87 -0
  268. package/src/modules/UIManager/index.tsx +68 -0
  269. package/src/modules/canUseDOM.tsx +14 -0
  270. package/src/modules/createDOMProps/__tests__/index-test.tsx +138 -0
  271. package/src/modules/createDOMProps/index.tsx +436 -0
  272. package/src/modules/createEventHandle/__tests__/index-test.tsx +459 -0
  273. package/src/modules/createEventHandle/index.tsx +100 -0
  274. package/src/modules/dismissKeyboard/index.tsx +14 -0
  275. package/src/modules/forwardedProps/index.tsx +145 -0
  276. package/src/modules/getBoundingClientRect/index.tsx +17 -0
  277. package/src/modules/invariant.ts +23 -0
  278. package/src/modules/isSelectionValid/index.tsx +20 -0
  279. package/src/modules/isWebColor/index.tsx +14 -0
  280. package/src/modules/mergeRefs/__tests__/index-test.tsx +32 -0
  281. package/src/modules/mergeRefs/index.tsx +36 -0
  282. package/src/modules/modality/__tests__/index-test.tsx +66 -0
  283. package/src/modules/modality/index.tsx +228 -0
  284. package/src/modules/multiplyStyleLengthValue/__tests__/index-test.tsx +23 -0
  285. package/src/modules/multiplyStyleLengthValue/index.tsx +26 -0
  286. package/src/modules/normalizeColor/index.tsx +32 -0
  287. package/src/modules/pick/index.tsx +25 -0
  288. package/src/modules/processColor/__tests__/index-test.tsx +78 -0
  289. package/src/modules/processColor/index.tsx +28 -0
  290. package/src/modules/requestIdleCallback/index.tsx +35 -0
  291. package/src/modules/setValueForStyles/dangerousStyleValue.tsx +74 -0
  292. package/src/modules/setValueForStyles/index.tsx +39 -0
  293. package/src/modules/unitlessNumbers/index.tsx +77 -0
  294. package/src/modules/useElementLayout/index.tsx +28 -0
  295. package/src/modules/useEvent/__tests__/index-test.tsx +511 -0
  296. package/src/modules/useEvent/index.tsx +64 -0
  297. package/src/modules/useHover/__tests__/index-test.tsx +342 -0
  298. package/src/modules/useHover/index.tsx +181 -0
  299. package/src/modules/useLayoutEffect/index.ts +15 -0
  300. package/src/modules/useLocale/index.tsx +57 -0
  301. package/src/modules/useLocale/isLocaleRTL.tsx +74 -0
  302. package/src/modules/useMergeRefs/__tests__/index-test.tsx +91 -0
  303. package/src/modules/useMergeRefs/index.tsx +22 -0
  304. package/src/modules/usePlatformMethods/index.tsx +40 -0
  305. package/src/modules/useStable/__tests__/index-test.tsx +101 -0
  306. package/src/modules/useStable/index.tsx +24 -0
  307. package/src/styleTypes.ts +321 -0
  308. package/src/types.ts +77 -0
  309. package/types/StyleSheet/compiler/createReactDOMStyle.d.ts +18 -0
  310. package/types/StyleSheet/compiler/createReactDOMStyle.d.ts.map +1 -0
  311. package/types/StyleSheet/compiler/hash.d.ts +2 -0
  312. package/types/StyleSheet/compiler/hash.d.ts.map +1 -0
  313. package/types/StyleSheet/compiler/hyphenateStyleName.d.ts +10 -0
  314. package/types/StyleSheet/compiler/hyphenateStyleName.d.ts.map +1 -0
  315. package/types/StyleSheet/compiler/index.d.ts +39 -0
  316. package/types/StyleSheet/compiler/index.d.ts.map +1 -0
  317. package/types/StyleSheet/compiler/normalizeColor.d.ts +10 -0
  318. package/types/StyleSheet/compiler/normalizeColor.d.ts.map +1 -0
  319. package/types/StyleSheet/compiler/normalizeValueWithProperty.d.ts +10 -0
  320. package/types/StyleSheet/compiler/normalizeValueWithProperty.d.ts.map +1 -0
  321. package/types/StyleSheet/compiler/resolveShadowValue.d.ts +10 -0
  322. package/types/StyleSheet/compiler/resolveShadowValue.d.ts.map +1 -0
  323. package/types/StyleSheet/dom/createCSSStyleSheet.d.ts +10 -0
  324. package/types/StyleSheet/dom/createCSSStyleSheet.d.ts.map +1 -0
  325. package/types/StyleSheet/dom/createOrderedCSSStyleSheet.d.ts +29 -0
  326. package/types/StyleSheet/dom/createOrderedCSSStyleSheet.d.ts.map +1 -0
  327. package/types/StyleSheet/dom/index.d.ts +15 -0
  328. package/types/StyleSheet/dom/index.d.ts.map +1 -0
  329. package/types/StyleSheet/index.d.ts +69 -0
  330. package/types/StyleSheet/index.d.ts.map +1 -0
  331. package/types/StyleSheet/preprocess.d.ts +16 -0
  332. package/types/StyleSheet/preprocess.d.ts.map +1 -0
  333. package/types/StyleSheet/validate.d.ts +10 -0
  334. package/types/StyleSheet/validate.d.ts.map +1 -0
  335. package/types/TextAncestorContext.d.ts +11 -0
  336. package/types/TextAncestorContext.d.ts.map +1 -0
  337. package/types/colorProps.d.ts +13 -0
  338. package/types/colorProps.d.ts.map +1 -0
  339. package/types/index.d.ts +45 -0
  340. package/types/index.d.ts.map +1 -0
  341. package/types/modules/AccessibilityUtil/index.d.ts +16 -0
  342. package/types/modules/AccessibilityUtil/index.d.ts.map +1 -0
  343. package/types/modules/AccessibilityUtil/isDisabled.d.ts +10 -0
  344. package/types/modules/AccessibilityUtil/isDisabled.d.ts.map +1 -0
  345. package/types/modules/AccessibilityUtil/propsToAccessibilityComponent.d.ts +10 -0
  346. package/types/modules/AccessibilityUtil/propsToAccessibilityComponent.d.ts.map +1 -0
  347. package/types/modules/AccessibilityUtil/propsToAriaRole.d.ts +12 -0
  348. package/types/modules/AccessibilityUtil/propsToAriaRole.d.ts.map +1 -0
  349. package/types/modules/AssetRegistry/index.d.ts +22 -0
  350. package/types/modules/AssetRegistry/index.d.ts.map +1 -0
  351. package/types/modules/ImageLoader/index.d.ts +27 -0
  352. package/types/modules/ImageLoader/index.d.ts.map +1 -0
  353. package/types/modules/InteractionManager.d.ts +48 -0
  354. package/types/modules/InteractionManager.d.ts.map +1 -0
  355. package/types/modules/Platform/index.d.ts +15 -0
  356. package/types/modules/Platform/index.d.ts.map +1 -0
  357. package/types/modules/TextInputState/index.d.ts +38 -0
  358. package/types/modules/TextInputState/index.d.ts.map +1 -0
  359. package/types/modules/UIManager/index.d.ts +23 -0
  360. package/types/modules/UIManager/index.d.ts.map +1 -0
  361. package/types/modules/canUseDOM.d.ts +10 -0
  362. package/types/modules/canUseDOM.d.ts.map +1 -0
  363. package/types/modules/createDOMProps/index.d.ts +12 -0
  364. package/types/modules/createDOMProps/index.d.ts.map +1 -0
  365. package/types/modules/createEventHandle/index.d.ts +20 -0
  366. package/types/modules/createEventHandle/index.d.ts.map +1 -0
  367. package/types/modules/dismissKeyboard/index.d.ts +10 -0
  368. package/types/modules/dismissKeyboard/index.d.ts.map +1 -0
  369. package/types/modules/forwardedProps/index.d.ts +383 -0
  370. package/types/modules/forwardedProps/index.d.ts.map +1 -0
  371. package/types/modules/getBoundingClientRect/index.d.ts +10 -0
  372. package/types/modules/getBoundingClientRect/index.d.ts.map +1 -0
  373. package/types/modules/invariant.d.ts +3 -0
  374. package/types/modules/invariant.d.ts.map +1 -0
  375. package/types/modules/isSelectionValid/index.d.ts +10 -0
  376. package/types/modules/isSelectionValid/index.d.ts.map +1 -0
  377. package/types/modules/isWebColor/index.d.ts +10 -0
  378. package/types/modules/isWebColor/index.d.ts.map +1 -0
  379. package/types/modules/mergeRefs/index.d.ts +11 -0
  380. package/types/modules/mergeRefs/index.d.ts.map +1 -0
  381. package/types/modules/modality/index.d.ts +17 -0
  382. package/types/modules/modality/index.d.ts.map +1 -0
  383. package/types/modules/multiplyStyleLengthValue/index.d.ts +10 -0
  384. package/types/modules/multiplyStyleLengthValue/index.d.ts.map +1 -0
  385. package/types/modules/normalizeColor/index.d.ts +10 -0
  386. package/types/modules/normalizeColor/index.d.ts.map +1 -0
  387. package/types/modules/pick/index.d.ts +12 -0
  388. package/types/modules/pick/index.d.ts.map +1 -0
  389. package/types/modules/processColor/index.d.ts +11 -0
  390. package/types/modules/processColor/index.d.ts.map +1 -0
  391. package/types/modules/requestIdleCallback/index.d.ts +3 -0
  392. package/types/modules/requestIdleCallback/index.d.ts.map +1 -0
  393. package/types/modules/setValueForStyles/dangerousStyleValue.d.ts +11 -0
  394. package/types/modules/setValueForStyles/dangerousStyleValue.d.ts.map +1 -0
  395. package/types/modules/setValueForStyles/index.d.ts +9 -0
  396. package/types/modules/setValueForStyles/index.d.ts.map +1 -0
  397. package/types/modules/unitlessNumbers/index.d.ts +60 -0
  398. package/types/modules/unitlessNumbers/index.d.ts.map +1 -0
  399. package/types/modules/useElementLayout/index.d.ts +4 -0
  400. package/types/modules/useElementLayout/index.d.ts.map +1 -0
  401. package/types/modules/useEvent/index.d.ts +24 -0
  402. package/types/modules/useEvent/index.d.ts.map +1 -0
  403. package/types/modules/useHover/index.d.ts +21 -0
  404. package/types/modules/useHover/index.d.ts.map +1 -0
  405. package/types/modules/useLayoutEffect/index.d.ts +13 -0
  406. package/types/modules/useLayoutEffect/index.d.ts.map +1 -0
  407. package/types/modules/useLocale/index.d.ts +14 -0
  408. package/types/modules/useLocale/index.d.ts.map +1 -0
  409. package/types/modules/useLocale/isLocaleRTL.d.ts +13 -0
  410. package/types/modules/useLocale/isLocaleRTL.d.ts.map +1 -0
  411. package/types/modules/useMergeRefs/index.d.ts +11 -0
  412. package/types/modules/useMergeRefs/index.d.ts.map +1 -0
  413. package/types/modules/usePlatformMethods/index.d.ts +18 -0
  414. package/types/modules/usePlatformMethods/index.d.ts.map +1 -0
  415. package/types/modules/useStable/index.d.ts +10 -0
  416. package/types/modules/useStable/index.d.ts.map +1 -0
  417. package/types/styleTypes.d.ts +211 -0
  418. package/types/styleTypes.d.ts.map +1 -0
  419. package/types/types.d.ts +46 -0
  420. package/types/types.d.ts.map +1 -0
@@ -0,0 +1,44 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import propsToAccessibilityComponent from '../propsToAccessibilityComponent'
9
+
10
+ describe('modules/AccessibilityUtil/propsToAccessibilityComponent', () => {
11
+ test('when missing accessibility props"', () => {
12
+ expect(propsToAccessibilityComponent({})).toBeUndefined()
13
+ })
14
+
15
+ test('when "accessibilityRole" is "button"', () => {
16
+ expect(propsToAccessibilityComponent({ accessibilityRole: 'button' })).toBeUndefined()
17
+ })
18
+
19
+ test('when "accessibilityRole" is "heading"', () => {
20
+ expect(propsToAccessibilityComponent({ accessibilityRole: 'heading' })).toEqual('h1')
21
+ })
22
+
23
+ test('when "accessibilityRole" is "heading" and "aria-level" is set', () => {
24
+ expect(
25
+ propsToAccessibilityComponent({
26
+ accessibilityRole: 'heading',
27
+ 'aria-level': 3,
28
+ })
29
+ ).toEqual('h3')
30
+ })
31
+
32
+ test('when "accessibilityRole" is "heading" and "accessibilityLevel" is set', () => {
33
+ expect(
34
+ propsToAccessibilityComponent({
35
+ accessibilityRole: 'heading',
36
+ accessibilityLevel: 3,
37
+ })
38
+ ).toEqual('h3')
39
+ })
40
+
41
+ test('when "accessibilityRole" is "label"', () => {
42
+ expect(propsToAccessibilityComponent({ accessibilityRole: 'label' })).toEqual('label')
43
+ })
44
+ })
@@ -0,0 +1,25 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ */
7
+
8
+ import propsToAriaRole from '../propsToAriaRole'
9
+
10
+ describe('modules/AccessibilityUtil/propsToAriaRole', () => {
11
+ test('when missing accessibility props', () => {
12
+ expect(propsToAriaRole({})).toBeUndefined()
13
+ })
14
+
15
+ test('when "accessibilityRole" is defined', () => {
16
+ expect(propsToAriaRole({ accessibilityRole: 'banner' })).toEqual('banner')
17
+ })
18
+
19
+ test('when "accessibilityRole" is a native-only value', () => {
20
+ expect(propsToAriaRole({ accessibilityRole: 'none' })).toEqual('presentation')
21
+ expect(propsToAriaRole({ accessibilityRole: 'imagebutton' })).toEqual(undefined)
22
+ // not really native-only, but used to allow Web to render <label> around TextInput
23
+ expect(propsToAriaRole({ accessibilityRole: 'label' })).toEqual(undefined)
24
+ })
25
+ })
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ */
9
+
10
+ import { isDisabled } from './isDisabled'
11
+ import { propsToAccessibilityComponent } from './propsToAccessibilityComponent'
12
+ import { propsToAriaRole } from './propsToAriaRole'
13
+
14
+ export const AccessibilityUtil = {
15
+ isDisabled,
16
+ propsToAccessibilityComponent,
17
+ propsToAriaRole,
18
+ }
@@ -0,0 +1,13 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ */
9
+
10
+ export const isDisabled = (props: Record<string, any>): boolean =>
11
+ props.disabled ||
12
+ (Array.isArray(props.accessibilityStates) &&
13
+ props.accessibilityStates.indexOf('disabled') > -1)
@@ -0,0 +1,54 @@
1
+ // @ts-nocheck
2
+ /**
3
+ * Copyright (c) Nicolas Gallagher.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ *
8
+ * @flow
9
+ */
10
+
11
+ import { propsToAriaRole } from './propsToAriaRole'
12
+
13
+ const roleComponents = {
14
+ article: 'article',
15
+ banner: 'header',
16
+ blockquote: 'blockquote',
17
+ code: 'code',
18
+ complementary: 'aside',
19
+ contentinfo: 'footer',
20
+ deletion: 'del',
21
+ emphasis: 'em',
22
+ figure: 'figure',
23
+ insertion: 'ins',
24
+ form: 'form',
25
+ list: 'ul',
26
+ listitem: 'li',
27
+ main: 'main',
28
+ navigation: 'nav',
29
+ region: 'section',
30
+ strong: 'strong',
31
+ }
32
+
33
+ const emptyObject = {}
34
+
35
+ export const propsToAccessibilityComponent = (
36
+ props: object = emptyObject
37
+ ): void | string => {
38
+ // special-case for "label" role which doesn't map to an ARIA role
39
+ if (props.accessibilityRole === 'label') {
40
+ return 'label'
41
+ }
42
+
43
+ const role = propsToAriaRole(props)
44
+ if (role) {
45
+ if (role === 'heading') {
46
+ const level = props.accessibilityLevel || props['aria-level']
47
+ if (level != null) {
48
+ return `h${level}`
49
+ }
50
+ return 'h1'
51
+ }
52
+ return roleComponents[role]
53
+ }
54
+ }
@@ -0,0 +1,37 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ */
9
+
10
+ const accessibilityRoleToWebRole = {
11
+ adjustable: 'slider',
12
+ button: 'button',
13
+ header: 'heading',
14
+ image: 'img',
15
+ imagebutton: null,
16
+ keyboardkey: null,
17
+ label: null,
18
+ link: 'link',
19
+ none: 'presentation',
20
+ search: 'search',
21
+ summary: 'region',
22
+ text: null,
23
+ }
24
+
25
+ export const propsToAriaRole = ({
26
+ accessibilityRole,
27
+ }: {
28
+ accessibilityRole?: string
29
+ }): string | void => {
30
+ if (accessibilityRole) {
31
+ const inferredRole = accessibilityRoleToWebRole[accessibilityRole]
32
+ if (inferredRole !== null) {
33
+ // ignore roles that don't map to web
34
+ return inferredRole || accessibilityRole
35
+ }
36
+ }
37
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ */
9
+
10
+ export type PackagerAsset = {
11
+ __packager_asset: boolean
12
+ fileSystemLocation: string
13
+ httpServerLocation: string
14
+ width: number | null
15
+ height: number | null
16
+ scales: Array<number>
17
+ hash: string
18
+ name: string
19
+ type: string
20
+ }
21
+
22
+ const assets: Array<PackagerAsset> = []
23
+
24
+ export function registerAsset(asset: PackagerAsset): number {
25
+ // `push` returns new array length, so the first asset will
26
+ // get id 1 (not 0) to make the value truthy
27
+ return assets.push(asset)
28
+ }
29
+
30
+ export function getAssetByID(assetId: number): PackagerAsset {
31
+ return assets[assetId - 1]
32
+ }
@@ -0,0 +1,165 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ *
4
+ * This source code is licensed under the MIT license found in the
5
+ * LICENSE file in the root directory of this source tree.
6
+ *
7
+ * @flow
8
+ */
9
+
10
+ const dataUriPattern = /^data:/
11
+
12
+ export class ImageUriCache {
13
+ static _maximumEntries = 256
14
+ static _entries = {}
15
+
16
+ static has(uri: string): boolean {
17
+ const entries = ImageUriCache._entries
18
+ const isDataUri = dataUriPattern.test(uri)
19
+ return isDataUri || Boolean(entries[uri])
20
+ }
21
+
22
+ static add(uri: string) {
23
+ const entries = ImageUriCache._entries
24
+ const lastUsedTimestamp = Date.now()
25
+ if (entries[uri]) {
26
+ entries[uri].lastUsedTimestamp = lastUsedTimestamp
27
+ entries[uri].refCount += 1
28
+ } else {
29
+ entries[uri] = {
30
+ lastUsedTimestamp,
31
+ refCount: 1,
32
+ }
33
+ }
34
+ }
35
+
36
+ static remove(uri: string) {
37
+ const entries = ImageUriCache._entries
38
+ if (entries[uri]) {
39
+ entries[uri].refCount -= 1
40
+ }
41
+ // Free up entries when the cache is "full"
42
+ ImageUriCache._cleanUpIfNeeded()
43
+ }
44
+
45
+ static _cleanUpIfNeeded() {
46
+ const entries = ImageUriCache._entries
47
+ const imageUris = Object.keys(entries)
48
+
49
+ if (imageUris.length + 1 > ImageUriCache._maximumEntries) {
50
+ let leastRecentlyUsedKey
51
+ let leastRecentlyUsedEntry
52
+
53
+ imageUris.forEach((uri) => {
54
+ const entry = entries[uri]
55
+ if (
56
+ (!leastRecentlyUsedEntry ||
57
+ entry.lastUsedTimestamp < leastRecentlyUsedEntry.lastUsedTimestamp) &&
58
+ entry.refCount === 0
59
+ ) {
60
+ leastRecentlyUsedKey = uri
61
+ leastRecentlyUsedEntry = entry
62
+ }
63
+ })
64
+
65
+ if (leastRecentlyUsedKey) {
66
+ delete entries[leastRecentlyUsedKey]
67
+ }
68
+ }
69
+ }
70
+ }
71
+
72
+ let id = 0
73
+ const requests = {}
74
+
75
+ export const ImageLoader = {
76
+ abort(requestId: number) {
77
+ let image = requests[`${requestId}`]
78
+ if (image) {
79
+ image.onerror = null
80
+ image.onload = null
81
+ image = null
82
+ delete requests[`${requestId}`]
83
+ }
84
+ },
85
+ getSize(
86
+ uri: string,
87
+ success: (width: number, height: number) => void,
88
+ failure: () => void
89
+ ) {
90
+ let complete = false
91
+ const interval = setInterval(callback, 16)
92
+ const requestId = ImageLoader.load(uri, callback, errorCallback)
93
+
94
+ function callback() {
95
+ const image = requests[`${requestId}`]
96
+ if (image) {
97
+ const { naturalHeight, naturalWidth } = image
98
+ if (naturalHeight && naturalWidth) {
99
+ success(naturalWidth, naturalHeight)
100
+ complete = true
101
+ }
102
+ }
103
+ if (complete) {
104
+ ImageLoader.abort(requestId)
105
+ clearInterval(interval)
106
+ }
107
+ }
108
+
109
+ function errorCallback() {
110
+ if (typeof failure === 'function') {
111
+ failure()
112
+ }
113
+ ImageLoader.abort(requestId)
114
+ clearInterval(interval)
115
+ }
116
+ },
117
+ has(uri: string): boolean {
118
+ return ImageUriCache.has(uri)
119
+ },
120
+ load(uri: string, onLoad: Function, onError: Function): number {
121
+ id += 1
122
+ const image = new window.Image()
123
+ // @ts-ignore
124
+ image.onerror = onError
125
+ image.onload = (e) => {
126
+ // avoid blocking the main thread
127
+ const onDecode = () => onLoad({ nativeEvent: e })
128
+ if (typeof image.decode === 'function') {
129
+ // Safari currently throws exceptions when decoding svgs.
130
+ // We want to catch that error and allow the load handler
131
+ // to be forwarded to the onLoad handler in this case
132
+ image.decode().then(onDecode, onDecode)
133
+ } else {
134
+ setTimeout(onDecode, 0)
135
+ }
136
+ }
137
+ image.src = uri
138
+ requests[`${id}`] = image
139
+ return id
140
+ },
141
+ prefetch(uri: string): Promise<void> {
142
+ return new Promise((resolve, reject) => {
143
+ ImageLoader.load(
144
+ uri,
145
+ () => {
146
+ // Add the uri to the cache so it can be immediately displayed when used
147
+ // but also immediately remove it to correctly reflect that it has no active references
148
+ ImageUriCache.add(uri)
149
+ ImageUriCache.remove(uri)
150
+ resolve()
151
+ },
152
+ reject
153
+ )
154
+ })
155
+ },
156
+ queryCache(uris: Array<string>): Promise<{ [uri: string]: 'disk/memory' }> {
157
+ const result = {}
158
+ uris.forEach((u) => {
159
+ if (ImageUriCache.has(u)) {
160
+ result[u] = 'disk/memory'
161
+ }
162
+ })
163
+ return Promise.resolve(result)
164
+ },
165
+ }
@@ -0,0 +1,315 @@
1
+ /**
2
+ * Copyright (c) Nicolas Gallagher.
3
+ * Copyright (c) Meta Platforms, Inc. and affiliates.
4
+ *
5
+ * This source code is licensed under the MIT license found in the
6
+ * LICENSE file in the root directory of this source tree.
7
+ */
8
+
9
+ import { invariant } from './invariant'
10
+ import { requestIdleCallback } from './requestIdleCallback/index'
11
+
12
+ // Simple EventEmitter implementation for internal use
13
+ type EventMap = {
14
+ [K in string]: any[]
15
+ }
16
+
17
+ type EventSubscription = {
18
+ remove: () => void
19
+ }
20
+
21
+ class EventEmitter<TEventMap extends EventMap = EventMap> {
22
+ private _registry: {
23
+ [K in keyof TEventMap]?: Set<{
24
+ context: any
25
+ listener: (...args: TEventMap[K]) => void
26
+ remove: () => void
27
+ }>
28
+ } = {}
29
+
30
+ addListener<K extends keyof TEventMap>(
31
+ eventType: K,
32
+ listener: (...args: TEventMap[K]) => void,
33
+ context?: any
34
+ ): EventSubscription {
35
+ const registrations = this._allocate(eventType)
36
+ const registration = {
37
+ context,
38
+ listener,
39
+ remove: () => {
40
+ registrations.delete(registration)
41
+ },
42
+ }
43
+ registrations.add(registration)
44
+ return registration
45
+ }
46
+
47
+ emit<K extends keyof TEventMap>(eventType: K, ...args: TEventMap[K]): void {
48
+ const registrations = this._registry[eventType]
49
+ if (registrations != null) {
50
+ for (const registration of Array.from(registrations)) {
51
+ registration.listener.apply(registration.context, args)
52
+ }
53
+ }
54
+ }
55
+
56
+ private _allocate<K extends keyof TEventMap>(
57
+ eventType: K
58
+ ): Set<{
59
+ context: any
60
+ listener: (...args: TEventMap[K]) => void
61
+ remove: () => void
62
+ }> {
63
+ let registrations = this._registry[eventType]
64
+ if (registrations == null) {
65
+ registrations = new Set()
66
+ this._registry[eventType] = registrations
67
+ }
68
+ return registrations
69
+ }
70
+ }
71
+
72
+ // TaskQueue implementation
73
+ type SimpleTask = {
74
+ name: string
75
+ run: () => void
76
+ }
77
+
78
+ type PromiseTask = {
79
+ name: string
80
+ gen: () => Promise<void>
81
+ }
82
+
83
+ export type Task = SimpleTask | PromiseTask | (() => void)
84
+
85
+ interface QueueItem {
86
+ tasks: Task[]
87
+ popable: boolean
88
+ }
89
+
90
+ class TaskQueue {
91
+ private _queueStack: QueueItem[]
92
+ private _onMoreTasks: () => void
93
+
94
+ constructor({ onMoreTasks }: { onMoreTasks: () => void }) {
95
+ this._onMoreTasks = onMoreTasks
96
+ this._queueStack = [{ tasks: [], popable: true }]
97
+ }
98
+
99
+ enqueueTasks(tasks: Task[]): void {
100
+ tasks.forEach((task) => this._enqueue(task))
101
+ }
102
+
103
+ cancelTasks(tasksToCancel: Task[]): void {
104
+ this._queueStack = this._queueStack
105
+ .map((queue) => ({
106
+ ...queue,
107
+ tasks: queue.tasks.filter((task) => !tasksToCancel.includes(task)),
108
+ }))
109
+ .filter((queue, idx) => queue.tasks.length > 0 || idx === 0)
110
+ }
111
+
112
+ hasTasksToProcess(): boolean {
113
+ return this._getCurrentQueue().length > 0
114
+ }
115
+
116
+ processNext(): void {
117
+ const queue = this._getCurrentQueue()
118
+ if (queue.length) {
119
+ const task = queue.shift()
120
+ try {
121
+ if (typeof task === 'object' && task && 'gen' in task) {
122
+ this._genPromise(task as PromiseTask)
123
+ } else if (typeof task === 'object' && task && 'run' in task) {
124
+ ;(task as SimpleTask).run()
125
+ } else {
126
+ invariant(
127
+ typeof task === 'function',
128
+ 'Expected Function, SimpleTask, or PromiseTask, but got:\n' +
129
+ JSON.stringify(task, null, 2)
130
+ )
131
+ ;(task as () => void)()
132
+ }
133
+ } catch (e) {
134
+ if (e instanceof Error) {
135
+ const taskName =
136
+ task && typeof task === 'object' && 'name' in task ? task.name : ''
137
+ e.message = 'TaskQueue: Error with task ' + taskName + ': ' + e.message
138
+ }
139
+ throw e
140
+ }
141
+ }
142
+ }
143
+
144
+ private _enqueue(task: Task): void {
145
+ this._getCurrentQueue().push(task)
146
+ }
147
+
148
+ private _getCurrentQueue(): Task[] {
149
+ const stackIdx = this._queueStack.length - 1
150
+ const queue = this._queueStack[stackIdx]
151
+ if (queue.popable && queue.tasks.length === 0 && stackIdx > 0) {
152
+ this._queueStack.pop()
153
+ return this._getCurrentQueue()
154
+ } else {
155
+ return queue.tasks
156
+ }
157
+ }
158
+
159
+ private _genPromise(task: PromiseTask) {
160
+ const length = this._queueStack.push({ tasks: [], popable: false })
161
+ const stackIdx = length - 1
162
+ const stackItem = this._queueStack[stackIdx]
163
+ task
164
+ .gen()
165
+ .then(() => {
166
+ stackItem.popable = true
167
+ if (this.hasTasksToProcess()) {
168
+ this._onMoreTasks()
169
+ }
170
+ })
171
+ .catch((ex) => {
172
+ setTimeout(() => {
173
+ if (ex instanceof Error) {
174
+ ex.message = `TaskQueue: Error resolving Promise in task ${task.name}: ${ex.message}`
175
+ }
176
+ throw ex
177
+ }, 0)
178
+ })
179
+ }
180
+ }
181
+
182
+ const _emitter = new EventEmitter<{
183
+ interactionComplete: []
184
+ interactionStart: []
185
+ }>()
186
+
187
+ export const InteractionManager = {
188
+ Events: {
189
+ interactionStart: 'interactionStart' as const,
190
+ interactionComplete: 'interactionComplete' as const,
191
+ },
192
+
193
+ /**
194
+ * Schedule a function to run after all interactions have completed.
195
+ */
196
+ runAfterInteractions(task?: Task): {
197
+ then: <TResult1 = void, TResult2 = never>(
198
+ onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null,
199
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
200
+ ) => Promise<TResult1 | TResult2>
201
+ done: <TResult1 = void, TResult2 = never>(
202
+ onfulfilled?: ((value: void) => TResult1 | PromiseLike<TResult1>) | null,
203
+ onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
204
+ ) => Promise<TResult1 | TResult2>
205
+ cancel: () => void
206
+ } {
207
+ const tasks: Array<Task> = []
208
+ const promise = new Promise<void>((resolve) => {
209
+ _scheduleUpdate()
210
+ if (task) {
211
+ tasks.push(task)
212
+ }
213
+ tasks.push({
214
+ run: resolve,
215
+ name:
216
+ 'resolve ' +
217
+ ((task && typeof task === 'object' && 'name' in task && task.name) || '?'),
218
+ })
219
+ _taskQueue.enqueueTasks(tasks)
220
+ })
221
+ return {
222
+ then: promise.then.bind(promise),
223
+ done: promise.then.bind(promise),
224
+ cancel: () => {
225
+ _taskQueue.cancelTasks(tasks)
226
+ },
227
+ }
228
+ },
229
+
230
+ /**
231
+ * Notify manager that an interaction has started.
232
+ */
233
+ createInteractionHandle(): number {
234
+ _scheduleUpdate()
235
+ const handle = ++_inc
236
+ _addInteractionSet.add(handle)
237
+ return handle
238
+ },
239
+
240
+ /**
241
+ * Notify manager that an interaction has completed.
242
+ */
243
+ clearInteractionHandle(handle: number): void {
244
+ invariant(!!handle, 'Must provide a handle to clear.')
245
+ _scheduleUpdate()
246
+ _addInteractionSet.delete(handle)
247
+ _deleteInteractionSet.add(handle)
248
+ },
249
+
250
+ addListener: _emitter.addListener.bind(_emitter) as (
251
+ eventType: 'interactionStart' | 'interactionComplete',
252
+ listener: () => void,
253
+ context?: any
254
+ ) => EventSubscription,
255
+
256
+ /**
257
+ * Set deadline for task processing
258
+ */
259
+ setDeadline(deadline: number): void {
260
+ _deadline = deadline
261
+ },
262
+ }
263
+
264
+ const _interactionSet = new Set<number>()
265
+ const _addInteractionSet = new Set<number>()
266
+ const _deleteInteractionSet = new Set<number>()
267
+ const _taskQueue = new TaskQueue({ onMoreTasks: _scheduleUpdate })
268
+ let _nextUpdateHandle: number | ReturnType<typeof setTimeout> | null = null
269
+ let _inc = 0
270
+ let _deadline = -1
271
+
272
+ /**
273
+ * Schedule an asynchronous update to the interaction state.
274
+ */
275
+ function _scheduleUpdate(): void {
276
+ if (!_nextUpdateHandle) {
277
+ if (_deadline > 0) {
278
+ _nextUpdateHandle = setTimeout(_processUpdate)
279
+ } else {
280
+ _nextUpdateHandle = requestIdleCallback(_processUpdate)
281
+ }
282
+ }
283
+ }
284
+
285
+ /**
286
+ * Notify listeners, process queue, etc
287
+ */
288
+ function _processUpdate(): void {
289
+ _nextUpdateHandle = null
290
+ const interactionCount = _interactionSet.size
291
+ _addInteractionSet.forEach((handle) => _interactionSet.add(handle))
292
+ _deleteInteractionSet.forEach((handle) => _interactionSet.delete(handle))
293
+ const nextInteractionCount = _interactionSet.size
294
+
295
+ if (interactionCount !== 0 && nextInteractionCount === 0) {
296
+ _emitter.emit('interactionComplete')
297
+ } else if (interactionCount === 0 && nextInteractionCount !== 0) {
298
+ _emitter.emit('interactionStart')
299
+ }
300
+
301
+ if (nextInteractionCount === 0) {
302
+ // It seems that we can't know the running time of the current event loop,
303
+ // we can only calculate the running time of the current task queue.
304
+ const begin = Date.now()
305
+ while (_taskQueue.hasTasksToProcess()) {
306
+ _taskQueue.processNext()
307
+ if (_deadline > 0 && Date.now() - begin >= _deadline) {
308
+ _scheduleUpdate()
309
+ break
310
+ }
311
+ }
312
+ }
313
+ _addInteractionSet.clear()
314
+ _deleteInteractionSet.clear()
315
+ }