@lynx-js/react 0.126.2 → 0.128.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 (425) hide show
  1. package/CHANGELOG.md +36 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/docs/directives.md +90 -0
  4. package/docs/global-events.md +50 -0
  5. package/docs/import-attributes.md +43 -0
  6. package/docs/macros.md +285 -0
  7. package/package.json +7 -14
  8. package/refresh/.turbo/turbo-build.log +2 -2
  9. package/refresh/dist/index.js +26 -6
  10. package/refresh/package.json +1 -1
  11. package/runtime/__test__/core/main-thread-object.test.js +149 -0
  12. package/runtime/__test__/element-template/fixtures/background/main-thread-function/run-on-main-thread/index.tsx +26 -3
  13. package/runtime/__test__/element-template/imports/main-thread-object-api.test.ts +10 -0
  14. package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +4 -1
  15. package/runtime/__test__/element-template/runtime/background/event/compiled-fixtures.test.tsx +39 -48
  16. package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +11 -9
  17. package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +6 -3
  18. package/runtime/__test__/element-template/test-utils/mock/globals.js +6 -0
  19. package/runtime/__test__/element-template/vitest.config.ts +4 -0
  20. package/runtime/__test__/snapshot/ctxNotFound.test.ts +271 -0
  21. package/runtime/__test__/snapshot/event.test.jsx +7 -7
  22. package/runtime/__test__/snapshot/lazy.test.js +73 -1
  23. package/runtime/__test__/snapshot/lynx/portals.test.jsx +75 -37
  24. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +59 -47
  25. package/runtime/__test__/snapshot/spread.test.jsx +10 -10
  26. package/runtime/__test__/snapshot/worklet/main-thread-object.test.jsx +306 -0
  27. package/runtime/__test__/snapshot/worklet/workletRef.test.jsx +1 -0
  28. package/runtime/__test__/worklet-runtime/initEntry.test.js +109 -0
  29. package/runtime/__test__/worklet-runtime/mainThreadObject.test.js +182 -0
  30. package/runtime/__test__/worklet-runtime/object-methods.test.js +59 -0
  31. package/runtime/__test__/worklet-runtime/workletRef.test.js +362 -2
  32. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +246 -1
  33. package/runtime/lazy/compat.js +2 -0
  34. package/runtime/lazy/element-template-internal.js +1 -1
  35. package/runtime/lazy/element-template.js +2 -0
  36. package/runtime/lazy/internal.js +2 -1
  37. package/runtime/lazy/react.js +2 -0
  38. package/runtime/lib/core/background-function/exec-map.js.map +1 -1
  39. package/runtime/lib/core/background-function/handle.js.map +1 -1
  40. package/runtime/lib/core/background-function/run-on-background.d.ts +16 -0
  41. package/runtime/lib/core/background-function/run-on-background.js +16 -0
  42. package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
  43. package/runtime/lib/core/capture-main-thread-object.d.ts +1 -0
  44. package/runtime/lib/core/capture-main-thread-object.js +16 -0
  45. package/runtime/lib/core/capture-main-thread-object.js.map +1 -0
  46. package/runtime/lib/core/commit-context.js.map +1 -1
  47. package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
  48. package/runtime/lib/core/compat/lynxComponent.js.map +1 -1
  49. package/runtime/lib/core/component.js.map +1 -1
  50. package/runtime/lib/core/forceRootRender.js.map +1 -1
  51. package/runtime/lib/core/globalProps.d.ts +2 -0
  52. package/runtime/lib/core/globalProps.js.map +1 -1
  53. package/runtime/lib/core/hooks/mainThread.js.map +1 -1
  54. package/runtime/lib/core/hooks/mainThreadImpl.js.map +1 -1
  55. package/runtime/lib/core/hooks/react.d.ts +4 -0
  56. package/runtime/lib/core/hooks/react.js +4 -0
  57. package/runtime/lib/core/hooks/react.js.map +1 -1
  58. package/runtime/lib/core/hooks/useLynxGlobalEventListener.d.ts +2 -0
  59. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js +2 -0
  60. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js.map +1 -1
  61. package/runtime/lib/core/initData.d.ts +2 -0
  62. package/runtime/lib/core/initData.js +2 -0
  63. package/runtime/lib/core/initData.js.map +1 -1
  64. package/runtime/lib/core/lynx/dynamic-import.js.map +1 -1
  65. package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
  66. package/runtime/lib/core/lynx/runtime-backend.js.map +1 -1
  67. package/runtime/lib/core/lynx/runtime-version.js.map +1 -1
  68. package/runtime/lib/core/lynx-data-processors.js.map +1 -1
  69. package/runtime/lib/core/lynx-page-data.js.map +1 -1
  70. package/runtime/lib/core/lynx-update-data.js.map +1 -1
  71. package/runtime/lib/core/main-thread-function.js.map +1 -1
  72. package/runtime/lib/core/main-thread-object-handle-registry.d.ts +3 -0
  73. package/runtime/lib/core/main-thread-object-handle-registry.js +17 -0
  74. package/runtime/lib/core/main-thread-object-handle-registry.js.map +1 -0
  75. package/runtime/lib/core/main-thread-object.d.ts +77 -0
  76. package/runtime/lib/core/main-thread-object.js +258 -0
  77. package/runtime/lib/core/main-thread-object.js.map +1 -0
  78. package/runtime/lib/core/main-thread-ref-id.d.ts +1 -0
  79. package/runtime/lib/core/main-thread-ref-id.js +14 -0
  80. package/runtime/lib/core/main-thread-ref-id.js.map +1 -0
  81. package/runtime/lib/core/main-thread-ref-init-value.d.ts +1 -1
  82. package/runtime/lib/core/main-thread-ref-init-value.js +4 -2
  83. package/runtime/lib/core/main-thread-ref-init-value.js.map +1 -1
  84. package/runtime/lib/core/main-thread-ref.d.ts +4 -0
  85. package/runtime/lib/core/main-thread-ref.js +5 -8
  86. package/runtime/lib/core/main-thread-ref.js.map +1 -1
  87. package/runtime/lib/core/mts-capability.js.map +1 -1
  88. package/runtime/lib/core/performance.js.map +1 -1
  89. package/runtime/lib/core/ref.js.map +1 -1
  90. package/runtime/lib/core/reload-version.js.map +1 -1
  91. package/runtime/lib/core/runtime-destroy.js.map +1 -1
  92. package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -1
  93. package/runtime/lib/core/thread-function-call/return-value.js.map +1 -1
  94. package/runtime/lib/document.js.map +1 -1
  95. package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
  96. package/runtime/lib/element-template/background/commit-context.js.map +1 -1
  97. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  98. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  99. package/runtime/lib/element-template/background/document.js.map +1 -1
  100. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  101. package/runtime/lib/element-template/background/hydration-listener.js.map +1 -1
  102. package/runtime/lib/element-template/background/instance.js.map +1 -1
  103. package/runtime/lib/element-template/background/manager.js.map +1 -1
  104. package/runtime/lib/element-template/background/page-root-hydrate.js.map +1 -1
  105. package/runtime/lib/element-template/background/render-scope.js.map +1 -1
  106. package/runtime/lib/element-template/client/root.js.map +1 -1
  107. package/runtime/lib/element-template/debug/alog.js.map +1 -1
  108. package/runtime/lib/element-template/debug/elementPAPICall.js.map +1 -1
  109. package/runtime/lib/element-template/debug/profile.js.map +1 -1
  110. package/runtime/lib/element-template/hydration-map.js.map +1 -1
  111. package/runtime/lib/element-template/index.d.ts +2 -0
  112. package/runtime/lib/element-template/index.js +1 -0
  113. package/runtime/lib/element-template/index.js.map +1 -1
  114. package/runtime/lib/element-template/internal.d.ts +1 -1
  115. package/runtime/lib/element-template/internal.js +1 -1
  116. package/runtime/lib/element-template/internal.js.map +1 -1
  117. package/runtime/lib/element-template/jsx-dev-runtime/index.js.map +1 -1
  118. package/runtime/lib/element-template/jsx-runtime/index.js.map +1 -1
  119. package/runtime/lib/element-template/lynx/env.js.map +1 -1
  120. package/runtime/lib/element-template/lynx/performance.js.map +1 -1
  121. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
  122. package/runtime/lib/element-template/native/index.js.map +1 -1
  123. package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
  124. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  125. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  126. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  127. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  128. package/runtime/lib/element-template/prop-adapters/event-value.js.map +1 -1
  129. package/runtime/lib/element-template/prop-adapters/event.js.map +1 -1
  130. package/runtime/lib/element-template/prop-adapters/ref.js.map +1 -1
  131. package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
  132. package/runtime/lib/element-template/protocol/lifecycle-constant.js.map +1 -1
  133. package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
  134. package/runtime/lib/element-template/protocol/page.js.map +1 -1
  135. package/runtime/lib/element-template/protocol/template-type.js.map +1 -1
  136. package/runtime/lib/element-template/protocol/types.d.ts +1 -1
  137. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  138. package/runtime/lib/element-template/protocol/update-event.js.map +1 -1
  139. package/runtime/lib/element-template/runtime/list/list.js.map +1 -1
  140. package/runtime/lib/element-template/runtime/page/authored-page.js.map +1 -1
  141. package/runtime/lib/element-template/runtime/page/page.js.map +1 -1
  142. package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
  143. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  144. package/runtime/lib/element-template/runtime/render/create-rendered-host.js.map +1 -1
  145. package/runtime/lib/element-template/runtime/render/host.js.map +1 -1
  146. package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
  147. package/runtime/lib/element-template/runtime/render/render-direct.js.map +1 -1
  148. package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
  149. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
  150. package/runtime/lib/element-template/runtime/template/handle.js.map +1 -1
  151. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  152. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  153. package/runtime/lib/element-template/runtime/template/main-thread-event-ctx.js.map +1 -1
  154. package/runtime/lib/element-template/runtime/template/main-thread-function.js.map +1 -1
  155. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  156. package/runtime/lib/element-template/runtime/template/registry.js.map +1 -1
  157. package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
  158. package/runtime/lib/element-template/runtime-marker.js.map +1 -1
  159. package/runtime/lib/index.js.map +1 -1
  160. package/runtime/lib/internal.d.ts +1 -1
  161. package/runtime/lib/internal.js +1 -1
  162. package/runtime/lib/internal.js.map +1 -1
  163. package/runtime/lib/lynx-api.d.ts +34 -0
  164. package/runtime/lib/lynx-api.js +21 -0
  165. package/runtime/lib/lynx-api.js.map +1 -1
  166. package/runtime/lib/lynx.js +4 -1
  167. package/runtime/lib/lynx.js.map +1 -1
  168. package/runtime/lib/root.js.map +1 -1
  169. package/runtime/lib/runtime-marker.js.map +1 -1
  170. package/runtime/lib/shared/attribute-name.js.map +1 -1
  171. package/runtime/lib/shared/component-stack.js.map +1 -1
  172. package/runtime/lib/shared/index-map.js.map +1 -1
  173. package/runtime/lib/shared/profile.js.map +1 -1
  174. package/runtime/lib/shared/render-constants.js.map +1 -1
  175. package/runtime/lib/snapshot/alog/elementPAPICall.js.map +1 -1
  176. package/runtime/lib/snapshot/alog/index.js.map +1 -1
  177. package/runtime/lib/snapshot/alog/render.js.map +1 -1
  178. package/runtime/lib/snapshot/compat/componentIs.js.map +1 -1
  179. package/runtime/lib/snapshot/debug/debug.js.map +1 -1
  180. package/runtime/lib/snapshot/debug/describeInvalidValue.js.map +1 -1
  181. package/runtime/lib/snapshot/debug/formatPatch.js.map +1 -1
  182. package/runtime/lib/snapshot/debug/printSnapshot.js.map +1 -1
  183. package/runtime/lib/snapshot/debug/profileHooks.js.map +1 -1
  184. package/runtime/lib/snapshot/debug/vnodeSource.js.map +1 -1
  185. package/runtime/lib/snapshot/gesture/processGesture.js.map +1 -1
  186. package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +1 -1
  187. package/runtime/lib/snapshot/gesture/types.js.map +1 -1
  188. package/runtime/lib/snapshot/index.js.map +1 -1
  189. package/runtime/lib/snapshot/lifecycle/constant.js.map +1 -1
  190. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  191. package/runtime/lib/snapshot/lifecycle/event/delayEvents.js.map +1 -1
  192. package/runtime/lib/snapshot/lifecycle/event/delayLifecycleEvents.js.map +1 -1
  193. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.js.map +1 -1
  194. package/runtime/lib/snapshot/lifecycle/isRendering.js.map +1 -1
  195. package/runtime/lib/snapshot/lifecycle/patch/commit.js.map +1 -1
  196. package/runtime/lib/snapshot/lifecycle/patch/error.d.ts +11 -1
  197. package/runtime/lib/snapshot/lifecycle/patch/error.js +24 -16
  198. package/runtime/lib/snapshot/lifecycle/patch/error.js.map +1 -1
  199. package/runtime/lib/snapshot/lifecycle/patch/globalState.js.map +1 -1
  200. package/runtime/lib/snapshot/lifecycle/patch/isMainThreadHydrating.js.map +1 -1
  201. package/runtime/lib/snapshot/lifecycle/patch/nodesRefApply.js.map +1 -1
  202. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
  203. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.d.ts +1 -1
  204. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +29 -7
  205. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  206. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +2 -2
  207. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  208. package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
  209. package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
  210. package/runtime/lib/snapshot/lifecycle/render.js.map +1 -1
  211. package/runtime/lib/snapshot/list/list.js.map +1 -1
  212. package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
  213. package/runtime/lib/snapshot/list/pendingListUpdates.js.map +1 -1
  214. package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
  215. package/runtime/lib/snapshot/lynx/children.d.ts +2 -0
  216. package/runtime/lib/snapshot/lynx/children.js.map +1 -1
  217. package/runtime/lib/snapshot/lynx/element.d.ts +8 -0
  218. package/runtime/lib/snapshot/lynx/element.js +4 -0
  219. package/runtime/lib/snapshot/lynx/element.js.map +1 -1
  220. package/runtime/lib/snapshot/lynx/env.js.map +1 -1
  221. package/runtime/lib/snapshot/lynx/injectLepusMethods.js.map +1 -1
  222. package/runtime/lib/snapshot/lynx/lazyBundleConstants.js.map +1 -1
  223. package/runtime/lib/snapshot/lynx/nodesRef.js.map +1 -1
  224. package/runtime/lib/snapshot/lynx/performance.js.map +1 -1
  225. package/runtime/lib/snapshot/lynx/portals.js.map +1 -1
  226. package/runtime/lib/snapshot/lynx/portalsPending.js.map +1 -1
  227. package/runtime/lib/snapshot/lynx/prepareLazyBundleMTS.js.map +1 -1
  228. package/runtime/lib/snapshot/lynx/runWithForce.js.map +1 -1
  229. package/runtime/lib/snapshot/lynx/suspense.js.map +1 -1
  230. package/runtime/lib/snapshot/lynx/tt.js.map +1 -1
  231. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
  232. package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
  233. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
  234. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
  235. package/runtime/lib/snapshot/snapshot/constants.js.map +1 -1
  236. package/runtime/lib/snapshot/snapshot/definition.js.map +1 -1
  237. package/runtime/lib/snapshot/snapshot/dynamicPartType.js.map +1 -1
  238. package/runtime/lib/snapshot/snapshot/event.js.map +1 -1
  239. package/runtime/lib/snapshot/snapshot/gesture.js.map +1 -1
  240. package/runtime/lib/snapshot/snapshot/index.js.map +1 -1
  241. package/runtime/lib/snapshot/snapshot/list.js.map +1 -1
  242. package/runtime/lib/snapshot/snapshot/platformInfo.js.map +1 -1
  243. package/runtime/lib/snapshot/snapshot/reconstructInstanceTree.js.map +1 -1
  244. package/runtime/lib/snapshot/snapshot/ref.js.map +1 -1
  245. package/runtime/lib/snapshot/snapshot/snapshot.js.map +1 -1
  246. package/runtime/lib/snapshot/snapshot/snapshotCreatorMap.js.map +1 -1
  247. package/runtime/lib/snapshot/snapshot/snapshotInstanceHydrationMap.js.map +1 -1
  248. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.js.map +1 -1
  249. package/runtime/lib/snapshot/snapshot/spread.js.map +1 -1
  250. package/runtime/lib/snapshot/snapshot/types.js.map +1 -1
  251. package/runtime/lib/snapshot/snapshot/utils.js.map +1 -1
  252. package/runtime/lib/snapshot/snapshot/workletEvent.js.map +1 -1
  253. package/runtime/lib/snapshot/snapshot/workletRef.js.map +1 -1
  254. package/runtime/lib/snapshot/worklet/call/runOnMainThread.d.ts +2 -0
  255. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +2 -0
  256. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
  257. package/runtime/lib/snapshot/worklet/ctx.js.map +1 -1
  258. package/runtime/lib/snapshot/worklet/destroy.js.map +1 -1
  259. package/runtime/lib/snapshot/worklet/functionality.js.map +1 -1
  260. package/runtime/lib/snapshot/worklet/hmr.js.map +1 -1
  261. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js.map +1 -1
  262. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  263. package/runtime/lib/utils.js.map +1 -1
  264. package/runtime/lib/worklet-runtime/api/animation/animation.js.map +1 -1
  265. package/runtime/lib/worklet-runtime/api/animation/effect.js.map +1 -1
  266. package/runtime/lib/worklet-runtime/api/element.js.map +1 -1
  267. package/runtime/lib/worklet-runtime/api/lepusQuerySelector.js.map +1 -1
  268. package/runtime/lib/worklet-runtime/api/lynxApi.js.map +1 -1
  269. package/runtime/lib/worklet-runtime/bindings/bindings.d.ts +3 -3
  270. package/runtime/lib/worklet-runtime/bindings/bindings.js +9 -1
  271. package/runtime/lib/worklet-runtime/bindings/bindings.js.map +1 -1
  272. package/runtime/lib/worklet-runtime/bindings/events.js.map +1 -1
  273. package/runtime/lib/worklet-runtime/bindings/index.d.ts +0 -1
  274. package/runtime/lib/worklet-runtime/bindings/index.js +0 -1
  275. package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
  276. package/runtime/lib/worklet-runtime/bindings/observers.js.map +1 -1
  277. package/runtime/lib/worklet-runtime/bindings/types.d.ts +3 -6
  278. package/runtime/lib/worklet-runtime/bindings/types.js.map +1 -1
  279. package/runtime/lib/worklet-runtime/delayRunOnBackground.js.map +1 -1
  280. package/runtime/lib/worklet-runtime/delayWorkletEvent.js.map +1 -1
  281. package/runtime/lib/worklet-runtime/eomImpl.js.map +1 -1
  282. package/runtime/lib/worklet-runtime/eventPropagation.js.map +1 -1
  283. package/runtime/lib/worklet-runtime/global.js.map +1 -1
  284. package/runtime/lib/worklet-runtime/hydrate.js +26 -7
  285. package/runtime/lib/worklet-runtime/hydrate.js.map +1 -1
  286. package/runtime/lib/worklet-runtime/index.js +6 -0
  287. package/runtime/lib/worklet-runtime/index.js.map +1 -1
  288. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
  289. package/runtime/lib/worklet-runtime/listeners.js.map +1 -1
  290. package/runtime/lib/worklet-runtime/mainThreadObject.d.ts +8 -0
  291. package/runtime/lib/worklet-runtime/mainThreadObject.js +67 -0
  292. package/runtime/lib/worklet-runtime/mainThreadObject.js.map +1 -0
  293. package/runtime/lib/worklet-runtime/runOnMainThread.js.map +1 -1
  294. package/runtime/lib/worklet-runtime/utils/mainThreadFlushLoopGuard.js.map +1 -1
  295. package/runtime/lib/worklet-runtime/utils/profile.js.map +1 -1
  296. package/runtime/lib/worklet-runtime/utils/version.js.map +1 -1
  297. package/runtime/lib/worklet-runtime/workletRef.d.ts +11 -7
  298. package/runtime/lib/worklet-runtime/workletRef.js +72 -9
  299. package/runtime/lib/worklet-runtime/workletRef.js.map +1 -1
  300. package/runtime/lib/worklet-runtime/workletRuntime.js +68 -9
  301. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  302. package/runtime/package.json +2 -3
  303. package/runtime/src/core/background-function/run-on-background.ts +16 -0
  304. package/runtime/src/core/capture-main-thread-object.ts +17 -0
  305. package/runtime/src/core/globalProps.ts +2 -0
  306. package/runtime/src/core/hooks/react.ts +4 -0
  307. package/runtime/src/core/hooks/useLynxGlobalEventListener.ts +2 -0
  308. package/runtime/src/core/initData.ts +2 -0
  309. package/runtime/src/core/main-thread-object-handle-registry.ts +25 -0
  310. package/runtime/src/core/main-thread-object.ts +380 -0
  311. package/runtime/src/core/main-thread-ref-id.ts +16 -0
  312. package/runtime/src/core/main-thread-ref-init-value.ts +12 -4
  313. package/runtime/src/core/main-thread-ref.ts +7 -8
  314. package/runtime/src/element-template/background/commit-hook.ts +1 -1
  315. package/runtime/src/element-template/index.ts +6 -0
  316. package/runtime/src/element-template/internal.ts +2 -1
  317. package/runtime/src/element-template/protocol/types.ts +1 -1
  318. package/runtime/src/internal.ts +1 -1
  319. package/runtime/src/lynx-api.ts +38 -0
  320. package/runtime/src/lynx.ts +5 -1
  321. package/runtime/src/snapshot/lifecycle/patch/error.ts +36 -19
  322. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +29 -7
  323. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +2 -2
  324. package/runtime/src/snapshot/lynx/children.ts +2 -0
  325. package/runtime/src/snapshot/lynx/element.ts +8 -0
  326. package/runtime/src/snapshot/worklet/call/runOnMainThread.ts +2 -0
  327. package/runtime/src/snapshot/worklet/ref/updateInitValue.ts +1 -1
  328. package/runtime/src/worklet-runtime/bindings/bindings.ts +17 -2
  329. package/runtime/src/worklet-runtime/bindings/index.ts +0 -1
  330. package/runtime/src/worklet-runtime/bindings/types.ts +7 -11
  331. package/runtime/src/worklet-runtime/hydrate.ts +43 -10
  332. package/runtime/src/worklet-runtime/index.ts +7 -0
  333. package/runtime/src/worklet-runtime/mainThreadObject.ts +117 -0
  334. package/runtime/src/worklet-runtime/workletRef.ts +120 -19
  335. package/runtime/src/worklet-runtime/workletRuntime.ts +83 -11
  336. package/runtime/tsconfig.json +2 -0
  337. package/runtime/vitest.config.ts +4 -0
  338. package/testing-library/.turbo/turbo-build.log +11 -11
  339. package/testing-library/dist/env/index.js +3 -15
  340. package/testing-library/dist/index.d.ts +165 -2
  341. package/testing-library/dist/plugins/index.js +10 -0
  342. package/testing-library/dist/rstest-config.js +15 -0
  343. package/testing-library/dist/setupFiles/common/worklet-runtime-init.js +11 -0
  344. package/testing-library/package.json +2 -1
  345. package/testing-library/rstest.config.ts +3 -0
  346. package/testing-library/src/__tests__/lynx.test.jsx +1 -1
  347. package/testing-library/src/__tests__/rstest-config.test.js +34 -1
  348. package/testing-library/src/__tests__/worklet.test.jsx +87 -33
  349. package/testing-library/src/plugins/vitest.ts +5 -1
  350. package/testing-library/src/rstest-config.ts +17 -1
  351. package/testing-library/src/setupFiles/common/worklet-runtime-init.js +22 -0
  352. package/testing-library/tsconfig.docs.json +9 -0
  353. package/testing-library/typedoc.json +22 -0
  354. package/transform/.turbo/turbo-build$colon$wasm.log +135 -73
  355. package/transform/__test__/defines.spec.js +80 -12
  356. package/transform/__test__/fixture.spec.js +231 -71
  357. package/transform/__test__/object-methods.spec.js +82 -0
  358. package/transform/crates/swc_plugin_worklet/decl_collect.rs +10 -0
  359. package/transform/crates/swc_plugin_worklet/extract_ident.rs +14 -0
  360. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +228 -13
  361. package/transform/crates/swc_plugin_worklet/lib.rs +599 -150
  362. package/transform/crates/swc_plugin_worklet/napi.rs +10 -1
  363. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +8 -1
  364. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +12 -7
  365. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_js.js +27 -0
  366. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_lepus.js +43 -0
  367. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_ident_from_this_lepus.js +2 -4
  368. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +16 -14
  369. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +18 -18
  370. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_3_lepus.js +3 -5
  371. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_4_lepus.js +2 -4
  372. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +7 -5
  373. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +9 -9
  374. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_computed_object_method_key_lepus.js +22 -0
  375. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_globals.js +2 -4
  376. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_object_method_local_decls_lepus.js +14 -0
  377. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_type_annotations.js +2 -4
  378. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_destructure_from_closure.js +2 -4
  379. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_extract_catch_clause_params.js +2 -4
  380. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_extract_idents_inside_of_ctx.js +2 -4
  381. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_transform_recursively.js +2 -4
  382. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +42 -0
  383. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +58 -0
  384. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_skip_shared_identifiers_lepus.js +2 -4
  385. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_arrow_function.js +2 -4
  386. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_fn_decl_lepus.js +2 -4
  387. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_function_expr.js +2 -4
  388. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_lepus.js +2 -4
  389. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_property_lepus.js +2 -4
  390. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_static_lepus.js +2 -4
  391. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_in_class_static_property_lepus.js +2 -4
  392. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +3 -1
  393. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js_fn_in_run_on_js_lepus.js +2 -4
  394. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +5 -5
  395. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus_alias.js +2 -4
  396. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus_general.js +2 -4
  397. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus_general_mixed.js +2 -4
  398. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_multiple_worklets.js +3 -5
  399. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_multiple_worklets_in_func.js +3 -5
  400. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_js.js +9 -0
  401. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_lepus.js +14 -0
  402. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_ui_worklet_lepus.js +2 -4
  403. package/transform/dist/wasm.cjs +1 -1
  404. package/transform/index.d.ts +25 -1
  405. package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
  406. package/transform/src/lib.rs +193 -23
  407. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +63 -467
  408. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  409. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +340 -28
  410. package/transform/swc-plugin-reactlynx-compat/swc_plugin_reactlynx_compat.wasm +0 -0
  411. package/transform/vitest.config.ts +5 -1
  412. package/types/react.docs.d.ts +130 -11
  413. package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +0 -8
  414. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +0 -23
  415. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +0 -1
  416. package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +0 -25
  417. package/runtime/worklet-runtime/dev.js +0 -638
  418. package/runtime/worklet-runtime/main.js +0 -1
  419. package/testing-library/node_modules/.bin/rsbuild +0 -68
  420. package/testing-library/node_modules/.bin/rspeedy +0 -68
  421. package/testing-library/node_modules/.bin/rstest +0 -68
  422. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_add_worklet_runtime_ident_with_inner_ident.js +0 -13
  423. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_add_worklet_runtime_ident_with_outer_ident.js +0 -13
  424. package/transform/node_modules/.bin/esbuild +0 -59
  425. package/transform/node_modules/.bin/napi +0 -68
