@lynx-js/react 0.128.0 → 0.129.1

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 (334) hide show
  1. package/CHANGELOG.md +34 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +3 -4
  4. package/refresh/.turbo/turbo-build.log +1 -1
  5. package/runtime/__test__/core/entry-re-eval.test.ts +161 -0
  6. package/runtime/__test__/core/hooks/useLynxGlobalEventListener.test.jsx +1 -1
  7. package/runtime/__test__/core/reload-scoped-dual-thread-communication.test.ts +129 -0
  8. package/runtime/__test__/core/thread-function-call.test.ts +1 -0
  9. package/runtime/__test__/element-template/fixtures/background/render/_shared.tsx +2 -0
  10. package/runtime/__test__/element-template/imports/no-legacy-imports.test.ts +7 -6
  11. package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +1 -2
  12. package/runtime/__test__/element-template/native/index.test.ts +8 -0
  13. package/runtime/__test__/element-template/native/main-thread-api.test.ts +5 -2
  14. package/runtime/__test__/element-template/native/main-thread-ref-init-value.test.ts +100 -0
  15. package/runtime/__test__/element-template/native/mts-destroy.test.ts +83 -0
  16. package/runtime/__test__/element-template/native/reload-dual-thread-communication.test.ts +104 -0
  17. package/runtime/__test__/element-template/native/reload.test.ts +79 -1
  18. package/runtime/__test__/element-template/runtime/background/commit-hook.test.ts +104 -47
  19. package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +7 -1
  20. package/runtime/__test__/element-template/runtime/background/re-eval-reload.test.ts +69 -0
  21. package/runtime/__test__/element-template/runtime/background/update-timing.test.ts +15 -0
  22. package/runtime/__test__/element-template/runtime/hydration/hydration-listener.test.ts +31 -5
  23. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +214 -10
  24. package/runtime/__test__/element-template/runtime/patch/update-timing.test.ts +4 -1
  25. package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +4 -1
  26. package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +4 -1
  27. package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +101 -1
  28. package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +2 -0
  29. package/runtime/__test__/element-template/runtime/template/element-template-handle.test.ts +32 -0
  30. package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +121 -26
  31. package/runtime/__test__/element-template/runtime/template/main-thread-function.test.ts +89 -2
  32. package/runtime/__test__/element-template/test-utils/debug/compiledFixtureModule.ts +5 -0
  33. package/runtime/__test__/element-template/test-utils/debug/envManager.ts +8 -5
  34. package/runtime/__test__/element-template/test-utils/mock/mockNativePapi.ts +35 -2
  35. package/runtime/__test__/guardrails/snapshot-containment.test.ts +45 -11
  36. package/runtime/__test__/shared/component-stack.test.ts +211 -0
  37. package/runtime/__test__/snapshot/debug/formatPatch.test.ts +0 -8
  38. package/runtime/__test__/snapshot/gesture/prepareGestureForCommit.test.js +1 -1
  39. package/runtime/__test__/snapshot/lifecycle/re-eval-reload.test.jsx +114 -0
  40. package/runtime/__test__/snapshot/lifecycle/reload-background-function.test.jsx +169 -0
  41. package/runtime/__test__/snapshot/lifecycle/reload-delayed-background.test.jsx +63 -0
  42. package/runtime/__test__/snapshot/lifecycle/reload-delayed-main-thread.test.jsx +103 -0
  43. package/runtime/__test__/snapshot/lifecycle.test.jsx +61 -1
  44. package/runtime/__test__/snapshot/lynx/lazy-effect-scheduling.test.jsx +105 -0
  45. package/runtime/__test__/snapshot/lynx/timing.test.jsx +3 -0
  46. package/runtime/__test__/snapshot/preact.test.jsx +0 -3
  47. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +0 -33
  48. package/runtime/__test__/snapshot/ssr.test.jsx +2 -2
  49. package/runtime/__test__/snapshot/worklet/runOnBackground.test.js +6 -11
  50. package/runtime/__test__/snapshot/worklet/runOnMainThread.test.jsx +1 -1
  51. package/runtime/__test__/snapshot/worklet/workletRef.test.jsx +24 -1
  52. package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +27 -1
  53. package/runtime/__test__/worklet-runtime/loadRuntime.test.js +68 -0
  54. package/runtime/__test__/worklet-runtime/mainThreadObject.test.js +46 -0
  55. package/runtime/__test__/worklet-runtime/runOnMainThread.test.js +3 -3
  56. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +30 -0
  57. package/runtime/lazy/element-template-internal.js +0 -1
  58. package/runtime/lazy/internal.js +1 -1
  59. package/runtime/lazy/jsx-dev-runtime.js +1 -1
  60. package/runtime/lazy/jsx-runtime.js +1 -1
  61. package/runtime/lazy/legacy-react-runtime.js +1 -1
  62. package/runtime/lazy/react-lepus.js +1 -1
  63. package/runtime/lazy/react.js +1 -1
  64. package/runtime/lib/core/background-function/exec-map.d.ts +2 -0
  65. package/runtime/lib/core/background-function/exec-map.js +7 -0
  66. package/runtime/lib/core/background-function/exec-map.js.map +1 -1
  67. package/runtime/lib/core/background-function/run-on-background.js +15 -16
  68. package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
  69. package/runtime/lib/core/capture-main-thread-object.js +2 -1
  70. package/runtime/lib/core/capture-main-thread-object.js.map +1 -1
  71. package/runtime/lib/core/card-lifetime.d.ts +5 -0
  72. package/runtime/lib/core/card-lifetime.js +17 -0
  73. package/runtime/lib/core/card-lifetime.js.map +1 -0
  74. package/runtime/lib/core/compat/legacy-react-runtime.js +1 -1
  75. package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
  76. package/runtime/lib/core/entry-reload.d.ts +7 -0
  77. package/runtime/lib/core/entry-reload.js +49 -0
  78. package/runtime/lib/core/entry-reload.js.map +1 -0
  79. package/runtime/lib/core/lynx/lazy-bundle.js +5 -2
  80. package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
  81. package/runtime/lib/core/lynx-data-processors.d.ts +125 -1
  82. package/runtime/lib/core/lynx-data-processors.js +3 -0
  83. package/runtime/lib/core/lynx-data-processors.js.map +1 -1
  84. package/runtime/lib/core/main-thread-ref-id.js +4 -4
  85. package/runtime/lib/core/main-thread-ref-id.js.map +1 -1
  86. package/runtime/lib/core/reload-scoped-event.d.ts +5 -0
  87. package/runtime/lib/core/reload-scoped-event.js +26 -0
  88. package/runtime/lib/core/reload-scoped-event.js.map +1 -0
  89. package/runtime/lib/core/reload-version.js +4 -3
  90. package/runtime/lib/core/reload-version.js.map +1 -1
  91. package/runtime/lib/core/runtime-destroy.d.ts +2 -0
  92. package/runtime/lib/core/runtime-destroy.js +10 -1
  93. package/runtime/lib/core/runtime-destroy.js.map +1 -1
  94. package/runtime/lib/core/thread-function-call/main-thread.js +5 -4
  95. package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -1
  96. package/runtime/lib/core/thread-function-call/return-value.js +6 -5
  97. package/runtime/lib/core/thread-function-call/return-value.js.map +1 -1
  98. package/runtime/lib/document.js +0 -5
  99. package/runtime/lib/document.js.map +1 -1
  100. package/runtime/lib/element-template/background/commit-hook.js +24 -22
  101. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  102. package/runtime/lib/element-template/background/hydration-listener.js +2 -6
  103. package/runtime/lib/element-template/background/hydration-listener.js.map +1 -1
  104. package/runtime/lib/element-template/client/root.d.ts +1 -1
  105. package/runtime/lib/element-template/client/root.js.map +1 -1
  106. package/runtime/lib/element-template/index.d.ts +2 -2
  107. package/runtime/lib/element-template/index.js.map +1 -1
  108. package/runtime/lib/element-template/internal.d.ts +1 -1
  109. package/runtime/lib/element-template/internal.js +0 -13
  110. package/runtime/lib/element-template/internal.js.map +1 -1
  111. package/runtime/lib/element-template/lynx/env.js.map +1 -1
  112. package/runtime/lib/element-template/main-thread-ref-init-value.d.ts +3 -0
  113. package/runtime/lib/element-template/main-thread-ref-init-value.js +38 -0
  114. package/runtime/lib/element-template/main-thread-ref-init-value.js.map +1 -0
  115. package/runtime/lib/element-template/native/flush.d.ts +1 -0
  116. package/runtime/lib/element-template/native/flush.js +9 -0
  117. package/runtime/lib/element-template/native/flush.js.map +1 -0
  118. package/runtime/lib/element-template/native/index.js +2 -0
  119. package/runtime/lib/element-template/native/index.js.map +1 -1
  120. package/runtime/lib/element-template/native/main-thread-api.js +3 -7
  121. package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
  122. package/runtime/lib/element-template/native/mts-destroy.js +10 -2
  123. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  124. package/runtime/lib/element-template/native/patch-listener.js +58 -54
  125. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  126. package/runtime/lib/element-template/native/reload-background.d.ts +1 -1
  127. package/runtime/lib/element-template/native/reload-background.js +12 -1
  128. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  129. package/runtime/lib/element-template/native/reload-main-thread.d.ts +6 -0
  130. package/runtime/lib/element-template/native/reload-main-thread.js +20 -3
  131. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  132. package/runtime/lib/element-template/protocol/lifecycle-constant.d.ts +2 -1
  133. package/runtime/lib/element-template/protocol/lifecycle-constant.js +1 -0
  134. package/runtime/lib/element-template/protocol/lifecycle-constant.js.map +1 -1
  135. package/runtime/lib/element-template/protocol/types.d.ts +0 -2
  136. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  137. package/runtime/lib/element-template/runtime/page/root-instance.d.ts +0 -1
  138. package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
  139. package/runtime/lib/element-template/runtime/patch.js +4 -0
  140. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  141. package/runtime/lib/element-template/runtime/render/render-components.js +4 -4
  142. package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
  143. package/runtime/lib/element-template/runtime/render/render-main-thread.js +2 -0
  144. package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
  145. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +8 -1
  146. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  147. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +1 -0
  148. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +41 -21
  149. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  150. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +1 -1
  151. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  152. package/runtime/lib/internal.d.ts +1 -1
  153. package/runtime/lib/internal.js +1 -1
  154. package/runtime/lib/internal.js.map +1 -1
  155. package/runtime/lib/lynx-api.d.ts +2 -125
  156. package/runtime/lib/lynx-api.js.map +1 -1
  157. package/runtime/lib/shared/component-stack.d.ts +0 -4
  158. package/runtime/lib/shared/component-stack.js +21 -5
  159. package/runtime/lib/shared/component-stack.js.map +1 -1
  160. package/runtime/lib/shared/index-map.d.ts +4 -1
  161. package/runtime/lib/shared/index-map.js +5 -2
  162. package/runtime/lib/shared/index-map.js.map +1 -1
  163. package/runtime/lib/snapshot/gesture/{processGestureBagkround.js → processGestureBackground.js} +1 -1
  164. package/runtime/lib/snapshot/gesture/processGestureBackground.js.map +1 -0
  165. package/runtime/lib/snapshot/lifecycle/destroy.js +6 -0
  166. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  167. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.d.ts +6 -2
  168. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.js +21 -18
  169. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.js.map +1 -1
  170. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.d.ts +0 -1
  171. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js +0 -5
  172. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
  173. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +0 -10
  174. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  175. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +56 -51
  176. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  177. package/runtime/lib/snapshot/lifecycle/reload.d.ts +9 -2
  178. package/runtime/lib/snapshot/lifecycle/reload.js +29 -8
  179. package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
  180. package/runtime/lib/snapshot/list/list.js +3 -2
  181. package/runtime/lib/snapshot/list/list.js.map +1 -1
  182. package/runtime/lib/snapshot/list/listUpdateInfo.js +0 -30
  183. package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
  184. package/runtime/lib/snapshot/lynx/calledByNative.js +14 -11
  185. package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
  186. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js +0 -52
  187. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
  188. package/runtime/lib/snapshot/renderToOpcodes/index.js +10 -10
  189. package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
  190. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js +0 -51
  191. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
  192. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js +1 -1
  193. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
  194. package/runtime/lib/snapshot/snapshot/list.js +2 -1
  195. package/runtime/lib/snapshot/snapshot/list.js.map +1 -1
  196. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.d.ts +4 -2
  197. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.js +11 -11
  198. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.js.map +1 -1
  199. package/runtime/lib/snapshot/worklet/call/runOnMainThread.d.ts +1 -0
  200. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +6 -1
  201. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
  202. package/runtime/lib/snapshot/worklet/ctx.d.ts +1 -1
  203. package/runtime/lib/snapshot/worklet/ctx.js +6 -1
  204. package/runtime/lib/snapshot/worklet/ctx.js.map +1 -1
  205. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js +6 -0
  206. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js.map +1 -1
  207. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  208. package/runtime/lib/worklet-runtime/bindings/index.d.ts +1 -0
  209. package/runtime/lib/worklet-runtime/bindings/index.js +1 -0
  210. package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
  211. package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +8 -0
  212. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +23 -0
  213. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +1 -0
  214. package/runtime/lib/worklet-runtime/delayRunOnBackground.js +3 -1
  215. package/runtime/lib/worklet-runtime/delayRunOnBackground.js.map +1 -1
  216. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.d.ts +2 -0
  217. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +8 -1
  218. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
  219. package/runtime/lib/worklet-runtime/listeners.js +2 -2
  220. package/runtime/lib/worklet-runtime/listeners.js.map +1 -1
  221. package/runtime/lib/worklet-runtime/mainThreadObject.js +3 -1
  222. package/runtime/lib/worklet-runtime/mainThreadObject.js.map +1 -1
  223. package/runtime/lib/worklet-runtime/runOnMainThread.js +4 -3
  224. package/runtime/lib/worklet-runtime/runOnMainThread.js.map +1 -1
  225. package/runtime/lib/worklet-runtime/workletRuntime.js +2 -0
  226. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  227. package/runtime/package.json +2 -3
  228. package/runtime/src/core/background-function/exec-map.ts +9 -0
  229. package/runtime/src/core/background-function/run-on-background.ts +14 -16
  230. package/runtime/src/core/capture-main-thread-object.ts +2 -1
  231. package/runtime/src/core/card-lifetime.ts +18 -0
  232. package/runtime/src/core/compat/legacy-react-runtime.ts +1 -1
  233. package/runtime/src/core/entry-reload.ts +63 -0
  234. package/runtime/src/core/lynx/lazy-bundle.ts +5 -2
  235. package/runtime/src/core/lynx-data-processors.ts +124 -1
  236. package/runtime/src/core/main-thread-ref-id.ts +5 -4
  237. package/runtime/src/core/reload-scoped-event.ts +35 -0
  238. package/runtime/src/core/reload-version.ts +5 -3
  239. package/runtime/src/core/runtime-destroy.ts +12 -1
  240. package/runtime/src/core/thread-function-call/main-thread.ts +4 -4
  241. package/runtime/src/core/thread-function-call/return-value.ts +6 -5
  242. package/runtime/src/document.ts +0 -6
  243. package/runtime/src/element-template/background/commit-hook.ts +32 -23
  244. package/runtime/src/element-template/background/hydration-listener.ts +2 -6
  245. package/runtime/src/element-template/client/root.ts +1 -3
  246. package/runtime/src/element-template/index.ts +2 -2
  247. package/runtime/src/element-template/internal.ts +0 -20
  248. package/runtime/src/element-template/lynx/env.ts +1 -1
  249. package/runtime/src/element-template/main-thread-ref-init-value.ts +44 -0
  250. package/runtime/src/element-template/native/flush.ts +10 -0
  251. package/runtime/src/element-template/native/index.ts +2 -0
  252. package/runtime/src/element-template/native/main-thread-api.ts +3 -6
  253. package/runtime/src/element-template/native/mts-destroy.ts +10 -2
  254. package/runtime/src/element-template/native/patch-listener.ts +62 -62
  255. package/runtime/src/element-template/native/reload-background.ts +13 -1
  256. package/runtime/src/element-template/native/reload-main-thread.ts +32 -3
  257. package/runtime/src/element-template/protocol/lifecycle-constant.ts +1 -0
  258. package/runtime/src/element-template/protocol/types.ts +0 -15
  259. package/runtime/src/element-template/runtime/page/root-instance.ts +0 -1
  260. package/runtime/src/element-template/runtime/patch.ts +5 -0
  261. package/runtime/src/element-template/runtime/render/render-components.ts +3 -3
  262. package/runtime/src/element-template/runtime/render/render-main-thread.ts +2 -0
  263. package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -1
  264. package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +51 -21
  265. package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +2 -2
  266. package/runtime/src/element-template/types.d.ts +6 -0
  267. package/runtime/src/internal.ts +1 -1
  268. package/runtime/src/lynx-api.ts +2 -124
  269. package/runtime/src/shared/component-stack.ts +22 -5
  270. package/runtime/src/shared/index-map.ts +3 -2
  271. package/runtime/src/snapshot/lifecycle/destroy.ts +7 -0
  272. package/runtime/src/snapshot/lifecycle/event/firstScreenSync.ts +26 -19
  273. package/runtime/src/snapshot/lifecycle/patch/snapshotPatch.ts +0 -5
  274. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +0 -10
  275. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +62 -58
  276. package/runtime/src/snapshot/lifecycle/reload.ts +47 -11
  277. package/runtime/src/snapshot/list/list.ts +9 -2
  278. package/runtime/src/snapshot/list/listUpdateInfo.ts +0 -52
  279. package/runtime/src/snapshot/lynx/calledByNative.ts +14 -13
  280. package/runtime/src/snapshot/renderToOpcodes/hydrate.ts +0 -58
  281. package/runtime/src/snapshot/renderToOpcodes/index.ts +8 -8
  282. package/runtime/src/snapshot/renderToOpcodes/opcodes.ts +0 -57
  283. package/runtime/src/snapshot/snapshot/backgroundSnapshot.ts +1 -1
  284. package/runtime/src/snapshot/snapshot/list.ts +5 -1
  285. package/runtime/src/snapshot/snapshot/snapshotInstanceManager.ts +19 -12
  286. package/runtime/src/snapshot/worklet/call/runOnMainThread.ts +10 -1
  287. package/runtime/src/snapshot/worklet/ctx.ts +10 -1
  288. package/runtime/src/snapshot/worklet/ref/updateInitValue.ts +6 -0
  289. package/runtime/src/worklet-runtime/README.md +33 -0
  290. package/runtime/src/worklet-runtime/bindings/index.ts +2 -0
  291. package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +25 -0
  292. package/runtime/src/worklet-runtime/delayRunOnBackground.ts +3 -1
  293. package/runtime/src/worklet-runtime/jsFunctionLifecycle.ts +8 -1
  294. package/runtime/src/worklet-runtime/listeners.ts +4 -7
  295. package/runtime/src/worklet-runtime/mainThreadObject.ts +2 -1
  296. package/runtime/src/worklet-runtime/runOnMainThread.ts +4 -3
  297. package/runtime/src/worklet-runtime/workletRuntime.ts +2 -0
  298. package/runtime/types/types.d.ts +3 -1
  299. package/runtime/vitest.config.ts +0 -2
  300. package/testing-library/.turbo/turbo-build.log +7 -7
  301. package/testing-library/src/__tests__/act.test.jsx +0 -7
  302. package/testing-library/src/__tests__/alog.test.jsx +12 -54
  303. package/testing-library/src/__tests__/end-to-end.test.jsx +0 -7
  304. package/testing-library/src/__tests__/renderComponent.test.jsx +0 -14
  305. package/testing-library/src/__tests__/worklet.test.jsx +59 -5
  306. package/transform/.turbo/turbo-build$colon$wasm.log +63 -136
  307. package/transform/__test__/defines.spec.js +137 -0
  308. package/transform/__test__/object-methods.spec.js +79 -0
  309. package/transform/crates/swc_plugin_worklet/definition_imports.rs +97 -0
  310. package/transform/crates/swc_plugin_worklet/extract_ident.rs +99 -49
  311. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +56 -48
  312. package/transform/crates/swc_plugin_worklet/lib.rs +400 -595
  313. package/transform/crates/swc_plugin_worklet/napi.rs +4 -6
  314. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +6 -1
  315. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +7 -1
  316. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +9 -0
  317. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +12 -1
  318. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +1 -1
  319. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +1 -1
  320. package/transform/dist/wasm.cjs +1 -1
  321. package/transform/index.d.ts +3 -1
  322. package/transform/package.json +0 -1
  323. package/transform/src/lib.rs +173 -12
  324. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +105 -32
  325. package/transform/swc-plugin-reactlynx/src/lib.rs +1 -4
  326. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  327. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +193 -194
  328. package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +0 -1
  329. package/runtime/lib/snapshot/worklet/hmr.d.ts +0 -1
  330. package/runtime/lib/snapshot/worklet/hmr.js +0 -28
  331. package/runtime/lib/snapshot/worklet/hmr.js.map +0 -1
  332. package/runtime/src/snapshot/worklet/hmr.ts +0 -34
  333. /package/runtime/lib/snapshot/gesture/{processGestureBagkround.d.ts → processGestureBackground.d.ts} +0 -0
  334. /package/runtime/src/snapshot/gesture/{processGestureBagkround.ts → processGestureBackground.ts} +0 -0
