@lynx-js/react 0.105.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 (337) hide show
  1. package/CHANGELOG.md +250 -0
  2. package/LICENSE +202 -0
  3. package/README.md +20 -0
  4. package/components/lib/Page.d.ts +3 -0
  5. package/components/lib/Page.js +56 -0
  6. package/components/lib/Page.js.map +1 -0
  7. package/components/lib/index.d.ts +1 -0
  8. package/components/lib/index.js +5 -0
  9. package/components/lib/index.js.map +1 -0
  10. package/components/src/Page.ts +75 -0
  11. package/components/src/index.ts +5 -0
  12. package/package.json +162 -0
  13. package/refresh/.gitignore +1 -0
  14. package/refresh/.npmignore +3 -0
  15. package/refresh/.turbo/turbo-build.log +8 -0
  16. package/refresh/dist/index.js +311 -0
  17. package/refresh/package.json +21 -0
  18. package/refresh/src/index.ts +6 -0
  19. package/refresh/src/prefresh.d.ts +11 -0
  20. package/refresh/tsconfig.json +4 -0
  21. package/refresh/turbo.json +15 -0
  22. package/runtime/jsx-dev-runtime/index.d.ts +5 -0
  23. package/runtime/jsx-dev-runtime/index.js +4 -0
  24. package/runtime/jsx-runtime/index.d.ts +4 -0
  25. package/runtime/jsx-runtime/index.js +4 -0
  26. package/runtime/lazy/import.d.ts +1 -0
  27. package/runtime/lazy/import.js +55 -0
  28. package/runtime/lazy/internal.js +39 -0
  29. package/runtime/lazy/jsx-dev-runtime.js +12 -0
  30. package/runtime/lazy/jsx-runtime.js +12 -0
  31. package/runtime/lazy/legacy-react-runtime.js +20 -0
  32. package/runtime/lazy/react-lepus.js +9 -0
  33. package/runtime/lazy/react.js +46 -0
  34. package/runtime/lazy/target.js +11 -0
  35. package/runtime/lepus/index.d.ts +4 -0
  36. package/runtime/lepus/index.js +43 -0
  37. package/runtime/lepus/jsx-dev-runtime/index.js +4 -0
  38. package/runtime/lepus/jsx-runtime/index.js +66 -0
  39. package/runtime/lib/backgroundSnapshot.d.ts +23 -0
  40. package/runtime/lib/backgroundSnapshot.js +348 -0
  41. package/runtime/lib/backgroundSnapshot.js.map +1 -0
  42. package/runtime/lib/compat/componentIs.d.ts +4 -0
  43. package/runtime/lib/compat/componentIs.js +20 -0
  44. package/runtime/lib/compat/componentIs.js.map +1 -0
  45. package/runtime/lib/compat/initData.d.ts +43 -0
  46. package/runtime/lib/compat/initData.js +93 -0
  47. package/runtime/lib/compat/initData.js.map +1 -0
  48. package/runtime/lib/compat/lynxComponent.d.ts +5 -0
  49. package/runtime/lib/compat/lynxComponent.js +71 -0
  50. package/runtime/lib/compat/lynxComponent.js.map +1 -0
  51. package/runtime/lib/debug/debug.d.ts +2 -0
  52. package/runtime/lib/debug/debug.js +9 -0
  53. package/runtime/lib/debug/debug.js.map +1 -0
  54. package/runtime/lib/debug/printSnapshot.d.ts +3 -0
  55. package/runtime/lib/debug/printSnapshot.js +21 -0
  56. package/runtime/lib/debug/printSnapshot.js.map +1 -0
  57. package/runtime/lib/debug/profile.d.ts +1 -0
  58. package/runtime/lib/debug/profile.js +51 -0
  59. package/runtime/lib/debug/profile.js.map +1 -0
  60. package/runtime/lib/document.d.ts +4 -0
  61. package/runtime/lib/document.js +49 -0
  62. package/runtime/lib/document.js.map +1 -0
  63. package/runtime/lib/gesture/processGesture.d.ts +4 -0
  64. package/runtime/lib/gesture/processGesture.js +48 -0
  65. package/runtime/lib/gesture/processGesture.js.map +1 -0
  66. package/runtime/lib/gesture/processGestureBagkround.d.ts +2 -0
  67. package/runtime/lib/gesture/processGestureBagkround.js +16 -0
  68. package/runtime/lib/gesture/processGestureBagkround.js.map +1 -0
  69. package/runtime/lib/gesture/types.d.ts +34 -0
  70. package/runtime/lib/gesture/types.js +13 -0
  71. package/runtime/lib/gesture/types.js.map +1 -0
  72. package/runtime/lib/hooks/react.d.ts +26 -0
  73. package/runtime/lib/hooks/react.js +35 -0
  74. package/runtime/lib/hooks/react.js.map +1 -0
  75. package/runtime/lib/hooks/useLynxGlobalEventListener.d.ts +29 -0
  76. package/runtime/lib/hooks/useLynxGlobalEventListener.js +53 -0
  77. package/runtime/lib/hooks/useLynxGlobalEventListener.js.map +1 -0
  78. package/runtime/lib/hydrate.d.ts +18 -0
  79. package/runtime/lib/hydrate.js +271 -0
  80. package/runtime/lib/hydrate.js.map +1 -0
  81. package/runtime/lib/index.d.ts +9 -0
  82. package/runtime/lib/index.js +46 -0
  83. package/runtime/lib/index.js.map +1 -0
  84. package/runtime/lib/internal.d.ts +26 -0
  85. package/runtime/lib/internal.js +41 -0
  86. package/runtime/lib/internal.js.map +1 -0
  87. package/runtime/lib/legacy-react-runtime/index.d.ts +7 -0
  88. package/runtime/lib/legacy-react-runtime/index.js +34 -0
  89. package/runtime/lib/legacy-react-runtime/index.js.map +1 -0
  90. package/runtime/lib/lifecycle/delayUnmount.d.ts +8 -0
  91. package/runtime/lib/lifecycle/delayUnmount.js +68 -0
  92. package/runtime/lib/lifecycle/delayUnmount.js.map +1 -0
  93. package/runtime/lib/lifecycle/destroy.d.ts +2 -0
  94. package/runtime/lib/lifecycle/destroy.js +21 -0
  95. package/runtime/lib/lifecycle/destroy.js.map +1 -0
  96. package/runtime/lib/lifecycle/pass.d.ts +3 -0
  97. package/runtime/lib/lifecycle/pass.js +12 -0
  98. package/runtime/lib/lifecycle/pass.js.map +1 -0
  99. package/runtime/lib/lifecycle/patchUpdate.d.ts +23 -0
  100. package/runtime/lib/lifecycle/patchUpdate.js +179 -0
  101. package/runtime/lib/lifecycle/patchUpdate.js.map +1 -0
  102. package/runtime/lib/lifecycle/reload.d.ts +3 -0
  103. package/runtime/lib/lifecycle/reload.js +63 -0
  104. package/runtime/lib/lifecycle/reload.js.map +1 -0
  105. package/runtime/lib/lifecycle/render.d.ts +2 -0
  106. package/runtime/lib/lifecycle/render.js +44 -0
  107. package/runtime/lib/lifecycle/render.js.map +1 -0
  108. package/runtime/lib/lifecycleConstant.d.ts +12 -0
  109. package/runtime/lib/lifecycleConstant.js +17 -0
  110. package/runtime/lib/lifecycleConstant.js.map +1 -0
  111. package/runtime/lib/list.d.ts +40 -0
  112. package/runtime/lib/list.js +305 -0
  113. package/runtime/lib/list.js.map +1 -0
  114. package/runtime/lib/lynx/calledByNative.d.ts +2 -0
  115. package/runtime/lib/lynx/calledByNative.js +113 -0
  116. package/runtime/lib/lynx/calledByNative.js.map +1 -0
  117. package/runtime/lib/lynx/component.d.ts +1 -0
  118. package/runtime/lib/lynx/component.js +69 -0
  119. package/runtime/lib/lynx/component.js.map +1 -0
  120. package/runtime/lib/lynx/dynamic-js.d.ts +2 -0
  121. package/runtime/lib/lynx/dynamic-js.js +31 -0
  122. package/runtime/lib/lynx/dynamic-js.js.map +1 -0
  123. package/runtime/lib/lynx/env.d.ts +1 -0
  124. package/runtime/lib/lynx/env.js +92 -0
  125. package/runtime/lib/lynx/env.js.map +1 -0
  126. package/runtime/lib/lynx/injectLepusMethods.d.ts +2 -0
  127. package/runtime/lib/lynx/injectLepusMethods.js +50 -0
  128. package/runtime/lib/lynx/injectLepusMethods.js.map +1 -0
  129. package/runtime/lib/lynx/lazy-bundle.d.ts +9 -0
  130. package/runtime/lib/lynx/lazy-bundle.js +119 -0
  131. package/runtime/lib/lynx/lazy-bundle.js.map +1 -0
  132. package/runtime/lib/lynx/performance.d.ts +29 -0
  133. package/runtime/lib/lynx/performance.js +100 -0
  134. package/runtime/lib/lynx/performance.js.map +1 -0
  135. package/runtime/lib/lynx/tt.d.ts +4 -0
  136. package/runtime/lib/lynx/tt.js +193 -0
  137. package/runtime/lib/lynx/tt.js.map +1 -0
  138. package/runtime/lib/lynx-api.d.ts +311 -0
  139. package/runtime/lib/lynx-api.js +116 -0
  140. package/runtime/lib/lynx-api.js.map +1 -0
  141. package/runtime/lib/lynx.d.ts +2 -0
  142. package/runtime/lib/lynx.js +91 -0
  143. package/runtime/lib/lynx.js.map +1 -0
  144. package/runtime/lib/opcodes.d.ts +2 -0
  145. package/runtime/lib/opcodes.js +59 -0
  146. package/runtime/lib/opcodes.js.map +1 -0
  147. package/runtime/lib/renderToOpcodes/constants.d.ts +17 -0
  148. package/runtime/lib/renderToOpcodes/constants.js +23 -0
  149. package/runtime/lib/renderToOpcodes/constants.js.map +1 -0
  150. package/runtime/lib/renderToOpcodes/index.d.ts +13 -0
  151. package/runtime/lib/renderToOpcodes/index.js +227 -0
  152. package/runtime/lib/renderToOpcodes/index.js.map +1 -0
  153. package/runtime/lib/root.d.ts +7 -0
  154. package/runtime/lib/root.js +17 -0
  155. package/runtime/lib/root.js.map +1 -0
  156. package/runtime/lib/snapshot/event.d.ts +3 -0
  157. package/runtime/lib/snapshot/event.js +24 -0
  158. package/runtime/lib/snapshot/event.js.map +1 -0
  159. package/runtime/lib/snapshot/gesture.d.ts +2 -0
  160. package/runtime/lib/snapshot/gesture.js +18 -0
  161. package/runtime/lib/snapshot/gesture.js.map +1 -0
  162. package/runtime/lib/snapshot/platformInfo.d.ts +4 -0
  163. package/runtime/lib/snapshot/platformInfo.js +35 -0
  164. package/runtime/lib/snapshot/platformInfo.js.map +1 -0
  165. package/runtime/lib/snapshot/ref.d.ts +10 -0
  166. package/runtime/lib/snapshot/ref.js +119 -0
  167. package/runtime/lib/snapshot/ref.js.map +1 -0
  168. package/runtime/lib/snapshot/spread.d.ts +5 -0
  169. package/runtime/lib/snapshot/spread.js +289 -0
  170. package/runtime/lib/snapshot/spread.js.map +1 -0
  171. package/runtime/lib/snapshot/workletEvent.d.ts +3 -0
  172. package/runtime/lib/snapshot/workletEvent.js +22 -0
  173. package/runtime/lib/snapshot/workletEvent.js.map +1 -0
  174. package/runtime/lib/snapshot/workletRef.d.ts +5 -0
  175. package/runtime/lib/snapshot/workletRef.js +52 -0
  176. package/runtime/lib/snapshot/workletRef.js.map +1 -0
  177. package/runtime/lib/snapshot.d.ts +81 -0
  178. package/runtime/lib/snapshot.js +477 -0
  179. package/runtime/lib/snapshot.js.map +1 -0
  180. package/runtime/lib/snapshotPatch.d.ts +14 -0
  181. package/runtime/lib/snapshotPatch.js +31 -0
  182. package/runtime/lib/snapshotPatch.js.map +1 -0
  183. package/runtime/lib/snapshotPatchApply.d.ts +2 -0
  184. package/runtime/lib/snapshotPatchApply.js +110 -0
  185. package/runtime/lib/snapshotPatchApply.js.map +1 -0
  186. package/runtime/lib/utils.d.ts +4 -0
  187. package/runtime/lib/utils.js +32 -0
  188. package/runtime/lib/utils.js.map +1 -0
  189. package/runtime/lib/worklet/ctx.d.ts +1 -0
  190. package/runtime/lib/worklet/ctx.js +17 -0
  191. package/runtime/lib/worklet/ctx.js.map +1 -0
  192. package/runtime/lib/worklet/execMap.d.ts +1 -0
  193. package/runtime/lib/worklet/execMap.js +42 -0
  194. package/runtime/lib/worklet/execMap.js.map +1 -0
  195. package/runtime/lib/worklet/functionality.d.ts +1 -0
  196. package/runtime/lib/worklet/functionality.js +11 -0
  197. package/runtime/lib/worklet/functionality.js.map +1 -0
  198. package/runtime/lib/worklet/hmr.d.ts +1 -0
  199. package/runtime/lib/worklet/hmr.js +21 -0
  200. package/runtime/lib/worklet/hmr.js.map +1 -0
  201. package/runtime/lib/worklet/indexMap.d.ts +8 -0
  202. package/runtime/lib/worklet/indexMap.js +17 -0
  203. package/runtime/lib/worklet/indexMap.js.map +1 -0
  204. package/runtime/lib/worklet/jsImpl.d.ts +11 -0
  205. package/runtime/lib/worklet/jsImpl.js +68 -0
  206. package/runtime/lib/worklet/jsImpl.js.map +1 -0
  207. package/runtime/lib/worklet/runWorklet.d.ts +14 -0
  208. package/runtime/lib/worklet/runWorklet.js +82 -0
  209. package/runtime/lib/worklet/runWorklet.js.map +1 -0
  210. package/runtime/lib/worklet/workletRef.d.ts +131 -0
  211. package/runtime/lib/worklet/workletRef.js +96 -0
  212. package/runtime/lib/worklet/workletRef.js.map +1 -0
  213. package/runtime/lib/worklet/workletRefPool.d.ts +2 -0
  214. package/runtime/lib/worklet/workletRefPool.js +22 -0
  215. package/runtime/lib/worklet/workletRefPool.js.map +1 -0
  216. package/runtime/src/backgroundSnapshot.ts +452 -0
  217. package/runtime/src/compat/componentIs.ts +30 -0
  218. package/runtime/src/compat/initData.ts +140 -0
  219. package/runtime/src/compat/lynxComponent.ts +80 -0
  220. package/runtime/src/debug/debug.ts +9 -0
  221. package/runtime/src/debug/printSnapshot.ts +28 -0
  222. package/runtime/src/debug/profile.ts +54 -0
  223. package/runtime/src/document.ts +53 -0
  224. package/runtime/src/gesture/processGesture.ts +77 -0
  225. package/runtime/src/gesture/processGestureBagkround.ts +15 -0
  226. package/runtime/src/gesture/types.ts +39 -0
  227. package/runtime/src/hooks/react.ts +62 -0
  228. package/runtime/src/hooks/useLynxGlobalEventListener.ts +59 -0
  229. package/runtime/src/hydrate.ts +364 -0
  230. package/runtime/src/index.ts +89 -0
  231. package/runtime/src/internal.ts +60 -0
  232. package/runtime/src/legacy-react-runtime/index.ts +40 -0
  233. package/runtime/src/lifecycle/delayUnmount.ts +77 -0
  234. package/runtime/src/lifecycle/destroy.ts +26 -0
  235. package/runtime/src/lifecycle/pass.ts +14 -0
  236. package/runtime/src/lifecycle/patchUpdate.ts +240 -0
  237. package/runtime/src/lifecycle/reload.ts +79 -0
  238. package/runtime/src/lifecycle/render.ts +46 -0
  239. package/runtime/src/lifecycleConstant.ts +16 -0
  240. package/runtime/src/list.ts +375 -0
  241. package/runtime/src/lynx/calledByNative.ts +132 -0
  242. package/runtime/src/lynx/component.ts +116 -0
  243. package/runtime/src/lynx/dynamic-js.ts +33 -0
  244. package/runtime/src/lynx/env.ts +110 -0
  245. package/runtime/src/lynx/injectLepusMethods.ts +53 -0
  246. package/runtime/src/lynx/lazy-bundle.ts +126 -0
  247. package/runtime/src/lynx/performance.ts +117 -0
  248. package/runtime/src/lynx/tt.ts +212 -0
  249. package/runtime/src/lynx-api.ts +374 -0
  250. package/runtime/src/lynx.ts +105 -0
  251. package/runtime/src/opcodes.ts +64 -0
  252. package/runtime/src/renderToOpcodes/constants.ts +24 -0
  253. package/runtime/src/renderToOpcodes/index.ts +301 -0
  254. package/runtime/src/root.ts +19 -0
  255. package/runtime/src/snapshot/event.ts +33 -0
  256. package/runtime/src/snapshot/gesture.ts +25 -0
  257. package/runtime/src/snapshot/platformInfo.ts +48 -0
  258. package/runtime/src/snapshot/ref.ts +151 -0
  259. package/runtime/src/snapshot/spread.ts +287 -0
  260. package/runtime/src/snapshot/workletEvent.ts +33 -0
  261. package/runtime/src/snapshot/workletRef.ts +63 -0
  262. package/runtime/src/snapshot.ts +576 -0
  263. package/runtime/src/snapshotPatch.ts +56 -0
  264. package/runtime/src/snapshotPatchApply.ts +117 -0
  265. package/runtime/src/utils.ts +35 -0
  266. package/runtime/src/worklet/ctx.ts +22 -0
  267. package/runtime/src/worklet/execMap.ts +50 -0
  268. package/runtime/src/worklet/functionality.ts +12 -0
  269. package/runtime/src/worklet/hmr.ts +26 -0
  270. package/runtime/src/worklet/indexMap.ts +20 -0
  271. package/runtime/src/worklet/jsImpl.ts +86 -0
  272. package/runtime/src/worklet/runWorklet.ts +88 -0
  273. package/runtime/src/worklet/workletRef.ts +229 -0
  274. package/runtime/src/worklet/workletRefPool.ts +25 -0
  275. package/transform/cjs/main.cjs +6 -0
  276. package/transform/cjs/main.d.cts +3 -0
  277. package/transform/cjs/package.json +3 -0
  278. package/transform/dist/wasm.cjs +8478 -0
  279. package/transform/index.d.ts +187 -0
  280. package/transform/main.js +15 -0
  281. package/types/react.d.ts +147 -0
  282. package/types/react.docs.d.ts +74 -0
  283. package/worklet-runtime/dist/dev.js +414 -0
  284. package/worklet-runtime/dist/dev.js.map +8 -0
  285. package/worklet-runtime/dist/main.js +414 -0
  286. package/worklet-runtime/dist/main.js.map +8 -0
  287. package/worklet-runtime/lib/api/element.d.ts +14 -0
  288. package/worklet-runtime/lib/api/element.js +71 -0
  289. package/worklet-runtime/lib/api/element.js.map +1 -0
  290. package/worklet-runtime/lib/api/lepusQuerySelector.d.ts +3 -0
  291. package/worklet-runtime/lib/api/lepusQuerySelector.js +23 -0
  292. package/worklet-runtime/lib/api/lepusQuerySelector.js.map +1 -0
  293. package/worklet-runtime/lib/api/lynxApi.d.ts +2 -0
  294. package/worklet-runtime/lib/api/lynxApi.js +35 -0
  295. package/worklet-runtime/lib/api/lynxApi.js.map +1 -0
  296. package/worklet-runtime/lib/bindings/bindings.d.ts +14 -0
  297. package/worklet-runtime/lib/bindings/bindings.js +61 -0
  298. package/worklet-runtime/lib/bindings/bindings.js.map +1 -0
  299. package/worklet-runtime/lib/bindings/events.d.ts +7 -0
  300. package/worklet-runtime/lib/bindings/events.js +9 -0
  301. package/worklet-runtime/lib/bindings/events.js.map +1 -0
  302. package/worklet-runtime/lib/bindings/index.d.ts +4 -0
  303. package/worklet-runtime/lib/bindings/index.js +7 -0
  304. package/worklet-runtime/lib/bindings/index.js.map +1 -0
  305. package/worklet-runtime/lib/bindings/loadRuntime.d.ts +8 -0
  306. package/worklet-runtime/lib/bindings/loadRuntime.js +25 -0
  307. package/worklet-runtime/lib/bindings/loadRuntime.js.map +1 -0
  308. package/worklet-runtime/lib/bindings/types.d.ts +35 -0
  309. package/worklet-runtime/lib/bindings/types.js +2 -0
  310. package/worklet-runtime/lib/bindings/types.js.map +1 -0
  311. package/worklet-runtime/lib/delayWorkletEvent.d.ts +11 -0
  312. package/worklet-runtime/lib/delayWorkletEvent.js +49 -0
  313. package/worklet-runtime/lib/delayWorkletEvent.js.map +1 -0
  314. package/worklet-runtime/lib/global.d.ts +15 -0
  315. package/worklet-runtime/lib/global.js +2 -0
  316. package/worklet-runtime/lib/global.js.map +1 -0
  317. package/worklet-runtime/lib/index.d.ts +1 -0
  318. package/worklet-runtime/lib/index.js +12 -0
  319. package/worklet-runtime/lib/index.js.map +1 -0
  320. package/worklet-runtime/lib/jsFunctionLifecycle.d.ts +18 -0
  321. package/worklet-runtime/lib/jsFunctionLifecycle.js +55 -0
  322. package/worklet-runtime/lib/jsFunctionLifecycle.js.map +1 -0
  323. package/worklet-runtime/lib/listeners.d.ts +2 -0
  324. package/worklet-runtime/lib/listeners.js +17 -0
  325. package/worklet-runtime/lib/listeners.js.map +1 -0
  326. package/worklet-runtime/lib/utils/profile.d.ts +1 -0
  327. package/worklet-runtime/lib/utils/profile.js +20 -0
  328. package/worklet-runtime/lib/utils/profile.js.map +1 -0
  329. package/worklet-runtime/lib/utils/version.d.ts +1 -0
  330. package/worklet-runtime/lib/utils/version.js +10 -0
  331. package/worklet-runtime/lib/utils/version.js.map +1 -0
  332. package/worklet-runtime/lib/workletRef.d.ts +12 -0
  333. package/worklet-runtime/lib/workletRef.js +50 -0
  334. package/worklet-runtime/lib/workletRef.js.map +1 -0
  335. package/worklet-runtime/lib/workletRuntime.d.ts +2 -0
  336. package/worklet-runtime/lib/workletRuntime.js +128 -0
  337. package/worklet-runtime/lib/workletRuntime.js.map +1 -0