@@ -0,0 +1,380 @@
1
+ // Copyright 2026 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
+
5
+ import { WorkletEvents, registerMainThreadObjectType } from '@lynx-js/react/worklet-runtime/bindings';
6
+ import type { Worklet, WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bindings';
7
+
8
+ import { useMemo } from './hooks/react.js';
9
+ import { getMainThreadObjectHandleType, registerMainThreadObjectHandle } from './main-thread-object-handle-registry.js';
10
+ import { allocateMainThreadRefId } from './main-thread-ref-id.js';
11
+ import { addMainThreadRefInitValue } from './main-thread-ref-init-value.js';
12
+
13
+ const mainThreadObjectTypeDefinitions = new WeakMap<
14
+ object,
15
+ MainThreadObjectTypeDefinition<unknown, object>
16
+ >();
17
+
18
+ /**
19
+ * Describes how a serializable initialization payload becomes a stable object
20
+ * in the main-thread runtime.
21
+ *
22
+ * @public
23
+ */
24
+ export interface MainThreadObjectTypeDefinition<I, O extends object> {
25
+ /** A globally unique and stable type key. */
26
+ readonly type: string;
27
+ /**
28
+ * Create the realized main-thread object.
29
+ *
30
+ * This must be a capture-free Main Thread Function. Imports used by the
31
+ * function must come from shared-runtime modules.
32
+ */
33
+ readonly create: (initialValue: I) => O;
34
+ }
35
+
36
+ /**
37
+ * An immutable MainThreadObject type token with source-handle inspection.
38
+ *
39
+ * The factory function belongs to the input definition and is not exposed
40
+ * by the returned token. In the background runtime they compile to opaque
41
+ * Main Thread Function descriptors rather than bundling their implementation.
42
+ *
43
+ * @public
44
+ */
45
+ export interface MainThreadObjectType<I, O extends object> {
46
+ /** A globally unique and stable type key. */
47
+ readonly type: string;
48
+ /**
49
+ * Narrow a value to a source-runtime handle of this exact type.
50
+ *
51
+ * This does not create or access the realized main-thread object.
52
+ */
53
+ readonly downcast: (value: unknown) => MainThreadObjectHandle<I, O> | undefined;
54
+ }
55
+
56
+ /**
57
+ * An opaque source-runtime handle for an object realized on the main thread.
58
+ *
59
+ * A handle has logical identity and is serializable, but it is not the target
60
+ * object or an RPC stub. Capturing it in a main-thread function resolves it to
61
+ * the corresponding local target object. Development builds may wrap the
62
+ * background handle in a throwing guard solely to diagnose invalid access;
63
+ * that guard never forwards operations between runtimes.
64
+ *
65
+ * @public
66
+ */
67
+ export abstract class MainThreadObjectHandle<I, O extends object> {
68
+ /** @internal */
69
+ declare private readonly _mainThreadObjectTargetType: O;
70
+ /** @internal */
71
+ protected _wvid: number;
72
+ /** @internal */
73
+ protected _initValue: I;
74
+ /** @internal */
75
+ protected _type: string;
76
+ /** @internal */
77
+ protected _releaseObserver?: unknown;
78
+
79
+ /** @internal */
80
+ protected constructor(initialValue: I, type: string) {
81
+ if (__DEV__ && __JS__) {
82
+ assertSerializableMainThreadObjectPayload(initialValue, type);
83
+ }
84
+
85
+ this._wvid = allocateMainThreadRefId();
86
+ this._initValue = initialValue;
87
+ this._type = type;
88
+ registerMainThreadObjectHandle(this, type);
89
+
90
+ if (__JS__) {
91
+ addMainThreadRefInitValue(
92
+ this._wvid,
93
+ initialValue,
94
+ type,
95
+ );
96
+
97
+ const id = this._wvid;
98
+ this._releaseObserver = lynx.getNativeApp().createJSObjectDestructionObserver?.(() => {
99
+ lynx.getCoreContext?.().dispatchEvent({
100
+ type: WorkletEvents.releaseWorkletRef,
101
+ data: { id },
102
+ });
103
+ });
104
+ }
105
+ }
106
+
107
+ /**
108
+ * The payload used to create the realized object.
109
+ *
110
+ * Readonly is a TypeScript contract; the value is not frozen at runtime.
111
+ */
112
+ public get creationPayload(): Readonly<I> {
113
+ return this._initValue;
114
+ }
115
+
116
+ /** @internal */
117
+ toJSON(): Pick<WorkletRefImpl<unknown>, '_wvid' | '_initValue' | '_type'> {
118
+ return {
119
+ _wvid: this._wvid,
120
+ _initValue: this._initValue,
121
+ _type: this._type,
122
+ };
123
+ }
124
+ }
125
+
126
+ class MainThreadObjectHandleImpl<I, O extends object> extends MainThreadObjectHandle<I, O> {
127
+ constructor(initialValue: I, type: string) {
128
+ super(initialValue, type);
129
+ }
130
+ }
131
+
132
+ /**
133
+ * Define a main-thread object type for use by a library-provided hook.
134
+ *
135
+ * @param definition - Stable type key and target-object factory function.
136
+ * @returns An immutable object type definition.
137
+ * @public
138
+ */
139
+ export function defineMainThreadObjectType<I, O extends object>(
140
+ definition: MainThreadObjectTypeDefinition<I, O>,
141
+ ): MainThreadObjectType<I, O> {
142
+ // Read accessor-backed fields once so validation and deferred registration
143
+ // operate on the same definition values.
144
+ const { type, create } = definition;
145
+ if (typeof type !== 'string' || type.length === 0) {
146
+ throw new Error('MainThreadObject type must be a non-empty string.');
147
+ }
148
+ if (type === 'main-thread') {
149
+ throw new Error(
150
+ 'MainThreadObject type "main-thread" is reserved for MainThreadRef.',
151
+ );
152
+ }
153
+ if (!isMainThreadLifecycleFunction(create)) {
154
+ throw new Error(
155
+ `MainThreadObject type "${type}" must provide a create Main Thread Function.`,
156
+ );
157
+ }
158
+ if (__DEV__) {
159
+ assertCaptureFreeLifecycleFunction(type, 'create', create);
160
+ }
161
+
162
+ // Keep the validated fields independent from the caller-owned definition.
163
+ // Registration may be deferred until the first main-thread hook use, so a
164
+ // later mutation of `definition` must not change the type or factory that is
165
+ // registered for this token.
166
+ const validatedDefinition = Object.freeze({
167
+ type,
168
+ create,
169
+ });
170
+ const objectType = Object.freeze({
171
+ type,
172
+ downcast(value: unknown): MainThreadObjectHandle<I, O> | undefined {
173
+ return getMainThreadObjectHandleType(value) === type
174
+ ? value as MainThreadObjectHandle<I, O>
175
+ : undefined;
176
+ },
177
+ });
178
+ mainThreadObjectTypeDefinitions.set(
179
+ objectType,
180
+ validatedDefinition as unknown as MainThreadObjectTypeDefinition<unknown, object>,
181
+ );
182
+ registerMainThreadObjectDefinition(validatedDefinition);
183
+ return objectType;
184
+ }
185
+
186
+ /**
187
+ * Create a stable handle that resolves to an object of the supplied type in
188
+ * main-thread functions.
189
+ *
190
+ * This primitive is intended for library hooks. The returned target type is
191
+ * only callable in a main-thread function; it must not be used from the
192
+ * background runtime.
193
+ *
194
+ * @param objectType - Definition used to realize the target object.
195
+ * @param initialValue - JSON-serializable initialization payload.
196
+ * @returns The main-thread-only target type.
197
+ * @public
198
+ */
199
+ export function useMainThreadObject<I, O extends object>(
200
+ objectType: MainThreadObjectType<I, O>,
201
+ initialValue: I,
202
+ ): O {
203
+ return useMemo(() => {
204
+ const definition = mainThreadObjectTypeDefinitions.get(objectType);
205
+ if (definition === undefined) {
206
+ throw new Error(
207
+ `Invalid MainThreadObject type token for "${objectType.type}". Create it with defineMainThreadObjectType().`,
208
+ );
209
+ }
210
+ // A library module normally registers its type while the MTS bundle is
211
+ // evaluated. Register again at the first hook use so runtimes that retain
212
+ // the module but reset their per-page worklet registry remain correct.
213
+ // The registry treats an equivalent duplicate as an idempotent lookup.
214
+ registerMainThreadObjectDefinition(definition);
215
+ const handle = new MainThreadObjectHandleImpl<I, O>(initialValue, objectType.type);
216
+ return guardBackgroundMainThreadObjectAccess(handle, objectType.type) as unknown as O;
217
+ }, []);
218
+ }
219
+
220
+ function registerMainThreadObjectDefinition<I, O extends object>(
221
+ definition: MainThreadObjectTypeDefinition<I, O>,
222
+ ): void {
223
+ if (__JS__) {
224
+ return;
225
+ }
226
+
227
+ registerMainThreadObjectType(
228
+ definition.type,
229
+ definition.create as ((initialValue: unknown) => object) | Worklet,
230
+ );
231
+ }
232
+
233
+ function isMainThreadLifecycleFunction(
234
+ value: unknown,
235
+ ): value is ((...args: never[]) => unknown) | Worklet {
236
+ return typeof value === 'function'
237
+ || (typeof value === 'object' && value !== null
238
+ && typeof (value as Partial<Worklet>)._wkltId === 'string');
239
+ }
240
+
241
+ function assertCaptureFreeLifecycleFunction(
242
+ type: string,
243
+ name: 'create',
244
+ value: ((...args: never[]) => unknown) | Worklet,
245
+ ): void {
246
+ if (typeof value === 'function') {
247
+ return;
248
+ }
249
+ const descriptors = Object.getOwnPropertyDescriptors(value);
250
+ const hasCaptures = Reflect.ownKeys(descriptors).some(key => {
251
+ if (typeof key !== 'string') {
252
+ return true;
253
+ }
254
+ if (key === '_wkltId' || key === '_workletType' || key === '_lepusWorkletHash') {
255
+ return false;
256
+ }
257
+ if (key === '_c') {
258
+ const descriptor = descriptors[key]!;
259
+ const closure = 'value' in descriptor ? descriptor.value : undefined;
260
+ return !('value' in descriptor)
261
+ || (closure !== undefined
262
+ && (typeof closure !== 'object' || closure === null
263
+ || Reflect.ownKeys(closure).length !== 0));
264
+ }
265
+ return true;
266
+ });
267
+ if (hasCaptures) {
268
+ throw new Error(
269
+ `MainThreadObject ${name} function for "${type}" must not capture values. Import dependencies from a shared-runtime module instead.`,
270
+ );
271
+ }
272
+ }
273
+
274
+ function guardBackgroundMainThreadObjectAccess<I, O extends object>(
275
+ handle: MainThreadObjectHandle<I, O>,
276
+ type: string,
277
+ ): MainThreadObjectHandle<I, O> {
278
+ if (!__DEV__ || !__JS__) {
279
+ return handle;
280
+ }
281
+
282
+ // This development-only guard never forwards a call to the main thread. It
283
+ // only turns an otherwise opaque "method is not a function" failure into a
284
+ // diagnostic. Production handles remain plain serializable objects.
285
+ const guardedHandle = new Proxy(handle, {
286
+ get(target, property, receiver): unknown {
287
+ if (property in target) {
288
+ return Reflect.get(target, property, receiver) as unknown;
289
+ }
290
+ if (
291
+ typeof property === 'symbol'
292
+ || property === 'then'
293
+ || property === '$$typeof'
294
+ ) {
295
+ return undefined;
296
+ }
297
+ throw new Error(
298
+ `MainThreadObject handle for "${type}" cannot access "${
299
+ String(property)
300
+ }" in the background runtime. Use the object only inside a main-thread function.`,
301
+ );
302
+ },
303
+ set(target, property, value, receiver) {
304
+ if (property in target) {
305
+ return Reflect.set(target, property, value, receiver);
306
+ }
307
+ throw new Error(
308
+ `MainThreadObject handle for "${type}" cannot set "${
309
+ String(property)
310
+ }" in the background runtime. Use the object only inside a main-thread function.`,
311
+ );
312
+ },
313
+ });
314
+ registerMainThreadObjectHandle(guardedHandle, type);
315
+ return guardedHandle;
316
+ }
317
+
318
+ function assertSerializableMainThreadObjectPayload(value: unknown, type: string): void {
319
+ const invalidPath = findNonSerializablePath(value, '$', new Set<object>());
320
+ if (invalidPath !== undefined) {
321
+ throw new Error(
322
+ `MainThreadObject initial value for "${type}" must be JSON-serializable; invalid value at ${invalidPath}.`,
323
+ );
324
+ }
325
+ }
326
+
327
+ function findNonSerializablePath(
328
+ value: unknown,
329
+ path: string,
330
+ ancestors: Set<object>,
331
+ ): string | undefined {
332
+ if (
333
+ value === undefined
334
+ || typeof value === 'function'
335
+ || typeof value === 'symbol'
336
+ || typeof value === 'bigint'
337
+ || (typeof value === 'number' && !Number.isFinite(value))
338
+ ) {
339
+ return path;
340
+ }
341
+ if (value === null || typeof value !== 'object') {
342
+ return undefined;
343
+ }
344
+ if (ancestors.has(value)) {
345
+ return path;
346
+ }
347
+
348
+ const prototype = Object.getPrototypeOf(value) as object | null;
349
+ if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
350
+ return path;
351
+ }
352
+
353
+ ancestors.add(value);
354
+ if (Array.isArray(value)) {
355
+ for (let index = 0; index < value.length; index++) {
356
+ if (!Object.prototype.hasOwnProperty.call(value, index)) {
357
+ continue;
358
+ }
359
+ const invalidPath = findNonSerializablePath(
360
+ value[index],
361
+ `${path}.${index}`,
362
+ ancestors,
363
+ );
364
+ if (invalidPath !== undefined) {
365
+ ancestors.delete(value);
366
+ return invalidPath;
367
+ }
368
+ }
369
+ } else {
370
+ for (const [key, item] of Object.entries(value)) {
371
+ const invalidPath = findNonSerializablePath(item, `${path}.${key}`, ancestors);
372
+ if (invalidPath !== undefined) {
373
+ ancestors.delete(value);
374
+ return invalidPath;
375
+ }
376
+ }
377
+ }
378
+ ancestors.delete(value);
379
+ return undefined;
380
+ }
@@ -0,0 +1,16 @@
1
+ // Copyright 2026 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
+
5
+ let lastIdBG = 0;
6
+ let lastIdMT = 0;
7
+
8
+ /** @internal */
9
+ export function allocateMainThreadRefId(): number {
10
+ return __JS__ ? ++lastIdBG : --lastIdMT;
11
+ }
12
+
13
+ /** @internal */
14
+ export function clearMainThreadRefIdsForTesting(): void {
15
+ lastIdBG = lastIdMT = 0;
16
+ }
@@ -2,21 +2,29 @@
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
- import { isMtsEnabled } from './mts-capability.js';
5
+ import type { MainThreadRefInitValuePatch } from '@lynx-js/react/worklet-runtime/bindings';
6
6
 