@@ -2,8 +2,9 @@
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 { afterEach, describe, expect, it, vi } from 'vitest';
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
6
 
7
+ import { flushElementTemplate } from '../../../../src/element-template/native/flush.js';
7
8
  import {
8
9
  __etAttrPlanMap,
9
10
  adaptMTEventAttrSlot,
@@ -12,18 +13,36 @@ import {
12
13
  } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
13
14
  import {
14
15
  attachMainThreadDynamicAttrRefsForSubtree,
16
+ applyPendingMainThreadRefAttachments,
15
17
  clearMainThreadDynamicAttrState,
16
18
  deleteMainThreadDynamicAttrStateForSubtree,
17
19
  detachMainThreadDynamicAttrRefsForSubtree,
18
20
  getMainThreadDynamicAttrState,
19
21
  initializeMainThreadDynamicAttrSlots,
22
+ prepareMainThreadDynamicAttrSlotsForNative,
20
23
  updateMainThreadEventAttrSlot as setMTEventSlot,
21
24
  updateMainThreadRefAttrSlot as setMTRefSlotImpl,
22
25
  } from '../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
23
26
 
27
+ import { retainMainThreadBackgroundFunctionCtx } from '../../../../src/element-template/runtime/template/main-thread-background-function.js';
28
+ import { prepareMTEventCtxForNative } from '../../../../src/element-template/runtime/template/main-thread-event-ctx.js';
29
+ import type { MTEventNativeWrapper } from '../../../../src/element-template/runtime/template/main-thread-event-ctx.js';
30
+ import type { Worklet } from '../../../../src/worklet-runtime/bindings/types.js';
31
+ import { initWorklet } from '../../../../src/worklet-runtime/workletRuntime.js';
32
+
24
33
  const MT_EVENT_TEMPLATE = '_et_mt_event';
25
34
  const MT_REF_TEMPLATE = '_et_mt_ref';
26
- const TEST_NATIVE_REF = { id: 'dynamic-attr-target' } as unknown as ElementTemplateHandle;
35
+ const TEST_NATIVE_REF = { id: 'dynamic-attr-template' } as unknown as ElementTemplateHandle;
36
+ const TEST_ELEMENT_REF = { id: 'dynamic-attr-element' } as unknown as ElementRef;
37
+
38
+ function completeRefAttachments(): void {
39
+ applyPendingMainThreadRefAttachments();
40
+ }
41
+
42
+ function attachAndComplete(handles: Parameters<typeof attachMainThreadDynamicAttrRefsForSubtree>[0]): void {
43
+ attachMainThreadDynamicAttrRefsForSubtree(handles);
44
+ completeRefAttachments();
45
+ }
27
46
 
28
47
  function setMTRefSlot(
29
48
  handleId: number,
@@ -33,6 +52,7 @@ function setMTRefSlot(
33
52
  nativeRef: ElementTemplateHandle = TEST_NATIVE_REF,
34
53
  ): void {
35
54
  setMTRefSlotImpl(handleId, attrSlotIndex, value, nativeRef, isHydration);
55
+ completeRefAttachments();
36
56
  }
37
57
 
38
58
  function registerMTRefSlots(handleId: number, ...slotIndexes: number[]): void {
@@ -53,6 +73,9 @@ function installJsFunctionLifecycleManager(): {
53
73
  ...previousWorkletImpl,
54
74
  _jsFunctionLifecycleManager: {
55
75
  addRef,
76
+ retainWorkletCtx: (ctx: { _execId?: number }) => {
77
+ if (ctx._execId !== undefined) addRef(ctx._execId, ctx);
78
+ },
56
79
  },
57
80
  } as typeof globalThis.lynxWorkletImpl;
58
81
  return {
@@ -84,11 +107,81 @@ function installRefRuntime(): {
84
107
  }
85
108
 
86
109
  describe('main-thread dynamic attr state', () => {
110
+ beforeEach(() => {
111
+ vi.stubGlobal('__GetElementByAttrSlot', vi.fn(() => TEST_ELEMENT_REF));
112
+ });
87
113
  afterEach(() => {
88
114
  clearMainThreadDynamicAttrState();
89
115
  clearEtAttrPlanMap();
90
116
  });
91
117
 
118
+ it('executes the original Native-held event context before and after hydration', () => {
119
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
120
+ const previousSdkVersion = SystemInfo.lynxSdkVersion;
121
+ try {
122
+ SystemInfo.lynxSdkVersion = '4.0';
123
+ vi.stubGlobal('__JS__', false);
124
+ initWorklet();
125
+ globalThis.registerWorklet('main-thread', 'callback', function(this: Worklet) {
126
+ return this._jsFn;
127
+ });
128
+ const first = prepareMTEventCtxForNative({
129
+ _wkltId: 'callback',
130
+ _jsFn: { cb: { _isFirstScreen: true } },
131
+ }) as unknown as MTEventNativeWrapper;
132
+ retainMainThreadBackgroundFunctionCtx(first.value);
133
+ setMTEventSlot(17, 0, first);
134
+ const deliver = (ctx: object) => globalThis.runWorklet(ctx, []);
135
+ expect(deliver(first.value)).toBe(first.value._jsFn);
136
+ const next = { _wkltId: 'callback', _execId: 11, _jsFn: { cb: { _jsFnId: 1 } } };
137
+ retainMainThreadBackgroundFunctionCtx(next);
138
+ setMTEventSlot(17, 0, { type: 'worklet', value: next }, true);
139
+ expect(deliver(next)).toBe(next._jsFn);
140
+ expect(next._jsFn.cb).toEqual({ _jsFnId: 1, _execId: 11 });
141
+ expect(getMainThreadDynamicAttrState(17, 0)).toEqual({ kind: 'mt-event', nativeHeldValue: next });
142
+ deleteMainThreadDynamicAttrStateForSubtree([17]);
143
+ expect(getMainThreadDynamicAttrState(17, 0)).toBeUndefined();
144
+ } finally {
145
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
146
+ SystemInfo.lynxSdkVersion = previousSdkVersion;
147
+ vi.unstubAllGlobals();
148
+ }
149
+ });
150
+
151
+ it('resolves distinct inner nodes before native flush without a layout hook', () => {
152
+ const { updateWorkletRef, restore } = installRefRuntime();
153
+ const flush = vi.fn();
154
+ vi.stubGlobal('__FlushElementTree', flush);
155
+ const firstRef = { _wvid: 7 };
156
+ const secondRef = { _wvid: 8 };
157
+ const secondElement = { id: 'second-inner-node' } as unknown as ElementRef;
158
+ vi.mocked(__GetElementByAttrSlot).mockImplementation((_handle, slot) =>
159
+ slot === 0 ? TEST_ELEMENT_REF : secondElement
160
+ );
161
+ try {
162
+ registerMTRefSlots(17, 0, 1);
163
+ setMTRefSlot(17, 0, { type: 'main-thread-ref', value: firstRef });
164
+ setMTRefSlot(17, 1, { type: 'main-thread-ref', value: secondRef });
165
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: TEST_NATIVE_REF }]);
166
+ expect(updateWorkletRef).not.toHaveBeenCalled();
167
+ const options = { emptyPatch: true, asyncFlush: true };
168
+ flushElementTemplate(options);
169
+ expect(updateWorkletRef.mock.calls).toEqual([[firstRef, TEST_ELEMENT_REF], [secondRef, secondElement]]);
170
+ expect(flush).toHaveBeenCalledWith(undefined, options);
171
+ expect(updateWorkletRef.mock.invocationCallOrder.at(-1)).toBeLessThan(flush.mock.invocationCallOrder[0]!);
172
+ } finally {
173
+ restore();
174
+ }
175
+ });
176
+
177
+ it('fails visibly when native cannot resolve a submitted ref target during ref delivery', () => {
178
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(null);
179
+ registerMTRefSlots(17, 0);
180
+ setMTRefSlot(17, 0, { type: 'main-thread-ref', value: { _wvid: 7 } });
181
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: TEST_NATIVE_REF }]);
182
+ expect(() => applyPendingMainThreadRefAttachments()).toThrow('target 17:0 could not be materialized');
183
+ });
184
+
92
185
  it('records native-held MTEvent ctx by handle and slot without storing the wrapper', () => {
93
186
  const ctx = { _wkltId: 'tap' };
94
187
  const wrapper = { type: 'worklet', value: ctx };
@@ -139,16 +232,16 @@ describe('main-thread dynamic attr state', () => {
139
232
  registerMTRefSlots(17, 0);
140
233
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: firstRef });
141
234
 
142
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: nativeRef }]);
235
+ attachAndComplete([{ uid: 17, ref: nativeRef }]);
143
236
 