@@ -0,0 +1,4 @@
1
+ export declare function isDirectOrDeepEqual(a: any, b: any): boolean;
2
+ export declare function isEmptyObject(obj?: object): obj is Record<string, never>;
3
+ export declare function isSdkVersionGt(major: number, minor: number): boolean;
4
+ export declare function pick<T extends object, K extends keyof T>(obj: T, keys: Iterable<K>): Pick<T, K>;
@@ -0,0 +1,32 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ export function isDirectOrDeepEqual(a, b) {
5
+ if (a === b) {
6
+ return true;
7
+ }
8
+ if (typeof a == 'object' && a !== null && typeof b == 'object' && b !== null && JSON.stringify(a) === JSON.stringify(b)) {
9
+ return true;
10
+ }
11
+ return false;
12
+ }
13
+ export function isEmptyObject(obj) {
14
+ for (var _ in obj)
15
+ return false;
16
+ return true;
17
+ }
18
+ export function isSdkVersionGt(major, minor) {
19
+ const lynxSdkVersion = SystemInfo.lynxSdkVersion || '1.0';
20
+ const version = lynxSdkVersion.split('.');
21
+ return Number(version[0]) > major || (Number(version[0]) == major && Number(version[1]) > minor);
22
+ }
23
+ export function pick(obj, keys) {
24
+ const result = {};
25
+ for (const key of keys) {
26
+ if (key in obj) {
27
+ result[key] = obj[key];
28
+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E,CAAC;QACvB,IAAI,GAAG,IAAI,GAAG,EAAE,CAAC;YACf,MAAM,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,GAAG,CAAC,CAAC;QACzB,CAAC;IACH,CAAC;IACD,OAAO,MAAM,CAAC;AAChB,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,17 @@
1
+ import { lynxWorkletJsImpl } from './jsImpl.js';
2
+ /**
3
+ * @internal
4
+ */
5
+ export function onPostWorkletCtx(afterValue) {
6
+ const impl = lynxWorkletJsImpl();
7
+ if (!impl && afterValue) {
8
+ lynx.reportError(new Error('Main thread script requires Lynx sdk version 2.14'));
9
+ return null;
10
+ }
11
+ if (!afterValue) {
12
+ return afterValue;
13
+ }
14
+ impl?._workletExecIdMap?.add(afterValue);
15
+ return afterValue;
16
+ }
17
+ //# sourceMappingURL=ctx.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"ctx.js","sourceRoot":"","sources":["../../src/worklet/ctx.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD;;GAEG;AACH,MAAM,UAAU,gBAAgB,CAAC,UAA0B;IACzD,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;IACjC,IAAI,CAAC,IAAI,IAAI,UAAU,EAAE,CAAC;QACxB,IAAI,CAAC,WAAW,CAAC,IAAI,KAAK,CAAC,mDAAmD,CAAC,CAAC,CAAC;QACjF,OAAO,IAAI,CAAC;IACd,CAAC;IACD,IAAI,CAAC,UAAU,EAAE,CAAC;QAChB,OAAO,UAAU,CAAC;IACpB,CAAC;IACD,IAAI,EAAE,iBAAiB,EAAE,GAAG,CAAC,UAAU,CAAC,CAAC;IACzC,OAAO,UAAU,CAAC;AACpB,CAAC"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,42 @@
1
+ import { IndexMap } from './indexMap.js';
2
+ /**
3
+ * `WorkletExecIdMap` ensures the worklet object is not released in js context when the main thread is still holding the
4
+ * worklet object (or any of its jsFnHandles). The worklet object holds the js functions which might be called by `runOnBackground()`.
5
+ * If the worklet object is incorrectly released earlier, the `runOnBackground()` would result in failure.
6
+ *
7
+ * Every time a worklet object is sent to the main thread, an `execId` is distributed and will be sent to element
8
+ * context with the worklet object. This relationship is recorded in this class. When all the references to the
9
+ * worklet object are released in the main thread, a message will be sent back to remove the record here.
10
+ *
11
+ * @internal
12
+ */
13
+ export class WorkletExecIdMap extends IndexMap {
14
+ add(worklet) {
15
+ const execId = super.add(worklet);
16
+ worklet._execId = execId;
17
+ return execId;
18
+ }
19
+ findJsFnHandle(execId, fnId) {
20
+ const worklet = this.get(execId);
21
+ if (!worklet) {
22
+ return undefined;
23
+ }
24
+ const f = (obj) => {
25
+ if (obj === null || typeof obj !== 'object') {
26
+ return undefined;
27
+ }
28
+ if ('_jsFnId' in obj && obj._jsFnId === fnId) {
29
+ return obj;
30
+ }
31
+ for (const i in obj) {
32
+ const result = f(obj[i]);
33
+ if (result) {
34
+ return result;
35
+ }
36
+ }
37
+ return undefined;
38
+ };
39
+ return f(worklet);
40
+ }
41
+ }
42
+ //# sourceMappingURL=execMap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"execMap.js","sourceRoot":"","sources":["../../src/worklet/execMap.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,QAAQ,EAAE,MAAM,eAAe,CAAC;AAEzC;;;;;;;;;;GAUG;AACH,MAAM,OAAO,gBAAiB,SAAQ,QAAiB;IACrC,GAAG,CAAC,OAAgB;QAClC,MAAM,MAAM,GAAG,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;QAClC,OAAO,CAAC,OAAO,GAAG,MAAM,CAAC;QACzB,OAAO,MAAM,CAAC;IAChB,CAAC;IAEM,cAAc,CAAC,MAAc,EAAE,IAAY;QAChD,MAAM,OAAO,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;QACjC,IAAI,CAAC,OAAO,EAAE,CAAC;YACb,OAAO,SAAS,CAAC;QACnB,CAAC;QAED,MAAM,CAAC,GAAG,CAAC,GAAQ,EAA0B,EAAE;YAC7C,IAAI,GAAG,KAAK,IAAI,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE,CAAC;gBAC5C,OAAO,SAAS,CAAC;YACnB,CAAC;YACD,IAAI,SAAS,IAAI,GAAG,IAAI,GAAG,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;gBAC7C,OAAO,GAAiB,CAAC;YAC3B,CAAC;YACD,KAAK,MAAM,CAAC,IAAI,GAAG,EAAE,CAAC;gBACpB,MAAM,MAAM,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC,CAAC;gBACzB,IAAI,MAAM,EAAE,CAAC;oBACX,OAAO,MAAM,CAAC;gBAChB,CAAC;YACH,CAAC;YACD,OAAO,SAAS,CAAC;QACnB,CAAC,CAAC;QAEF,OAAO,CAAC,CAAC,OAAO,CAAC,CAAC;IACpB,CAAC;CACF"}
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,11 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { isSdkVersionGt } from '../utils.js';
5
+ /**
6
+ * @internal
7
+ */
8
+ export function enableRunOnBackground() {
9
+ return isSdkVersionGt(2, 15);
10
+ }
11
+ //# sourceMappingURL=functionality.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"functionality.js","sourceRoot":"","sources":["../../src/worklet/functionality.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,cAAc,EAAE,MAAM,aAAa,CAAC;AAE7C;;GAEG;AACH,MAAM,UAAU,qBAAqB;IACnC,OAAO,cAAc,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC/B,CAAC"}
@@ -0,0 +1 @@
1
+ export { registerWorkletOnBackground };
@@ -0,0 +1,21 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { SnapshotOperation, __globalSnapshotPatch } from '../snapshotPatch.js';
5
+ const workletHashSet = /* @__PURE__ */ new Set();
6
+ /**
7
+ * @internal
8
+ */
9
+ function registerWorkletOnBackground(_type, hash, fn) {
10
+ if (workletHashSet.has(hash)) {
11
+ return;
12
+ }
13
+ workletHashSet.add(hash);
14
+ if (__globalSnapshotPatch) {
15
+ __globalSnapshotPatch.push(SnapshotOperation.DEV_ONLY_RegisterWorklet, hash,
16
+ // We use `Function.prototype.toString` to serialize the function for Lepus.
17
+ fn.toString());
18
+ }
19
+ }
20
+ export { registerWorkletOnBackground };
21
+ //# sourceMappingURL=hmr.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"hmr.js","sourceRoot":"","sources":["../../src/worklet/hmr.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,iBAAiB,EAAE,qBAAqB,EAAE,MAAM,qBAAqB,CAAC;AAE/E,MAAM,cAAc,GAAgB,eAAe,CAAC,IAAI,GAAG,EAAE,CAAC;AAE9D;;GAEG;AACH,SAAS,2BAA2B,CAAC,KAAa,EAAE,IAAY,EAAE,EAAY;IAC5E,IAAI,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC;QAC7B,OAAO;IACT,CAAC;IACD,cAAc,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;IACzB,IAAI,qBAAqB,EAAE,CAAC;QAC1B,qBAAqB,CAAC,IAAI,CACxB,iBAAiB,CAAC,wBAAwB,EAC1C,IAAI;QACJ,4EAA4E;QAC5E,EAAE,CAAC,QAAQ,EAAE,CACd,CAAC;IACJ,CAAC;AACH,CAAC;AAED,OAAO,EAAE,2BAA2B,EAAE,CAAC"}
@@ -0,0 +1,8 @@
1
+ declare class IndexMap<T> {
2
+ protected lastIndex: number;
3
+ protected indexMap: Map<number, T>;
4
+ add(value: T): number;
5
+ get(index: number): T | undefined;
6
+ remove(index: number): void;
7
+ }
8
+ export { IndexMap };
@@ -0,0 +1,17 @@
1
+ class IndexMap {
2
+ lastIndex = 0;
3
+ indexMap = new Map();
4
+ add(value) {
5
+ const index = ++this.lastIndex;
6
+ this.indexMap.set(index, value);
7
+ return index;
8
+ }
9
+ get(index) {
10
+ return this.indexMap.get(index);
11
+ }
12
+ remove(index) {
13
+ this.indexMap.delete(index);
14
+ }
15
+ }
16
+ export { IndexMap };
17
+ //# sourceMappingURL=indexMap.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"indexMap.js","sourceRoot":"","sources":["../../src/worklet/indexMap.ts"],"names":[],"mappings":"AAAA,MAAM,QAAQ;IACF,SAAS,GAAG,CAAC,CAAC;IACd,QAAQ,GAAmB,IAAI,GAAG,EAAE,CAAC;IAExC,GAAG,CAAC,KAAQ;QACjB,MAAM,KAAK,GAAG,EAAE,IAAI,CAAC,SAAS,CAAC;QAC/B,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QAChC,OAAO,KAAK,CAAC;IACf,CAAC;IAEM,GAAG,CAAC,KAAa;QACtB,OAAO,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;IAEM,MAAM,CAAC,KAAa;QACzB,IAAI,CAAC,QAAQ,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;IAC9B,CAAC;CACF;AAED,OAAO,EAAE,QAAQ,EAAE,CAAC"}
@@ -0,0 +1,11 @@
1
+ import { WorkletExecIdMap } from './execMap.js';
2
+ interface LynxWorkletJsImpl {
3
+ _workletExecIdMap?: WorkletExecIdMap;
4
+ _workletJsFnLastId: number;
5
+ _workletRefLastId: number;
6
+ _workletRefInitValueSet: Set<number>;
7
+ _workletRefInitValuePatch: [number, unknown][];
8
+ }
9
+ export declare function destroyWorklet(): void;
10
+ export declare function lynxWorkletJsImpl(shouldInit?: boolean): LynxWorkletJsImpl | undefined;
11
+ export {};
@@ -0,0 +1,68 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
5
+ import { WorkletExecIdMap } from './execMap.js';
6
+ import { enableRunOnBackground } from './functionality.js';
7
+ /**
8
+ * @internal
9
+ */
10
+ export function runJSFunction(event) {
11
+ const impl = lynxWorkletJsImpl();
12
+ if (!impl || !impl._workletExecIdMap) {
13
+ return;
14
+ }
15
+ const data = JSON.parse(event.data);
16
+ const obj = impl._workletExecIdMap.findJsFnHandle(data.obj._execId, data.obj._jsFnId);
17
+ const f = obj?._fn;
18
+ if (!f) {
19
+ throw new Error('runOnBackground: JS function not found: ' + JSON.stringify(data.obj));
20
+ }
21
+ f(...data.params);
22
+ }
23
+ /**
24
+ * @internal
25
+ */
26
+ export function removeJsWorklets(event) {
27
+ const impl = lynxWorkletJsImpl();
28
+ if (!impl?._workletExecIdMap) {
29
+ return;
30
+ }
31
+ for (const id of event.data) {
32
+ impl._workletExecIdMap.remove(id);
33
+ }
34
+ }
35
+ function initWorklet() {
36
+ if (lynx.getCoreContext === undefined) {
37
+ return false;
38
+ }
39
+ lynx.lynxWorkletJsImpl = {
40
+ _workletJsFnLastId: 0,
41
+ _workletRefLastId: 0,
42
+ _workletRefInitValueSet: new Set(),
43
+ _workletRefInitValuePatch: [],
44
+ };
45
+ if (enableRunOnBackground()) {
46
+ lynx.lynxWorkletJsImpl._workletExecIdMap = new WorkletExecIdMap();
47
+ lynx.getCoreContext().addEventListener(WorkletEvents.runOnBackground, runJSFunction);
48
+ lynx.getCoreContext().addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
49
+ }
50
+ return true;
51
+ }
52
+ export function destroyWorklet() {
53
+ if (!lynx.lynxWorkletJsImpl) {
54
+ return;
55
+ }
56
+ lynx.lynxWorkletJsImpl = undefined;
57
+ if (enableRunOnBackground()) {
58
+ lynx.getCoreContext?.().removeEventListener(WorkletEvents.runOnBackground, runJSFunction);
59
+ lynx.getCoreContext?.().removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
60
+ }
61
+ }
62
+ export function lynxWorkletJsImpl(shouldInit = true) {
63
+ if (lynx.lynxWorkletJsImpl || (shouldInit && initWorklet())) {
64
+ return lynx.lynxWorkletJsImpl;
65
+ }
66
+ return undefined;
67
+ }
68
+ //# sourceMappingURL=jsImpl.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"jsImpl.js","sourceRoot":"","sources":["../../src/worklet/jsImpl.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,OAAO,EAAE,gBAAgB,EAAE,MAAM,cAAc,CAAC;AAChD,OAAO,EAAE,qBAAqB,EAAE,MAAM,oBAAoB,CAAC;AAU3D;;GAEG;AACH,MAAM,UAAU,aAAa,CAAC,KAAyB;IACrD,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;IACjC,IAAI,CAAC,IAAI,IAAI,CAAC,IAAI,CAAC,iBAAiB,EAAE,CAAC;QACrC,OAAO;IACT,CAAC;IACD,MAAM,IAAI,GAAG,IAAI,CAAC,KAAK,CAAC,KAAK,CAAC,IAAI,CAAC,CAAC;IACpC,MAAM,GAAG,GAAG,IAAI,CAAC,iBAAiB,CAAC,cAAc,CAAC,IAAI,CAAC,GAAG,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IACtF,MAAM,CAAC,GAAG,GAAG,EAAE,GAAG,CAAC;IACnB,IAAI,CAAC,CAAC,EAAE,CAAC;QACP,MAAM,IAAI,KAAK,CAAC,0CAA0C,GAAG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qB,EAAE,EAAE,CAAC;QAC5B,IAAI,CAAC,cAAc,EAAE,EAAE,CAAC,mBAAmB,CAAC,aAAa,CAAC,eAAe,EAAE,aAAa,CAAC,CAAC;QAC1F,IAAI,CAAC,cAAc,EAAE,EAAE,CAAC,mBAAmB,CAAC,aAAa,CAAC,2BAA2B,EAAE,gBAAgB,CAAC,CAAC;IAC3G,CAAC;AACH,CAAC;AAED,MAAM,UAAU,iBAAiB,CAAC,aAAsB,IAAI;IAC1D,IAAI,IAAI,CAAC,iBAAiB,IAAI,CAAC,UAAU,IAAI,WAAW,EAAE,CAAC,EAAE,CAAC;QAC5D,OAAO,IAAI,CAAC,iBAAiB,CAAC;IAChC,CAAC;IACD,OAAO,SAAS,CAAC;AACnB,CAAC"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
3
+ * @param fn - The main thread functions to be called.
4
+ * @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread.
5
+ * @public
6
+ */
7
+ export declare function runOnMainThread<Fn extends (...args: any[]) => any>(fn: Fn): (...args: Parameters<Fn>) => void;
8
+ /**
9
+ * `runOnBackground` allows triggering js functions on the js context asynchronously.
10
+ * @param f - The js function to be called.
11
+ * @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the js context.
12
+ * @public
13
+ */
14
+ export declare function runOnBackground<Fn extends (...args: any[]) => any>(f: Fn): (...args: Parameters<Fn>) => void;
@@ -0,0 +1,82 @@
1
+ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
2
+ import { onPostWorkletCtx } from './ctx.js';
3
+ import { enableRunOnBackground } from './functionality.js';
4
+ import { lynxWorkletJsImpl } from './jsImpl.js';
5
+ /**
6
+ * transform args of `runOnJS()`.
7
+ *
8
+ * @internal
9
+ */
10
+ export function transformToWorklet(obj) {
11
+ const impl = lynxWorkletJsImpl();
12
+ const id = impl ? ++impl._workletJsFnLastId : 0;
13
+ if (typeof obj !== 'function') {
14
+ // We save the error message in the object, so that we can throw it later when the function is called on the main thread.
15
+ return {
16
+ _jsFnId: id,
17
+ _error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`,
18
+ };
19
+ }
20
+ return {
21
+ _jsFnId: id,
22
+ _fn: obj,
23
+ };
24
+ }
25
+ /**
26
+ * `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
27
+ * @param fn - The main thread functions to be called.
28
+ * @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread.
29
+ * @public
30
+ */
31
+ export function runOnMainThread(fn) {
32
+ if (__LEPUS__) {
33
+ throw new Error('runOnMainThread can only be used on the background thread.');
34
+ }
35
+ const impl = lynxWorkletJsImpl();
36
+ if (!impl) {
37
+ throw new Error('runOnMainThread requires Lynx sdk version 2.14.');
38
+ }
39
+ return (...params) => {
40
+ onPostWorkletCtx(fn);
41
+ lynx.getCoreContext().dispatchEvent({
42
+ type: WorkletEvents.runWorkletCtx,
43
+ data: JSON.stringify({
44
+ worklet: fn,
45
+ params,
46
+ }),
47
+ });
48
+ };
49
+ }
50
+ /**
51
+ * `runOnBackground` allows triggering js functions on the js context asynchronously.
52
+ * @param f - The js function to be called.
53
+ * @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the js context.
54
+ * @public
55
+ */
56
+ export function runOnBackground(f) {
57
+ if (!enableRunOnBackground()) {
58
+ throw new Error('runOnBackground requires Lynx sdk version 2.16.');
59
+ }
60
+ if (__JS__) {
61
+ throw new Error('runOnBackground can not be used on the main thread.');
62
+ }
63
+ const obj = f;
64
+ if (obj._error) {
65
+ throw new Error(obj._error);
66
+ }
67
+ return (...params) => {
68
+ if (lynx.getJSContext) {
69
+ lynx.getJSContext().dispatchEvent({
70
+ type: WorkletEvents.runOnBackground,
71
+ data: JSON.stringify({
72
+ obj: {
73
+ _jsFnId: obj._jsFnId,
74
+ _execId: obj._execId,
75
+ },
76
+ params,
77
+ }),
78
+ });
79
+ }
80
+ };
81
+ }
82
+ //# sourceMappingURL=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AAI,CAAC,SAAS,CAAC;gBACnB,OAAO,EAAE,EAAE;gBACX,MAAM;aACP,CAAC;SACH,CAAC,CAAC;IACL,CAAC,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,eAAe,CAAqC,CAAK;IACvE,IAAI,CAAC,qBAAqB,EAAE,EAAE,CAAC;QAC7B,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;IACrE,CAAC;IACD,IAAI,MAAM,EAAE,CAAC;QACX,MAAM,IAAI,KAAK,CAAC,qDAAqD,CAAC,CAAC;IACzE,CAAC;IACD,MAAM,GAAG,GAAG,CAAsB,CAAC;IACnC,IAAI,GAAG,CAAC,MAAM,EAAE,CAAC;QACf,MAAM,IAAI,KAAK,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC;IAC9B,CAAC;IACD,OAAO,CAAC,GAAG,MAAa,EAAQ,EAAE;QAChC,IAAI,IAAI,CAAC,YAAY,EAAE,CAAC;YACtB,IAAI,CAAC,YAAY,EAAE,CAAC,aAAa,CAAC;gBAChC,IAAI,EAAE,aAAa,CAAC,eAAe;gBACnC,IAAI,EAAE,IAAI,CAAC,SAAS,CAAC;oBACnB,GAAG,EAAE;wBACH,OAAO,EAAE,GAAG,CAAC,OAAO;wBACpB,OAAO,EAAE,GAAG,CAAC,OAAQ;qBACtB;oBACD,MAAM;iBACP,CAAC;aACH,CAAC,CAAC;QACL,CAAC;IACH,CAAC,CAAC;AACJ,CAAC"}
@@ -0,0 +1,131 @@
1
+ import type { RefObject } from 'react';
2
+ declare abstract class WorkletRef<T> {
3
+ get current(): T;
4
+ set current(_: T);
5
+ }
6
+ /**
7
+ * A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
8
+ * states between main thread function calls.
9
+ * The data saved in `current` property of the `MainThreadRef` can be read and written in
10
+ * multiple main thread functions.
11
+ * @public
12
+ */
13
+ export declare class MainThreadRef<T> extends WorkletRef<T> {
14
+ constructor(initValue: T);
15
+ }
16
+ /**
17
+ * Create A `MainThreadRef`.
18
+ *
19
+ * A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
20
+ * states between main thread function calls.
21
+ * The data saved in `current` property of the `MainThreadRef` can be read and written in
22
+ * multiple main thread functions.
23
+ *
24
+ * It is a hook and it should only be called at the top level of your component.
25
+ *
26
+ * @param initValue - The init value of the `MainThreadRef`.
27
+ *
28
+ * @example
29
+ *
30
+ * ```ts
31
+ * import { useMainThreadRef } from '@lynx-js/react'
32
+ * import type { MainThread } from '@lynx-js/types'
33
+ *
34
+ * export function App() {
35
+ * const ref = useMainThreadRef<MainThread.Element>(null)
36
+ *
37
+ * const handleTap = () => {
38
+ * 'main thread'
39
+ * ref.current?.setStyleProperty('background-color', 'blue')
40
+ * }
41
+ *
42
+ * return (
43
+ * <view
44
+ * main-thread:ref={ref}
45
+ * main-thread:bindtap={handleTap}
46
+ * style={{ width: '300px', height: '300px' }}
47
+ * />
48
+ * )
49
+ * }
50
+ * ```
51
+ *
52
+ * @public
53
+ */
54
+ export declare function useMainThreadRef<T>(initValue: T): MainThreadRef<T>;
55
+ /**
56
+ * Create A `MainThreadRef`.
57
+ *
58
+ * A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
59
+ * states between main thread function calls.
60
+ * The data saved in `current` property of the `MainThreadRef` can be read and written in
61
+ * multiple main thread functions.
62
+ *
63
+ * It is a hook and it should only be called at the top level of your component.
64
+ *
65
+ * @param initValue - The init value of the `MainThreadRef`.
66
+ *
67
+ * @example
68
+ *
69
+ * ```ts
70
+ * import { useMainThreadRef } from '@lynx-js/react'
71
+ * import type { MainThread } from '@lynx-js/types'
72
+ *
73
+ * export function App() {
74
+ * const ref = useMainThreadRef<MainThread.Element>(null)
75
+ *
76
+ * const handleTap = () => {
77
+ * 'main thread'
78
+ * ref.current?.setStyleProperty('background-color', 'blue')
79
+ * }
80
+ *
81
+ * return (
82
+ * <view
83
+ * main-thread:ref={ref}
84
+ * main-thread:bindtap={handleTap}
85
+ * style={{ width: '300px', height: '300px' }}
86
+ * />
87
+ * )
88
+ * }
89
+ * ```
90
+ *
91
+ * @public
92
+ */
93
+ export declare function useMainThreadRef<T>(initValue: T | null): RefObject<T>;
94
+ /**
95
+ * Create A `MainThreadRef`.
96
+ *
97
+ * A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
98
+ * states between main thread function calls.
99
+ * The data saved in `current` property of the `MainThreadRef` can be read and written in
100
+ * multiple main thread functions.
101
+ *
102
+ * It is a hook and it should only be called at the top level of your component.
103
+ *
104
+ * @example
105
+ *
106
+ * ```ts
107
+ * import { useMainThreadRef } from '@lynx-js/react'
108
+ * import type { MainThread } from '@lynx-js/types'
109
+ *
110
+ * export function App() {
111
+ * const ref = useMainThreadRef<MainThread.Element>(null)
112
+ *
113
+ * const handleTap = () => {
114
+ * 'main thread'
115
+ * ref.current?.setStyleProperty('background-color', 'blue')
116
+ * }
117
+ *
118
+ * return (
119
+ * <view
120
+ * main-thread:ref={ref}
121
+ * main-thread:bindtap={handleTap}
122
+ * style={{ width: '300px', height: '300px' }}
123
+ * />
124
+ * )
125
+ * }
126
+ * ```
127
+ *
128
+ * @public
129
+ */
130
+ export declare function useMainThreadRef<T = undefined>(): MainThreadRef<T | undefined>;
131
+ export {};
@@ -0,0 +1,96 @@
1
+ import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
2
+ import { lynxWorkletJsImpl } from './jsImpl.js';
3
+ import { addWorkletRefInitValue } from './workletRefPool.js';
4
+ import { useMemo } from '../hooks/react.js';
5
+ class WorkletRef {
6
+ /**
7
+ * @internal
8
+ */
9
+ _id;
10
+ /**
11
+ * @internal
12
+ */
13
+ _initValue;
14
+ /**
15
+ * @internal
16
+ */
17
+ _type;
18
+ /**
19
+ * @internal
20
+ */
21
+ _lifecycleObserver;
22
+ /**
23
+ * @internal
24
+ */
25
+ constructor(initValue, type) {
26
+ if (__JS__) {
27
+ const impl = lynxWorkletJsImpl();
28
+ this._id = impl ? ++impl._workletRefLastId : 0;
29
+ this._initValue = initValue;
30
+ this._type = type;
31
+ addWorkletRefInitValue(this._id, initValue);
32
+ }
33
+ else {
34
+ // Out of the js thread, the `WorkletRef` class here is just a placeholder and should not be accessed directly.
35
+ // The real WorkletRef will be generated by the worklet runtime.
36
+ this._id = -1;
37
+ this._type = '__LEPUS__';
38
+ }
39
+ }
40
+ get current() {
41
+ if (__JS__ && __DEV__) {
42
+ throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed in the background thread.');
43
+ }
44
+ if (__LEPUS__ && __DEV__) {
45
+ /* v8 ignore next */
46
+ throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed outside of main thread script.');
47
+ }
48
+ /* v8 ignore next */
49
+ return undefined;
50
+ }
51
+ set current(_) {
52
+ if (__JS__ && __DEV__) {
53
+ throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed in the background thread.');
54
+ }
55
+ if (__LEPUS__ && __DEV__) {
56
+ throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed outside of main thread script.');
57
+ }
58
+ }
59
+ /**
60
+ * @internal
61
+ */
62
+ toJSON() {
63
+ return {
64
+ _wvid: this._id,
65
+ };
66
+ }
67
+ }
68
+ /**
69
+ * A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
70
+ * states between main thread function calls.
71
+ * The data saved in `current` property of the `MainThreadRef` can be read and written in
72
+ * multiple main thread functions.
73
+ * @public
74
+ */
75
+ export class MainThreadRef extends WorkletRef {
76
+ constructor(initValue) {
77
+ super(initValue, 'main-thread');
78
+ if (__JS__) {
79
+ const id = this._id;
80
+ this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver(() => {
81
+ lynx.getCoreContext?.().dispatchEvent({
82
+ type: WorkletEvents.releaseWorkletRef,
83
+ data: {
84
+ id,
85
+ },
86
+ });
87
+ });
88
+ }
89
+ }
90
+ }
91
+ export function useMainThreadRef(initValue) {
92
+ return useMemo(() => {
93
+ return new MainThreadRef(initValue);
94
+ }, []);
95
+ }
96
+ //# sourceMappingURL=workletRef.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workletRef.js","sourceRoot":"","sources":["../../src/worklet/workletRef.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,aAAa,EAAE,MAAM,yCAAyC,CAAC;AAExE,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAChD,OAAO,EAAE,sBAAsB,EAAE,MAAM,qBAAqB,CAAC;AAC7D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAE5C,MAAe,UAAU;IACvB;;OAEG;IACO,GAAG,CAAS;IACtB;;OAEG;IACO,UAAU,CAAgB;IACpC;;OAEG;IACO,KAAK,CAAS;IACxB;;OAEG;IACO,kBAAkB,CAAU;IAEtC;;OAEG;IACH,YAAsB,SAAY,EAAE,IAAY;QAC9C,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;YACjC,IAAI,CAAC,GAAG,GAAG,IAAI,CAAC,CAAC,CAAC,EAAE,IAAI,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC,CAAC;YAC/C,IAAI,CAAC,UAAU,GAAG,SAAS,CAAC;YAC5B,IAAI,CAAC,KAAK,GAAG,IAAI,CAAC;YAClB,sBAAsB,CAAC,IAAI,CAAC,GAAG,EAAE,SAAS,CAAC,CAAC;QAC9C,CAAC;aAAM,CAAC;YACN,+GAA+G;YAC/G,gEAAgE;YAChE,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,CAAC;YACd,IAAI,CAAC,KAAK,GAAG,WAAW,CAAC;QAC3B,CAAC;IACH,CAAC;IAED,IAAI,OAAO;QACT,IAAI,MAAM,IAAI,OAAO,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,sFAAsF,CAAC,CAAC;QAC1G,CAAC;QACD,IAAI,SAAS,IAAI,OAAO,EAAE,CAAC;YACzB,oBAAoB;YACpB,MAAM,IAAI,KAAK,CAAC,2FAA2F,CAAC,CAAC;QAC/G,CAAC;QACD,oBAAoB;QACpB,OAAO,SAAc,CAAC;IACxB,CAAC;IAED,IAAI,OAAO,CAAC,CAAI;QACd,IAAI,MAAM,IAAI,OAAO,EAAE,CAAC;YACtB,MAAM,IAAI,KAAK,CAAC,sFAAsF,CAAC,CAAC;QAC1G,CAAC;QACD,IAAI,SAAS,IAAI,OAAO,EAAE,CAAC;YACzB,MAAM,IAAI,KAAK,CAAC,2FAA2F,CAAC,CAAC;QAC/G,CAAC;IACH,CAAC;IAED;;OAEG;IACH,MAAM;QACJ,OAAO;YACL,KAAK,EAAE,IAAI,CAAC,GAAG;SAChB,CAAC;IACJ,CAAC;CACF;AAED;;;;;;GAMG;AACH,MAAM,OAAO,aAAiB,SAAQ,UAAa;IACjD,YAAY,SAAY;QACtB,KAAK,CAAC,SAAS,EAAE,aAAa,CAAC,CAAC;QAChC,IAAI,MAAM,EAAE,CAAC;YACX,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,CAAC;YACpB,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC,iCAAiC,CAAC,GAAG,EAAE;gBACnF,IAAI,CAAC,cAAc,EAAE,EAAE,CAAC,aAAa,CAAC;oBACpC,IAAI,EAAE,aAAa,CAAC,iBAAiB;oBACrC,IAAI,EAAE;wBACJ,EAAE;qBACH;iBACF,CAAC,CAAC;YACL,CAAC,CAAC,CAAC;QACL,CAAC;IACH,CAAC;CACF;AA2HD,MAAM,UAAU,gBAAgB,CAAI,SAAa;IAC/C,OAAO,OAAO,CAAC,GAAG,EAAE;QAClB,OAAO,IAAI,aAAa,CAAC,SAAS,CAAC,CAAC;IACtC,CAAC,EAAE,EAAE,CAAC,CAAC;AACT,CAAC"}
@@ -0,0 +1,2 @@
1
+ export declare function addWorkletRefInitValue(id: number, value: unknown): void;
2
+ export declare function takeWorkletRefInitValuePatch(): [number, unknown][];
@@ -0,0 +1,22 @@
1
+ // Copyright 2024 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { lynxWorkletJsImpl } from './jsImpl.js';
5
+ export function addWorkletRefInitValue(id, value) {
6
+ const impl = lynxWorkletJsImpl();
7
+ if (!impl) {
8
+ return;
9
+ }
10
+ impl._workletRefInitValueSet.add(id);
11
+ impl._workletRefInitValuePatch.push([id, value]);
12
+ }
13
+ export function takeWorkletRefInitValuePatch() {
14
+ const impl = lynxWorkletJsImpl(false);
15
+ if (!impl) {
16
+ return [];
17
+ }
18
+ const res = impl._workletRefInitValuePatch;
19
+ impl._workletRefInitValuePatch = [];
20
+ return res;
21
+ }
22
+ //# sourceMappingURL=workletRefPool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"workletRefPool.js","sourceRoot":"","sources":["../../src/worklet/workletRefPool.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,aAAa,CAAC;AAEhD,MAAM,UAAU,sBAAsB,CAAC,EAAU,EAAE,KAAc;IAC/D,MAAM,IAAI,GAAG,iBAAiB,EAAE,CAAC;IACjC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO;IACT,CAAC;IAED,IAAI,CAAC,uBAAuB,CAAC,GAAG,CAAC,EAAE,CAAC,CAAC;IACrC,IAAI,CAAC,yBAAyB,CAAC,IAAI,CAAC,CAAC,EAAE,EAAE,KAAK,CAAC,CAAC,CAAC;AACnD,CAAC;AAED,MAAM,UAAU,4BAA4B;IAC1C,MAAM,IAAI,GAAG,iBAAiB,CAAC,KAAK,CAAC,CAAC;IACtC,IAAI,CAAC,IAAI,EAAE,CAAC;QACV,OAAO,EAAE,CAAC;IACZ,CAAC;IAED,MAAM,GAAG,GAAG,IAAI,CAAC,yBAAyB,CAAC;IAC3C,IAAI,CAAC,yBAAyB,GAAG,EAAE,CAAC;IACpC,OAAO,GAAG,CAAC;AACb,CAAC"}