7
- export type MainThreadRefInitValuePatch = [id: number, value: unknown][];
7
+ import { isMtsEnabled } from './mts-capability.js';
8
8
 
9
9
  let mainThreadRefInitValuePatch: MainThreadRefInitValuePatch = [];
10
10
 
11
11
  /**
12
12
  * @internal
13
13
  */
14
- export function addMainThreadRefInitValue(id: number, value: unknown): void {
14
+ export function addMainThreadRefInitValue(
15
+ id: number,
16
+ value: unknown,
17
+ type?: string,
18
+ ): void {
15
19
  if (!isMtsEnabled()) {
16
20
  return;
17
21
  }
18
22
 
19
- mainThreadRefInitValuePatch.push([id, value]);
23
+ mainThreadRefInitValuePatch.push(
24
+ type === undefined
25
+ ? [id, value]
26
+ : [id, value, type],
27
+ );
20
28
  }
21
29
 
22
30
  /**
@@ -4,16 +4,13 @@
4
4
  import type { RefObject } from 'react';
5
5
 
6
6
  import { useMemo } from './hooks/react.js';
7
+ import { allocateMainThreadRefId, clearMainThreadRefIdsForTesting } from './main-thread-ref-id.js';
7
8
  import { addMainThreadRefInitValue } from './main-thread-ref-init-value.js';
8
9
  import { WorkletEvents } from '../worklet-runtime/bindings/events.js';
9
10
  import type { WorkletRefImpl } from '../worklet-runtime/bindings/types.js';
10
11
 
11
- // Split into two variables for testing purposes.
12
- let lastIdBG = 0;
13
- let lastIdMT = 0;
14
-
15
12
  export function clearMainThreadRefLastIdForTesting(): void {
16
- lastIdBG = lastIdMT = 0;
13
+ clearMainThreadRefIdsForTesting();
17
14
  }
18
15
 
19
16
  export function isMainThreadRef(value: unknown): value is WorkletRefImpl<unknown> {
@@ -28,6 +25,8 @@ export function isMainThreadRef(value: unknown): value is WorkletRefImpl<unknown
28
25
  * The data saved in `current` property of the `MainThreadRef` can be read and written in
29
26
  * multiple main thread functions.
30
27
  * @public
28
+ *
29
+ * @group Types
31
30
  */
32
31
  export class MainThreadRef<T> {
33
32
  /**
@@ -50,8 +49,8 @@ export class MainThreadRef<T> {
50
49
  constructor(initValue: T) {
51
50
  this._initValue = initValue;
52
51
  this._type = 'main-thread';
52
+ this._wvid = allocateMainThreadRefId();
53
53
  if (__JS__) {
54
- this._wvid = ++lastIdBG;
55
54
  addMainThreadRefInitValue(this._wvid, initValue);
56
55
  const id = this._wvid;
57
56
  this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver?.(() => {
@@ -62,8 +61,6 @@ export class MainThreadRef<T> {
62
61
  },
63
62
  });
64
63
  });
65
- } else {
66
- this._wvid = --lastIdMT;
67
64
  }
68
65
  }
69
66
 
@@ -134,6 +131,8 @@ export class MainThreadRef<T> {
134
131
  * ```
135
132
  *
136
133
  * @public
134
+ *
135
+ * @group Hooks
137
136
  */
138
137
  export function useMainThreadRef<T>(initValue: T): MainThreadRef<T>;
139
138
 
@@ -11,7 +11,6 @@ import {
11
11
  } from './commit-context.js';
12
12
  import type { BackgroundElementTemplateInstance } from './instance.js';
13
13
  import { clearElementTemplateRenderScope, resetElementTemplateRenderScope } from './render-scope.js';
14
- import type { MainThreadRefInitValuePatch } from '../../core/main-thread-ref-init-value.js';
15
14
  import { takeMainThreadRefInitValuePatch } from '../../core/main-thread-ref-init-value.js';
16
15
  import { globalPipelineOptions, markTiming, markTimingLegacy, setPipeline } from '../../core/performance.js';
17
16
  import { getReloadVersion } from '../../core/reload-version.js';
@@ -22,6 +21,7 @@ import {
22
21
  import { dropFunctionCallReturnIds } from '../../core/thread-function-call/return-value.js';
23
22
  import { COMMIT } from '../../shared/render-constants.js';
24
23
  import { hook, isEmptyObject } from '../../utils.js';
24
+ import type { MainThreadRefInitValuePatch } from '../../worklet-runtime/bindings/types.js';
25
25
  import { formatElementTemplateUpdateCommands } from '../debug/alog.js';
26
26
  import { profileEnd, profileStart } from '../debug/profile.js';
27
27
  import { clearPendingRefs, flushPendingRefs, hasPendingRefs } from '../prop-adapters/ref.js';
@@ -133,6 +133,12 @@ export * from './client/root.js';
133
133
  export { runOnBackground } from './runtime/template/main-thread-background-function.js';
134
134
  export { runOnMainThread } from './runtime/template/main-thread-function.js';
135
135
  export { MainThreadRef, useMainThreadRef } from '../core/main-thread-ref.js';
136
+ export { defineMainThreadObjectType, useMainThreadObject } from '../core/main-thread-object.js';
136
137
 
137
138
  export type { GlobalProps } from '../core/globalProps.js';
139
+ export type {
140
+ MainThreadObjectHandle,
141
+ MainThreadObjectType,
142
+ MainThreadObjectTypeDefinition,
143
+ } from '../core/main-thread-object.js';
138
144
  export type { DataProcessorDefinition, DataProcessors, InitData, InitDataRaw } from '../lynx-api.js';
@@ -59,7 +59,8 @@ export { wrapWithLynxComponent } from '../core/compat/lynxComponent.js';
59
59
  export { loadLazyBundle } from '../core/lynx/lazy-bundle.js';
60
60
 
61
61
  export { transformToWorklet } from './runtime/template/main-thread-background-function.js';
62
- export { loadWorkletRuntime } from '@lynx-js/react/worklet-runtime/bindings';
62
+
63
+ export { captureMainThreadObject } from '../core/capture-main-thread-object.js';
63
64
 
64
65
  export function registerWorkletOnBackground(
65
66
  _type: string,
@@ -6,7 +6,7 @@ import type { RunWorkletCtxData } from '@lynx-js/react/worklet-runtime/bindings'
6
6
 
7
7
  import { ElementTemplateUpdateOps } from './opcodes.js';
8
8
  import { ELEMENT_TEMPLATE_PAGE_HANDLE_ID, ELEMENT_TEMPLATE_PAGE_TYPE } from './page.js';
9
- import type { MainThreadRefInitValuePatch } from '../../core/main-thread-ref-init-value.js';
9
+ import type { MainThreadRefInitValuePatch } from '../../worklet-runtime/bindings/types.js';
10
10
 
11
11
  export type SerializableValue =
12
12
  | string
@@ -100,7 +100,7 @@ export { loadLazyBundle } from './core/lynx/lazy-bundle.js';
100
100
  export { createBackgroundFunctionHandle as transformToWorklet } from './core/background-function/handle.js';
101
101
  export { registerWorkletOnBackground } from './snapshot/worklet/hmr.js';
102
102
 
103
- export { loadWorkletRuntime } from '@lynx-js/react/worklet-runtime/bindings';
103
+ export { captureMainThreadObject } from './core/capture-main-thread-object.js';
104
104
 
105
105
  if (__DEV__) {
106
106
  // Dev snapshot creators take the runtime as a parameter (they are
@@ -19,6 +19,8 @@ import { flushDelayedLifecycleEvents } from './snapshot/lynx/tt.js';
19
19
  /**
20
20
  * The default root exported by `@lynx-js/react` for you to render a JSX
21
21
  * @public
22
+ *
23
+ * @group Types
22
24
  */
23
25
  export interface Root {
24
26
  /**
@@ -83,6 +85,8 @@ export interface Root {
83
85
  * ```
84
86
  *
85
87
  * @public
88
+ *
89
+ * @group Functions
86
90
  */
87
91
  export const root: Root = {
88
92
  render: (jsx: ReactNode): void => {
@@ -134,6 +138,8 @@ export const root: Root = {
134
138
  * ```
135
139
  *
136
140
  * @public
141
+ *
142
+ * @group Functions
137
143
  */
138
144
  export function markFirstScreenSyncReady(): void {
139
145
  if (__FIRST_SCREEN_SYNC_TIMING__ !== 'manual') {
@@ -182,6 +188,8 @@ const _InitData = /* @__PURE__ */ factory<InitData>(
182
188
  * ```
183
189
  *
184
190
  * @public
191
+ *
192
+ * @function
185
193
  */
186
194
  // @ts-expect-error make preact and react types work
187
195
  export const InitDataProvider: FC<{ children?: ReactNode | undefined }> = /* @__PURE__ */ _InitData.Provider();
@@ -190,6 +198,8 @@ export const InitDataProvider: FC<{ children?: ReactNode | undefined }> = /* @__
190
198
  * This should be used with {@link InitDataProvider}
191
199
  * @group Components
192
200
  * @public
201
+ *
202
+ * @function
193
203
  */
194
204
  // @ts-expect-error make preact and react types work
195
205
  export const InitDataConsumer: Consumer<InitData> = /* @__PURE__ */ _InitData.Consumer();
@@ -209,6 +219,8 @@ export const InitDataConsumer: Consumer<InitData> = /* @__PURE__ */ _InitData.Co
209
219
  *
210
220
  * @function
211
221
  * @public
222
+ *
223
+ * @group Hooks
212
224
  */
213
225
  export const useInitData: () => InitData = /* @__PURE__ */ _InitData.use();
214
226
  /**
@@ -224,6 +236,8 @@ export const useInitData: () => InitData = /* @__PURE__ */ _InitData.use();
224
236
  * ```
225
237
  * @function
226
238
  * @public
239
+ *
240
+ * @group Hooks
227
241
  */
228
242
  export const useInitDataChanged: (callback: (data: InitData) => void) => void = /* @__PURE__ */ _InitData.useChanged();
229
243
 
@@ -264,6 +278,8 @@ const _GlobalProps = /* @__PURE__ */ createGlobalProps<GlobalProps>({
264
278
  * ```
265
279
  *
266
280
  * @public
281
+ *
282
+ * @function
267
283
  */
268
284
  // @ts-expect-error make preact and react types work
269
285
  export const GlobalPropsProvider: FC<{ children?: ReactNode | undefined }> = /* @__PURE__ */ _GlobalProps.Provider();
@@ -274,6 +290,8 @@ export const GlobalPropsProvider: FC<{ children?: ReactNode | undefined }> = /*
274
290
  * it calls `children` with `lynx.__globalProps` directly and updates come from a full re-render.
275
291
  * @group Components
276
292
  * @public
293
+ *
294
+ * @function
277
295
  */
278
296
  // @ts-expect-error make preact and react types work
279
297
  export const GlobalPropsConsumer: Consumer<GlobalProps> = /* @__PURE__ */ _GlobalProps.Consumer();
@@ -295,6 +313,8 @@ export const GlobalPropsConsumer: Consumer<GlobalProps> = /* @__PURE__ */ _Globa
295
313
  *
296
314
  * @function
297
315
  * @public
316
+ *
317
+ * @group Hooks
298
318
  */
299
319
  export const useGlobalProps: () => GlobalProps = /* @__PURE__ */ _GlobalProps.use();
300
320
 
@@ -312,6 +332,8 @@ export const useGlobalProps: () => GlobalProps = /* @__PURE__ */ _GlobalProps.us
312
332
  * ```
313
333
  * @function
314
334
  * @public
335
+ *
336
+ * @group Hooks
315
337
  */
316
338
  export const useGlobalPropsChanged: (callback: (data: GlobalProps) => void) => void = /* @__PURE__ */ _GlobalProps
317
339
  .useChanged();
@@ -322,6 +344,8 @@ export const useGlobalPropsChanged: (callback: (data: GlobalProps) => void) => v
322
344
  * Should be used with `lynx.registerDataProcessors`. See more examples at {@link Lynx.registerDataProcessors}.
323
345
  *
324
346
  * @public
347
+ *
348
+ * @group Types
325
349
  */
326
350
  export interface InitDataRaw {}
327
351
  /**
@@ -330,6 +354,8 @@ export interface InitDataRaw {}
330
354
  * Should be used with `lynx.registerDataProcessors`. See more examples at {@link Lynx.registerDataProcessors}.
331
355
  *
332
356
  * @public
357
+ *
358
+ * @group Types
333
359
  */
334
360
  export interface InitData {}
335
361
 
@@ -365,6 +391,8 @@ export { withInitDataInState };
365
391
  * ```
366
392
  *
367
393
  * @public
394
+ *
395
+ * @group Types
368
396
  */
369
397
  export interface DataProcessors {
370
398
  /**
@@ -414,6 +442,8 @@ export interface DataProcessors {
414
442
  /**
415
443
  * Definition of DataProcessor(s)
416
444
  * @public
445
+ *
446
+ * @group Types
417
447
  */
418
448
  export interface DataProcessorDefinition {
419
449
  /**
@@ -446,6 +476,8 @@ export interface DataProcessorDefinition {
446
476
  * ```
447
477
  *
448
478
  * @public
479
+ *
480
+ * @group Types
449
481
  */
450
482
  export interface Lynx {
451
483
  /**
@@ -570,3 +602,9 @@ export { useLynxGlobalEventListener } from './core/hooks/useLynxGlobalEventListe
570
602
  export { runOnBackground } from './core/background-function/run-on-background.js';
571
603
  export { runOnMainThread } from './snapshot/worklet/call/runOnMainThread.js';
572
604
  export { MainThreadRef, useMainThreadRef } from './core/main-thread-ref.js';
605
+ export { defineMainThreadObjectType, useMainThreadObject } from './core/main-thread-object.js';
606
+ export type {
607
+ MainThreadObjectHandle,
608
+ MainThreadObjectType,
609
+ MainThreadObjectTypeDefinition,
610
+ } from './core/main-thread-object.js';