144
- expect(updateWorkletRef).toHaveBeenCalledWith(firstRef, nativeRef);
237
+ expect(updateWorkletRef).toHaveBeenCalledWith(firstRef, TEST_ELEMENT_REF);
145
238
  expect(getMainThreadDynamicAttrState(17, 0)).toEqual({
146
239
  kind: 'mt-ref',
147
240
  value: firstRef,
148
241
  });
149
242
 
150
243
  updateWorkletRef.mockClear();
151
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: nativeRef }]);
244
+ attachAndComplete([{ uid: 17, ref: nativeRef }]);
152
245
  expect(updateWorkletRef).not.toHaveBeenCalled();
153
246
 
154
247
  updateWorkletRef.mockClear();
@@ -175,9 +268,9 @@ describe('main-thread dynamic attr state', () => {
175
268
 
176
269
  expect(updateWorkletRef).not.toHaveBeenCalled();
177
270
 
178
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: nativeRef }]);
271
+ attachAndComplete([{ uid: 17, ref: nativeRef }]);
179
272
 
180
- expect(updateWorkletRef).toHaveBeenCalledWith(nextRef, nativeRef);
273
+ expect(updateWorkletRef).toHaveBeenCalledWith(nextRef, TEST_ELEMENT_REF);
181
274
  expect(getMainThreadDynamicAttrState(17, 0)).toEqual({
182
275
  kind: 'mt-ref',
183
276
  value: nextRef,
@@ -193,12 +286,12 @@ describe('main-thread dynamic attr state', () => {
193
286
 
194
287
  try {
195
288
  registerMTRefSlots(17, 0);
196
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: TEST_NATIVE_REF }]);
289
+ attachAndComplete([{ uid: 17, ref: TEST_NATIVE_REF }]);
197
290
  expect(updateWorkletRef).not.toHaveBeenCalled();
198
291
 
199
292
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: ref });
200
293
 
201
- expect(updateWorkletRef).toHaveBeenCalledWith(ref, TEST_NATIVE_REF);
294
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, TEST_ELEMENT_REF);
202
295
  } finally {
203
296
  restore();
204
297
  }
@@ -215,11 +308,11 @@ describe('main-thread dynamic attr state', () => {
215
308
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: ref });
216
309
 
217
310
  updateWorkletRef.mockImplementationOnce(() => {
218
- attachMainThreadDynamicAttrRefsForSubtree(subtree);
311
+ attachAndComplete(subtree);
219
312
  });
220
- attachMainThreadDynamicAttrRefsForSubtree(subtree);
313
+ attachAndComplete(subtree);
221
314
  expect(updateWorkletRef).toHaveBeenCalledTimes(1);
222
- expect(updateWorkletRef).toHaveBeenCalledWith(ref, nativeRef);
315
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, TEST_ELEMENT_REF);
223
316
 
224
317
  updateWorkletRef.mockClear();
225
318
  updateWorkletRef.mockImplementationOnce(() => {
@@ -241,7 +334,7 @@ describe('main-thread dynamic attr state', () => {
241
334
  try {
242
335
  registerMTRefSlots(17, 0);
243
336
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: ref });
244
- attachMainThreadDynamicAttrRefsForSubtree(subtree);
337
+ attachAndComplete(subtree);
245
338
  detachMainThreadDynamicAttrRefsForSubtree(subtree);
246
339
 
247
340
  updateWorkletRef.mockClear();
@@ -254,31 +347,33 @@ describe('main-thread dynamic attr state', () => {
254
347
  }
255
348
  });
256
349
 
257
- it('retains native-held MTEvent ctx recorded from initial attribute slots', () => {
350
+ it('tracks prepared MTEvent input once before committing initial slots', () => {
258
351
  const { addRef, restore } = installJsFunctionLifecycleManager();
259
352
  const ctx = { _execId: 99, _wkltId: 'tap' };
260
353
  __etAttrPlanMap[MT_EVENT_TEMPLATE] = [0, adaptMTEventAttrSlot];
261
354
 
262
355
  try {
263
- initializeMainThreadDynamicAttrSlots(17, MT_EVENT_TEMPLATE, [{
264
- type: 'worklet',
265
- value: ctx,
266
- }]);
356
+ const slots = [{ type: 'worklet', value: ctx }];
357
+ prepareMainThreadDynamicAttrSlotsForNative(MT_EVENT_TEMPLATE, slots);
358
+ expect(getMainThreadDynamicAttrState(17, 0)).toBeUndefined();
359
+ initializeMainThreadDynamicAttrSlots(17, MT_EVENT_TEMPLATE, slots);
267
360
 
268
- expect(addRef).toHaveBeenCalledWith(99, ctx);
361
+ expect(addRef).toHaveBeenCalledExactlyOnceWith(99, ctx);
269
362
  } finally {
270
363
  restore();
271
364
  }
272
365
  });
273
366
 
274
- it('retains native-held MTEvent ctx recorded from updates', () => {
367
+ it('commits MTEvent updates without repeating prepared input tracking', () => {
275
368
  const { addRef, restore } = installJsFunctionLifecycleManager();
276
369
  const ctx = { _execId: 100, _wkltId: 'tap' };
277
370
 
278
371
  try {
372
+ retainMainThreadBackgroundFunctionCtx(ctx);
373
+ expect(getMainThreadDynamicAttrState(17, 3)).toBeUndefined();
279
374
  setMTEventSlot(17, 3, { type: 'worklet', value: ctx });
280
375
 
281
- expect(addRef).toHaveBeenCalledWith(100, ctx);
376
+ expect(addRef).toHaveBeenCalledExactlyOnceWith(100, ctx);
282
377
  } finally {
283
378
  restore();
284
379
  }
@@ -410,7 +505,7 @@ describe('main-thread dynamic attr state', () => {
410
505
  const previousRunWorklet = globalThis.runWorklet;
411
506
  const ref = { _wvid: 7 };
412
507
  const callbackCleanup = vi.fn();
413
- const callback = { _wkltId: 'ref-callback', _unmount: callbackCleanup };
508
+ const callback = { _wkltId: 'ref-callback' };
414
509
  globalThis.runWorklet = vi.fn(() => callbackCleanup);
415
510
 
416
511
  try {
@@ -430,7 +525,7 @@ describe('main-thread dynamic attr state', () => {
430
525
  false,
431
526
  { id: 'callback-target' } as unknown as ElementTemplateHandle,
432
527
  );
433
- attachMainThreadDynamicAttrRefsForSubtree([
528
+ attachAndComplete([
434
529
  { uid: 17, ref: { id: 'ref-target' } as unknown as ElementTemplateHandle },
435
530
  { uid: 18, ref: { id: 'callback-target' } as unknown as ElementTemplateHandle },
436
531
  ]);
@@ -439,7 +534,7 @@ describe('main-thread dynamic attr state', () => {
439
534
 
440
535
  expect(updateWorkletRef).toHaveBeenCalledWith(ref, null);
441
536
  expect(callbackCleanup).toHaveBeenCalledTimes(1);
442
- expect(globalThis.runWorklet).toHaveBeenCalledWith(callback, [{ elementRefptr: { id: 'callback-target' } }]);
537
+ expect(globalThis.runWorklet).toHaveBeenCalledWith(callback, [{ elementRefptr: TEST_ELEMENT_REF }]);
443
538
  expect(getMainThreadDynamicAttrState(17, 0)).toBeUndefined();
444
539
  expect(getMainThreadDynamicAttrState(18, 0)).toBeUndefined();
445
540
  } finally {
@@ -462,7 +557,7 @@ describe('main-thread dynamic attr state', () => {
462
557
  false,
463
558
  { id: 'callback-target' } as unknown as ElementTemplateHandle,
464
559
  );
465
- attachMainThreadDynamicAttrRefsForSubtree([
560
+ attachAndComplete([
466
561
  { uid: 17, ref: { id: 'callback-target' } as unknown as ElementTemplateHandle },
467
562
  ]);
468
563
 
@@ -488,7 +583,7 @@ describe('main-thread dynamic attr state', () => {
488
583
  false,
489
584
  { id: 'target' } as unknown as ElementTemplateHandle,
490
585
  );
491
- attachMainThreadDynamicAttrRefsForSubtree([
586
+ attachAndComplete([
492
587
  { uid: 17, ref: { id: 'target' } as unknown as ElementTemplateHandle },
493
588
  ]);
494
589
 
@@ -4,7 +4,7 @@
4
4
 
5
5
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
6
 
7
- import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
7
+ import { WorkletEvents, registerWorklet } from '@lynx-js/react/worklet-runtime/bindings';
8
8
  import { options } from 'preact';
9
9
  import { createElement } from 'preact/compat';
10
10
  import { useLayoutEffect, useState } from 'preact/hooks';
@@ -16,11 +16,22 @@ import {
16
16
  } from '../../../../src/element-template/background/commit-hook.js';
17
17
  import { destroyElementTemplateBackgroundRuntime } from '../../../../src/element-template/background/destroy.js';
18
18
  import { clearMtsConfigCacheForTesting } from '../../../../src/core/mts-capability.js';
19
- import { takeDelayedRunOnMainThreadData } from '../../../../src/core/thread-function-call/main-thread.js';
19
+ import {
20
+ delayedRunOnMainThreadData,
21
+ takeDelayedRunOnMainThreadData,
22
+ } from '../../../../src/core/thread-function-call/main-thread.js';
20
23
  import { resetFunctionCallReturnListener } from '../../../../src/core/thread-function-call/return-value.js';
24
+ import {
25
+ installElementTemplatePatchListener,
26
+ resetElementTemplatePatchListener,
27
+ } from '../../../../src/element-template/native/patch-listener.js';
28
+ import { reloadBackground } from '../../../../src/element-template/native/reload-background.js';
29
+ import { reloadMainThread } from '../../../../src/element-template/native/reload-main-thread.js';
21
30
  import { resetElementTemplateBackgroundFunctionRuntime } from '../../../../src/element-template/runtime/template/main-thread-background-function.js';
22
31
  import { ElementTemplateLifecycleConstant } from '../../../../src/element-template/protocol/lifecycle-constant.js';
23
32
  import { parseElementTemplateUpdateEventPayload } from '../../../../src/element-template/protocol/update-event.js';
33
+ import { createElementTemplatePage, setupPage } from '../../../../src/element-template/runtime/page/page.js';
34
+ import { initWorklet } from '../../../../src/worklet-runtime/workletRuntime.js';
24
35
  import { ElementTemplateEnvManager } from '../../test-utils/debug/envManager.js';
25
36
 
26
37
  describe('ElementTemplate runOnMainThread', () => {
@@ -60,6 +71,7 @@ describe('ElementTemplate runOnMainThread', () => {
60
71
  worklet,
61
72
  params: ['hello'],
62
73
  resolveId: 1,
74
+ reloadVersion: 0,
63
75
  }),
64
76
  });
65
77
 
@@ -208,6 +220,7 @@ describe('ElementTemplate runOnMainThread', () => {
208
220
  worklet,
209
221
  params: ['from-layout-effect'],
210
222
  resolveId: 1,
223
+ reloadVersion: 0,
211
224
  }),
212
225
  });
213
226
  expect(takeDelayedRunOnMainThreadData()).toEqual([]);
@@ -262,6 +275,80 @@ describe('ElementTemplate runOnMainThread', () => {
262
275
  expect(removeEventListener).toHaveBeenCalledWith(WorkletEvents.FunctionCallRet, expect.any(Function));
263
276
  });
264
277
 
278
+ it('drops pre-hydration and unmount calls before a reload reuses their return IDs', async () => {
279
+ const oldResult = vi.fn();
280
+ const cleanupResult = vi.fn();
281
+ const cleanup = vi.fn(() => {
282
+ void runOnMainThread({ _wkltId: 'unmount-call' } as unknown as () => string)().then(cleanupResult);
283
+ expect(delayedRunOnMainThreadData.map(data => data.resolveId)).toEqual([1, 2]);
284
+ });
285
+ function App() {
286
+ useLayoutEffect(() => cleanup, []);
287
+ return null;
288
+ }
289
+
290
+ const oldCall = vi.fn(() => 'old-page');
291
+ const unmountCall = vi.fn(() => 'old-page');
292
+ const newCall = vi.fn(() => 'new-page');
293
+ const workletGlobals = {
294
+ lynxWorkletImpl: globalThis.lynxWorkletImpl as unknown,
295
+ runWorklet: globalThis.runWorklet as unknown,
296
+ registerWorklet: globalThis.registerWorklet as unknown,
297
+ registerWorkletInternal: globalThis.registerWorkletInternal as unknown,
298
+ };
299
+ const dispatch = vi.spyOn(lynx.getCoreContext(), 'dispatchEvent');
300
+ try {
301
+ envManager.switchToMainThread();
302
+ initWorklet();
303
+ registerWorklet('main-thread', 'old-call', oldCall);
304
+ registerWorklet('main-thread', 'unmount-call', unmountCall);
305
+ registerWorklet('main-thread', 'new-call', newCall);
306
+ setupPage(createElementTemplatePage());
307
+ root.render(null);
308
+ installElementTemplatePatchListener();
309
+ envManager.switchToBackground();
310
+ root.render(createElement(App));
311
+ void runOnMainThread({ _wkltId: 'old-call' } as unknown as () => string)().then(oldResult);
312
+ expect(delayedRunOnMainThreadData[0]?.resolveId).toBe(1);
313
+
314
+ reloadBackground({});
315
+ expect(cleanup).toHaveBeenCalledOnce();
316
+ expect(delayedRunOnMainThreadData).toEqual([]);
317
+ // The harness shares resolver state between threads. Finish MTS reload
318
+ // before registering the new BTS call, then deliver the real hydration.
319
+ envManager.switchToMainThread(() => reloadMainThread({}, { reloadTemplate: true }));
320
+ let result!: Promise<string>;
321
+ envManager.switchToBackground(() => {
322
+ result = runOnMainThread({ _wkltId: 'new-call' } as unknown as () => string)();
323
+ expect(delayedRunOnMainThreadData[0]?.resolveId).toBe(1);
324
+ });
325
+
326
+ const update = dispatch.mock.calls.find(([event]) =>
327
+ String(event.type) === ElementTemplateLifecycleConstant.update
328
+ )?.[0];
329
+ expect(parseElementTemplateUpdateEventPayload(update?.data)).toMatchObject({
330
+ isHydration: true,
331
+ delayedRunOnMainThreadData: [{ worklet: { _wkltId: 'new-call' }, params: [], resolveId: 1 }],
332
+ });
333
+ envManager.switchToMainThread();
334
+ envManager.switchToBackground();
335
+
336
+ expect(newCall).toHaveBeenCalledExactlyOnceWith();
337
+ expect(oldCall).not.toHaveBeenCalled();
338
+ expect(unmountCall).not.toHaveBeenCalled();
339
+ await expect(result).resolves.toBe('new-page');
340
+ expect(oldResult).not.toHaveBeenCalled();
341
+ expect(cleanupResult).not.toHaveBeenCalled();
342
+ } finally {
343
+ // Do not enqueue another cleanup call while disposing the replacement tree.
344
+ cleanup.mockImplementation(() => {});
345
+ envManager.switchToBackground(() => destroyElementTemplateBackgroundRuntime());
346
+ envManager.switchToMainThread(() => resetElementTemplatePatchListener());
347
+ envManager.switchToBackground();
348
+ Object.assign(globalThis, workletGlobals);
349
+ }
350
+ });
351
+
265
352
  it('throws on the main thread', () => {
266
353
  envManager.switchToMainThread();
267
354
 
@@ -9,6 +9,7 @@ import {
9
9
  type CompiledFixtureTarget,
10
10
  } from './compiledFixtureCompiler.js';
11
11
  import { primeCompiledFixtureTemplates } from './compiledFixtureRegistry.js';
12
+ import type { ElementTemplateEnvManager } from './envManager.js';
12
13
 
13
14
  export interface CompiledFixtureModuleExports {
14
15
  App: (props?: Record<string, unknown>) => JSX.Element;
@@ -33,6 +34,7 @@ export async function loadCompiledFixtureModule<T extends object = CompiledFixtu
33
34
  interface LoadCompiledFixturePairOptions {
34
35
  backgroundTarget?: CompiledFixtureTarget;
35
36
  enableWorkletTransform?: boolean;
37
+ envManager?: ElementTemplateEnvManager;
36
38
  isDynamicComponent?: boolean;
37
39
  mainTarget?: CompiledFixtureTarget;
38
40
  registerMainTemplates?: boolean;
@@ -48,6 +50,7 @@ export async function loadCompiledFixturePair<T extends object = CompiledFixture
48
50
  const {
49
51
  backgroundTarget = 'JS',
50
52
  enableWorkletTransform = false,
53
+ envManager,
51
54
  isDynamicComponent = false,
52
55
  mainTarget = 'LEPUS',
53
56
  registerMainTemplates = true,
@@ -61,6 +64,7 @@ export async function loadCompiledFixturePair<T extends object = CompiledFixture
61
64
  if (registerMainTemplates) {
62
65
  primeCompiledFixtureTemplates(mainArtifact);
63
66
  }
67
+ envManager?.switchToMainThread();
64
68
  const mainModule = await loadCompiledFixtureModule<T>(mainArtifact);
65
69
 
66
70
  const backgroundArtifact = await compileFixtureSource(sourcePath, {
@@ -68,6 +72,7 @@ export async function loadCompiledFixturePair<T extends object = CompiledFixture
68
72
  isDynamicComponent,
69
73
  target: backgroundTarget,
70
74
  });
75
+ envManager?.switchToBackground();
71
76
  const backgroundModule = await loadCompiledFixtureModule<T>(backgroundArtifact);
72
77
 
73
78
  return { backgroundModule, mainModule };
@@ -4,6 +4,10 @@
4
4
 
5
5
  import { flushCoreContextEvents, flushJSContextEvents } from '../mock/mockNativePapi/context.js';
6
6
  import { installMainThreadHooks } from '../../../../src/core/hooks/mainThreadImpl.js';
7
+ import { takeMainThreadRefInitValuePatch } from '../../../../src/core/main-thread-ref-init-value.js';
8
+ import { clearMainThreadRefLastIdForTesting } from '../../../../src/core/main-thread-ref.js';
9
+ import { clearMtsConfigCacheForTesting } from '../../../../src/core/mts-capability.js';
10
+ import { resetTimingState } from '../../../../src/core/performance.js';
7
11
  import { setupBackgroundElementTemplateDocument } from '../../../../src/element-template/background/document.js';
8
12
  import { BackgroundPageRootInstance } from '../../../../src/element-template/background/instance.js';
9
13
  import { backgroundElementTemplateInstanceManager } from '../../../../src/element-template/background/manager.js';
@@ -21,11 +25,6 @@ type EnvTarget = typeof globalThis & {
21
25
  __USE_ELEMENT_TEMPLATE__: boolean | undefined;
22
26
  };
23
27
 
24
- type RootWithTemplates = RootRef & {
25
- __jsx?: unknown;
26
- __opcodes?: unknown;
27
- };
28
-
29
28
  export class ElementTemplateEnvManager {
30
29
  private mainRoot: RootRef | undefined;
31
30
  private backgroundRoot: BackgroundPageRootInstance | undefined;
@@ -77,11 +76,15 @@ export class ElementTemplateEnvManager {
77
76
  this.backgroundRoot = undefined;
78
77
  // @ts-expect-error - allow reset to undefined during tests
79
78
  setRoot(undefined);
79
+ resetTimingState();
80
80
 
81
81
  backgroundElementTemplateInstanceManager.clear();
82
82
  backgroundElementTemplateInstanceManager.nextId = 0;
83
83
  elementTemplateRegistry.clear();
84
84
  resetTemplateId();
85
+ takeMainThreadRefInitValuePatch();
86
+ clearMainThreadRefLastIdForTesting();
87
+ clearMtsConfigCacheForTesting();
85
88
 
86
89
  if (initial === 'background') {
87
90
  this.switchToBackground();
@@ -35,6 +35,7 @@ export interface MockNativePapi {
35
35
  mockRemoveElementTemplateNodeFromSlot: any;
36
36
  mockReportError: any;
37
37
  mockFlushElementTree: any;
38
+ mockGetElementByAttrSlot: any;
38
39
  cleanup: () => void;
39
40
  }
40
41
 
@@ -347,8 +348,38 @@ export function installMockNativePapi(
347
348
  },
348
349
  );
349
350
 
350
- const mockFlushElementTree = vi.fn().mockImplementation((element: unknown, options: unknown) => {
351
- nativeLog.push(['__FlushElementTree', formatNode(element), options]);
351
+ const mockFlushElementTree = vi.fn().mockImplementation((element: unknown, flushOptions?: FlushOptions) => {
352
+ nativeLog.push(['__FlushElementTree', formatNode(element), flushOptions]);
353
+ });
354
+
355
+ const rootTargets = new WeakMap<object, object>();
356
+ const mockGetElementByAttrSlot = vi.fn((handle: CompiledTemplateNode, slot: number) => {
357
+ function findTarget(definition: unknown, element: unknown): object | null {
358
+ if (!isRecord(definition) || !isRecord(element)) return null;
359
+ const descriptors = definition['attributesArray'] as Array<{ kind: string; attrSlotIndex?: number }> | undefined;
360
+ if (descriptors?.some(descriptor => descriptor.kind !== 'static' && descriptor.attrSlotIndex === slot)) {
361
+ if (element !== handle) {
362
+ if (typeof element['__mockNativeId'] !== 'number') attachMockNativeId(element);
363
+ return element;
364
+ }
365
+ // Even a root target is an ElementRef distinct from the ET handle.
366
+ let target = rootTargets.get(handle);
367
+ if (!target) {
368
+ target = Object.create(handle) as object;
369
+ attachMockNativeId(target);
370
+ rootTargets.set(handle, target);
371
+ }
372
+ return target;
373
+ }
374
+ const definitions = definition['children'] as unknown[] | undefined;
375
+ const elements = element['children'] as unknown[] | undefined;
376
+ for (const [index, child] of (definitions ?? []).entries()) {
377
+ const target = findTarget(child, elements?.[index]);
378
+ if (target) return target;
379
+ }
380
+ return null;
381
+ }
382
+ return findTarget(handle.__compiledTemplate, handle);
352
383
  });
353
384
 
354
385
  vi.stubGlobal('__CreateElementTemplate', mockCreateCompiledElementTemplate);
@@ -365,6 +396,7 @@ export function installMockNativePapi(
365
396
  vi.stubGlobal('__RemoveNodeFromElementTemplate', mockRemoveElementTemplateNodeFromSlot);
366
397
  vi.stubGlobal('__SerializeElementTemplate', mockSerializeElementTemplate);
367
398
  vi.stubGlobal('__FlushElementTree', mockFlushElementTree);
399
+ vi.stubGlobal('__GetElementByAttrSlot', mockGetElementByAttrSlot);
368
400
  const currentLynx = (globalThis as unknown as { lynx?: any }).lynx;
369
401
  const baseLynx = (currentLynx && typeof currentLynx === 'object') ? currentLynx : {};
370
402
  const baseGetNativeApp = typeof baseLynx.getNativeApp === 'function'
@@ -395,6 +427,7 @@ export function installMockNativePapi(
395
427
  mockRemoveElementTemplateNodeFromSlot: mockRemoveElementTemplateNodeFromSlot,
396
428
  mockReportError: mockReportError,
397
429
  mockFlushElementTree: mockFlushElementTree,
430
+ mockGetElementByAttrSlot,
398
431
  cleanup: (): void => {
399
432
  const errorCalls = mockReportError.mock.calls;
400
433
  if (clearTemplatesOnCleanup) {
@@ -64,6 +64,19 @@ function walkFiles(dir: string, pattern = /\.(?:[cm]?[jt]sx?)$/): string[] {
64
64
  });
65
65
  }
66
66
 
67
+ function getResolvedRelativeImports(file: string): string[] {
68
+ const source = fs.readFileSync(file, 'utf8');
69
+ const relativeImportPattern = /(?:from\s+|import\s*\(\s*|import\s+)['"](\.[^'"]+)['"]/g;
70
+ return Array.from(
71
+ source.matchAll(relativeImportPattern),
72
+ (match) => path.resolve(path.dirname(file), match[1]!),
73
+ );
74
+ }
75
+
76
+ function stripModuleExtension(filePath: string): string {
77
+ return filePath.replace(/\.(?:[cm]?[jt]sx?)$/, '');
78
+ }
79
+
67
80
  describe('snapshot containment guardrails', () => {
68
81
  test('keeps legacy runtime implementation directories under snapshot backend', () => {
69
82
  for (const dir of legacySourceDirs) {
@@ -89,13 +102,14 @@ describe('snapshot containment guardrails', () => {
89
102
  });
90
103
 
91
104
  test('does not import old implementation paths outside the snapshot backend', () => {
92
- const oldPathPattern = new RegExp(
93
- String.raw`(?:from\s+|import\s*\(\s*)['"](?:\./|\../)+(?:${legacySourceDirPattern})(?:/|['"])`,
94
- );
105
+ const legacyRootPrefixes = legacySourceDirs.map((dir) => `${path.join(srcRoot, dir)}${path.sep}`);
95
106
  const offenders = walkFiles(srcRoot)
96
107
  .filter((file) => !file.includes(`${path.sep}src${path.sep}snapshot${path.sep}`))
97
- .filter((file) => !file.includes(`${path.sep}src${path.sep}element-template${path.sep}`))
98
- .filter((file) => oldPathPattern.test(fs.readFileSync(file, 'utf8')))
108
+ .filter((file) =>
109
+ getResolvedRelativeImports(file).some((resolved) =>
110
+ legacyRootPrefixes.some((prefix) => resolved.startsWith(prefix))
111
+ )
112
+ )
99
113
  .map((file) => path.relative(runtimeRoot, file));
100
114
 
101
115
  expect(offenders).toEqual([]);
@@ -103,10 +117,18 @@ describe('snapshot containment guardrails', () => {
103
117
 
104
118
  test('prevents element-template from depending on snapshot private paths', () => {
105
119
  const elementTemplateRoot = path.join(srcRoot, 'element-template');
106
- const forbiddenImportPattern =
107
- /(?:from\s+|import\s*\(\s*)['"](?:\.\/|\.\.\/)+(?:snapshot|internal|root|lifecycle|renderToOpcodes)(?:\/|['"])/;
120
+ const snapshotPrefix = `${path.join(srcRoot, 'snapshot')}${path.sep}`;
121
+ const snapshotRootFiles = new Set(
122
+ ['document', 'index', 'internal', 'lynx', 'lynx-api', 'root', 'runtime-marker'].map((name) =>
123
+ path.join(srcRoot, name)
124
+ ),
125
+ );
108
126
  const offenders = walkFiles(elementTemplateRoot)
109
- .filter((file) => forbiddenImportPattern.test(fs.readFileSync(file, 'utf8')))
127
+ .filter((file) =>
128
+ getResolvedRelativeImports(file).some((resolved) =>
129
+ resolved.startsWith(snapshotPrefix) || snapshotRootFiles.has(stripModuleExtension(resolved))
130
+ )
131
+ )
110
132
  .map((file) => path.relative(runtimeRoot, file));
111
133
 
112
134
  expect(offenders).toEqual([]);
@@ -135,13 +157,25 @@ describe('snapshot containment guardrails', () => {
135
157
  });
136
158
 
137
159
  test('prevents core and shared modules from depending on runtime backends', () => {
138
- const backendImportPattern =
139
- /(?:from\s+|import\s*\(\s*)['"](?:\.\/|\.\.\/)+(?:snapshot|element-template)(?:\/|['"])/;
160
+ const backendPrefixes = [
161
+ `${path.join(srcRoot, 'snapshot')}${path.sep}`,
162
+ `${path.join(srcRoot, 'element-template')}${path.sep}`,
163
+ ];
164
+ const snapshotRootFiles = new Set(
165
+ ['document', 'index', 'internal', 'lynx', 'lynx-api', 'root', 'runtime-marker'].map((name) =>
166
+ path.join(srcRoot, name)
167
+ ),
168
+ );
140
169
  const offenders = [
141
170
  ...walkFiles(coreRoot),
142
171
  ...walkFiles(sharedRoot),
143
172
  ]
144
- .filter((file) => backendImportPattern.test(fs.readFileSync(file, 'utf8')))
173
+ .filter((file) =>
174
+ getResolvedRelativeImports(file).some((resolved) =>
175
+ backendPrefixes.some((prefix) => resolved.startsWith(prefix))
176
+ || snapshotRootFiles.has(stripModuleExtension(resolved))
177
+ )
178
+ )
145
179
  .map((file) => path.relative(runtimeRoot, file));
146
180
 
147
181
  expect(offenders).toEqual([]);