@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,13 @@
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 {
6
+ installElementTemplateRefInitValueListener,
7
+ resetElementTemplateRefInitValueListener,
8
+ } from '../../../../src/element-template/main-thread-ref-init-value.js';
5
9
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
10
 
11
+ import { JsFunctionLifecycleManager } from '../../../../src/worklet-runtime/jsFunctionLifecycle.js';
7
12
  import { getReloadVersion, increaseReloadVersion } from '../../../../src/core/reload-version.js';
8
13
  import type { BackgroundElementTemplateInstance } from '../../../../src/element-template/background/instance.js';
9
14
  import { root } from '../../../../src/element-template/index.js';
@@ -37,6 +42,7 @@ import {
37
42
  } from '../../../../src/element-template/runtime/list/list.js';
38
43
  import {
39
44
  attachMainThreadDynamicAttrRefsForSubtree,
45
+ applyPendingMainThreadRefAttachments,
40
46
  clearMainThreadDynamicAttrState,
41
47
  detachMainThreadDynamicAttrRefsForSubtree,
42
48
  getMainThreadDynamicAttrState,
@@ -109,6 +115,7 @@ function createMaterializedListForFrontendTest(nativeId: number): FiberElement {
109
115
 
110
116
  const MT_EVENT_TEMPLATE = '_et_mt_event';
111
117
  const MT_REF_TEMPLATE = '_et_mt_ref';
118
+ const REF_TARGET = { id: 'materialized-ref-node' } as unknown as ElementRef;
112
119
 
113
120
  function registerMTEventSlotsForTemplate(templateType: string, ...slotIndexes: number[]): void {
114
121
  __etAttrPlanMap[templateType] = slotIndexes.flatMap(slotIndex => [
@@ -154,6 +161,8 @@ function seedMTRefState(
154
161
  attributeSlots[attrSlotIndex] = { type: 'main-thread-ref', value };
155
162
  initializeMainThreadDynamicAttrSlots(handleId, MT_REF_TEMPLATE, attributeSlots);
156
163
  attachMainThreadDynamicAttrRefsForSubtree([{ uid: handleId, ref: nativeRef }]);
164
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(REF_TARGET);
165
+ applyPendingMainThreadRefAttachments();
157
166
  }
158
167
 
159
168
  function installWorkletRefRuntime(): {
@@ -335,6 +344,43 @@ describe('ElementTemplate patch stream (apply)', () => {
335
344
  expect(mockFlushElementTree.mock.calls.length).toBeGreaterThan(0);
336
345
  });
337
346
 
347
+ it.each([false, true])('tracks setter input before Native and commits only on success (throws=%s)', (throws) => {
348
+ const previous = globalThis.lynxWorkletImpl;
349
+ const manager = new JsFunctionLifecycleManager();
350
+ const hydrate = vi.fn();
351
+ globalThis.lynxWorkletImpl = { ...previous, _jsFunctionLifecycleManager: manager, _hydrateCtx: hydrate };
352
+ const oldCtx = { _wkltId: 'tap', _execId: 30 };
353
+ const nextCtx = { _wkltId: 'tap', _execId: 31 };
354
+ elementTemplateRegistry.set(101, {} as ElementTemplateHandle);
355
+ seedMTEventState(101, 0, oldCtx);
356
+ const retain = vi.spyOn(manager, 'addRef');
357
+ const error = new Error('setter failed');
358
+ mockSetElementTemplateAttributeSlot.mockImplementationOnce((_native, _slot, value) => {
359
+ expect(value.value).toBe(nextCtx);
360
+ expect(retain).toHaveBeenCalledExactlyOnceWith(31, nextCtx);
361
+ expect(getMainThreadDynamicAttrState(101, 0)?.nativeHeldValue).toBe(oldCtx);
362
+ if (throws) throw error;
363
+ });
364
+ try {
365
+ const apply = () =>
366
+ applyElementTemplateUpdateCommands([
367
+ ElementTemplateUpdateOps.setMainThreadEvent,
368
+ 101,
369
+ 0,
370
+ { type: 'worklet', value: nextCtx },
371
+ ], true);
372
+ if (throws) expect(apply).toThrow(error);
373
+ else apply();
374
+ expect(getMainThreadDynamicAttrState(101, 0)?.nativeHeldValue).toBe(throws ? oldCtx : nextCtx);
375
+ expect(retain).toHaveBeenCalledExactlyOnceWith(31, nextCtx);
376
+ if (throws) expect(hydrate).not.toHaveBeenCalled();
377
+ else expect(hydrate).toHaveBeenCalledExactlyOnceWith(nextCtx, oldCtx);
378
+ } finally {
379
+ manager.dispose();
380
+ globalThis.lynxWorkletImpl = previous;
381
+ }
382
+ });
383
+
338
384
  it('records MTEvent state after setMainThreadEvent PAPI succeeds', () => {
339
385
  const targetId = 101;
340
386
  const nativeRef = {};
@@ -602,7 +648,11 @@ describe('ElementTemplate patch stream (apply)', () => {
602
648
  ]);
603
649
 
604
650
  expect(mockInsertElementTemplateNodeIntoSlot).toHaveBeenCalledWith(parentRef, 0, nativeRef, null);
605
- expect(updateWorkletRef).toHaveBeenCalledWith(ref, nativeRef);
651
+ expect(updateWorkletRef).not.toHaveBeenCalled();
652
+ applyPendingMainThreadRefAttachments();
653
+ const target = vi.mocked(__GetElementByAttrSlot).mock.results.at(-1)!.value;
654
+ expect(target).not.toBe(nativeRef);
655
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, target);
606
656
  expect(getMainThreadDynamicAttrState(handleId, 0)).toEqual({
607
657
  kind: 'mt-ref',
608
658
  value: ref,
@@ -757,19 +807,19 @@ describe('ElementTemplate patch stream (apply)', () => {
757
807
 
758
808
  expect(mockSetElementTemplateAttributeSlot).not.toHaveBeenCalled();
759
809
  expect(oldCleanup).toHaveBeenCalledTimes(1);
760
- const mountedCallback = runWorklet.mock.calls[0]?.[0] as typeof nextCallback & {
761
- _unmount?: () => void;
762
- };
810
+ const mountedCallback = runWorklet.mock.calls[0]?.[0] as typeof nextCallback;
763
811
  expect(mountedCallback).toEqual(expect.objectContaining({
764
812
  _wkltId: 'next-ref-callback',
765
813
  }));
766
- expect(runWorklet.mock.calls[0]?.[1]).toEqual([{ elementRefptr: nativeRef }]);
767
- expect(mountedCallback._unmount).toBe(nextCleanup);
814
+ expect(runWorklet.mock.calls[0]?.[1]).toEqual([{ elementRefptr: REF_TARGET }]);
815
+ expect(mountedCallback).toHaveProperty('_unmount', nextCleanup);
768
816
  expect(nextCallback).not.toHaveProperty('_unmount');
769
817
  expect(getMainThreadDynamicAttrState(targetId, 0)).toEqual({
770
818
  kind: 'mt-ref',
771
819
  value: mountedCallback,
772
820
  });
821
+ applyElementTemplateUpdateCommands([ElementTemplateUpdateOps.setMainThreadRef, targetId, 0, null]);
822
+ expect(nextCleanup).toHaveBeenCalledOnce();
773
823
  } finally {
774
824
  globalThis.runWorklet = previousRunWorklet;
775
825
  }
@@ -804,10 +854,11 @@ describe('ElementTemplate patch stream (apply)', () => {
804
854
  { type: 'main-thread-ref', value: nextCallback },
805
855
  ], true);
806
856
 
857
+ applyPendingMainThreadRefAttachments();
807
858
  expect(hydrateCtx).toHaveBeenCalledWith(nextCallback, oldCallback);
808
859
  expect(runDelayedWorklet).not.toHaveBeenCalled();
809
860
  expect(oldCleanup).toHaveBeenCalledTimes(1);
810
- expect(globalThis.runWorklet).toHaveBeenCalledWith(nextCallback, [{ elementRefptr: nativeRef }]);
861
+ expect(globalThis.runWorklet).toHaveBeenCalledWith(nextCallback, [{ elementRefptr: REF_TARGET }]);
811
862
  } finally {
812
863
  globalThis.runWorklet = previousRunWorklet;
813
864
  globalThis.lynxWorkletImpl = previousWorkletImpl;
@@ -841,6 +892,7 @@ describe('ElementTemplate patch stream (apply)', () => {
841
892
  { type: 'main-thread-ref', value: nextCallback },
842
893
  ], true);
843
894
 
895
+ applyPendingMainThreadRefAttachments();
844
896
  expect(hydrateCtx).toHaveBeenCalledWith(nextCallback, oldCallback);
845
897
  expect(globalThis.runWorklet).not.toHaveBeenCalled();
846
898
  expect(getMainThreadDynamicAttrState(targetId, 0)).toEqual({
@@ -1388,6 +1440,55 @@ describe('ElementTemplate patch stream (apply)', () => {
1388
1440
  }
1389
1441
  });
1390
1442
 
1443
+ it('cleans hydration handoff and propagates a ref callback error without running later work', () => {
1444
+ const handle = { id: 'throwing-ref-template' } as unknown as ElementTemplateHandle;
1445
+ const target = { id: 'inner-node' } as unknown as ElementRef;
1446
+ const callback = { _wkltId: 'throwing-ref' };
1447
+ const callbackError = new Error('ref callback failed');
1448
+ const runDelayedBackgroundFunctions = vi.fn();
1449
+ const clearFirstScreenWorkletRefMap = vi.fn();
1450
+ const runRunOnMainThreadTask = vi.fn();
1451
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
1452
+ const previousRunWorklet = globalThis.runWorklet;
1453
+ globalThis.lynxWorkletImpl = {
1454
+ ...previousWorkletImpl,
1455
+ _refImpl: { ...createMainThreadRefImplMock(), clearFirstScreenWorkletRefMap },
1456
+ _runOnBackgroundDelayImpl: { runDelayedBackgroundFunctions },
1457
+ _runRunOnMainThreadTask: runRunOnMainThreadTask,
1458
+ };
1459
+ globalThis.runWorklet = vi.fn(() => {
1460
+ throw callbackError;
1461
+ });
1462
+ try {
1463
+ registerMTRefSlots(0);
1464
+ initializeMainThreadDynamicAttrSlots(193, MT_REF_TEMPLATE, [{ type: 'main-thread-ref', value: callback }]);
1465
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 193, ref: handle }]);
1466
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
1467
+ envManager.switchToMainThread();
1468
+ installElementTemplatePatchListener();
1469
+ mockFlushElementTree.mockClear();
1470
+ expect(() => {
1471
+ envManager.switchToBackground();
1472
+ dispatchElementTemplateUpdate({
1473
+ ops: [],
1474
+ isHydration: true,
1475
+ flushOptions: {},
1476
+ delayedRunOnMainThreadData: [{ worklet: { _wkltId: 'should-not-run' }, params: [], resolveId: 33 }],
1477
+ });
1478
+ envManager.switchToMainThread();
1479
+ }).toThrow(callbackError);
1480
+ expect(globalThis.runWorklet).toHaveBeenCalledWith(callback, [{ elementRefptr: target }]);
1481
+ expect(runDelayedBackgroundFunctions).toHaveBeenCalledTimes(1);
1482
+ expect(clearFirstScreenWorkletRefMap).toHaveBeenCalledTimes(1);
1483
+ expect(runRunOnMainThreadTask).not.toHaveBeenCalled();
1484
+ expect(mockFlushElementTree).not.toHaveBeenCalled();
1485
+ } finally {
1486
+ globalThis.runWorklet = previousRunWorklet;
1487
+ clearMainThreadDynamicAttrState();
1488
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
1489
+ }
1490
+ });
1491
+
1391
1492
  it('continues delayed lifecycle when patch apply reports a missing target', () => {
1392
1493
  const runDelayedBackgroundFunctions = vi.fn();
1393
1494
  const runRunOnMainThreadTask = vi.fn();
@@ -1439,7 +1540,100 @@ describe('ElementTemplate patch stream (apply)', () => {
1439
1540
  }
1440
1541
  });
1441
1542
 
1442
- it('applies MainThreadRef init patches before same-payload delayed main-thread tasks', () => {
1543
+ it('delivers pending targets before hydration handoff and delayed tasks, then flushes their writes', () => {
1544
+ const callOrder: string[] = [];
1545
+ const handle = { id: 'pending-template' } as unknown as ElementTemplateHandle;
1546
+ const target = { id: 'inner-node' } as unknown as ElementRef;
1547
+ const ref = { _wvid: 91 };
1548
+ const worklet = { _wkltId: 'uses-hydrated-ref' };
1549
+ const updateWorkletRef = vi.fn(() => callOrder.push('attach'));
1550
+ const clearFirstScreenWorkletRefMap = vi.fn(() => callOrder.push('clear-first-screen'));
1551
+ const runDelayedBackgroundFunctions = vi.fn(() => callOrder.push('background-handoff'));
1552
+ const runRunOnMainThreadTask = vi.fn(() => {
1553
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, target);
1554
+ callOrder.push('task');
1555
+ });
1556
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
1557
+ globalThis.lynxWorkletImpl = {
1558
+ ...previousWorkletImpl,
1559
+ _refImpl: { updateWorkletRef, clearFirstScreenWorkletRefMap },
1560
+ _runOnBackgroundDelayImpl: { runDelayedBackgroundFunctions },
1561
+ _eomImpl: { setShouldFlush: vi.fn() },
1562
+ _runRunOnMainThreadTask: runRunOnMainThreadTask,
1563
+ };
1564
+ try {
1565
+ registerMTRefSlots(0);
1566
+ initializeMainThreadDynamicAttrSlots(191, MT_REF_TEMPLATE, [{ type: 'main-thread-ref', value: ref }]);
1567
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 191, ref: handle }]);
1568
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
1569
+ mockFlushElementTree.mockImplementationOnce(() => callOrder.push('initial-flush'));
1570
+ envManager.switchToMainThread();
1571
+ installElementTemplatePatchListener();
1572
+ envManager.switchToBackground();
1573
+ dispatchElementTemplateUpdate({
1574
+ ops: [],
1575
+ isHydration: true,
1576
+ flushOptions: { emptyPatch: true },
1577
+ delayedRunOnMainThreadData: [{ worklet, params: [], resolveId: 31 }],
1578
+ });
1579
+ envManager.switchToMainThread();
1580
+ expect(callOrder).toEqual(['attach', 'background-handoff', 'clear-first-screen', 'task', 'initial-flush']);
1581
+ expect(runRunOnMainThreadTask).toHaveBeenCalledWith(worklet, [], 31);
1582
+ expect(mockFlushElementTree).toHaveBeenCalledOnce();
1583
+ expect(mockFlushElementTree.mock.calls[0]![1]).toEqual({ emptyPatch: true });
1584
+ } finally {
1585
+ clearMainThreadDynamicAttrState();
1586
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
1587
+ }
1588
+ });
1589
+
1590
+ it('delivers refs synchronously before a later delayed-task payload', () => {
1591
+ const handleId = 192;
1592
+ const handle = { id: 'pending-template' } as unknown as ElementTemplateHandle;
1593
+ const target = { id: 'inner-node' } as unknown as ElementRef;
1594
+ const ref = { _wvid: 92 };
1595
+ const worklet = { _wkltId: 'later-payload-uses-ref' };
1596
+ const updateWorkletRef = vi.fn();
1597
+ const runRunOnMainThreadTask = vi.fn(() => expect(updateWorkletRef).toHaveBeenCalledWith(ref, target));
1598
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
1599
+ globalThis.lynxWorkletImpl = {
1600
+ ...previousWorkletImpl,
1601
+ _refImpl: { updateWorkletRef },
1602
+ _eomImpl: { setShouldFlush: vi.fn() },
1603
+ _runRunOnMainThreadTask: runRunOnMainThreadTask,
1604
+ };
1605
+ try {
1606
+ elementTemplateRegistry.set(handleId, handle);
1607
+ registerMTRefSlots(0);
1608
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: handleId, ref: handle }]);
1609
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
1610
+ envManager.switchToMainThread();
1611
+ installElementTemplatePatchListener();
1612
+ envManager.switchToBackground();
1613
+ dispatchElementTemplateUpdate({
1614
+ ops: [ElementTemplateUpdateOps.setMainThreadRef, handleId, 0, { type: 'main-thread-ref', value: ref }],
1615
+ flushOptions: {},
1616
+ });
1617
+ envManager.switchToMainThread();
1618
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, target);
1619
+ envManager.switchToBackground();
1620
+ dispatchElementTemplateUpdate({
1621
+ ops: [],
1622
+ flushOptions: { emptyPatch: true },
1623
+ delayedRunOnMainThreadData: [{ worklet, params: [], resolveId: 32 }],
1624
+ });
1625
+ envManager.switchToMainThread();
1626
+ expect(runRunOnMainThreadTask).toHaveBeenCalledWith(worklet, [], 32);
1627
+ expect(__GetElementByAttrSlot).toHaveBeenCalledOnce();
1628
+ expect(updateWorkletRef).toHaveBeenCalledOnce();
1629
+ expect(mockFlushElementTree.mock.calls).toEqual([[undefined, {}], [undefined, { emptyPatch: true }]]);
1630
+ } finally {
1631
+ clearMainThreadDynamicAttrState();
1632
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
1633
+ }
1634
+ });
1635
+
1636
+ it('applies independent ref initialization before delayed main-thread tasks', () => {
1443
1637
  const callOrder: string[] = [];
1444
1638
  const updateWorkletRefInitValueChanges = vi.fn(() => {
1445
1639
  callOrder.push('init-patch');
@@ -1461,12 +1655,17 @@ describe('ElementTemplate patch stream (apply)', () => {
1461
1655
 
1462
1656
  try {
1463
1657
  envManager.switchToMainThread();
1658
+ installElementTemplateRefInitValueListener();
1464
1659
  installElementTemplatePatchListener();
1465
1660
  mockFlushElementTree.mockImplementationOnce(() => {
1466
1661
  callOrder.push('flushElementTree');
1467
1662
  });
1468
1663
 
1469
1664
  envManager.switchToBackground();
1665
+ lynx.getCoreContext().dispatchEvent({
1666
+ type: ElementTemplateLifecycleConstant.updateMTRefInitValue,
1667
+ data: JSON.stringify([[1, 'same-payload-init']]),
1668
+ });
1470
1669
  dispatchElementTemplateUpdate({
1471
1670
  ops: [],
1472
1671
  flushOptions: {},
@@ -1477,7 +1676,6 @@ describe('ElementTemplate patch stream (apply)', () => {
1477
1676
  resolveId: 10,
1478
1677
  },
1479
1678
  ],
1480
- mainThreadRefInitValuePatch: [[1, 'same-payload-init']],
1481
1679
  });
1482
1680
  envManager.switchToMainThread();
1483
1681
 
@@ -1489,6 +1687,7 @@ describe('ElementTemplate patch stream (apply)', () => {
1489
1687
  'flushElementTree',
1490
1688
  ]);
1491
1689
  } finally {
1690
+ resetElementTemplateRefInitValueListener();
1492
1691
  globalThis.lynxWorkletImpl = previousWorkletImpl;
1493
1692
  }
1494
1693
  });
@@ -1512,10 +1711,15 @@ describe('ElementTemplate patch stream (apply)', () => {
1512
1711
  try {
1513
1712
  increaseReloadVersion();
1514
1713
  envManager.switchToMainThread();
1714
+ installElementTemplateRefInitValueListener();
1515
1715
  installElementTemplatePatchListener();
1516
1716
  mockFlushElementTree.mockClear();
1517
1717
 
1518
1718
  envManager.switchToBackground();
1719
+ lynx.getCoreContext().dispatchEvent({
1720
+ type: ElementTemplateLifecycleConstant.updateMTRefInitValue,
1721
+ data: JSON.stringify([[1, 'stale-init']]),
1722
+ });
1519
1723
  dispatchElementTemplateUpdate({
1520
1724
  ops: [],
1521
1725
  flushOptions: {},
@@ -1527,7 +1731,6 @@ describe('ElementTemplate patch stream (apply)', () => {
1527
1731
  resolveId: 10,
1528
1732
  },
1529
1733
  ],
1530
- mainThreadRefInitValuePatch: [[1, 'stale-init']],
1531
1734
  });
1532
1735
  envManager.switchToMainThread();
1533
1736
 
@@ -1535,6 +1738,7 @@ describe('ElementTemplate patch stream (apply)', () => {
1535
1738
  expect(runRunOnMainThreadTask).not.toHaveBeenCalled();
1536
1739
  expect(mockFlushElementTree).not.toHaveBeenCalled();
1537
1740
  } finally {
1741
+ resetElementTemplateRefInitValueListener();
1538
1742
  globalThis.lynxWorkletImpl = previousWorkletImpl;
1539
1743
  }
1540
1744
  });
@@ -4,7 +4,7 @@ import {
4
4
  installElementTemplatePatchListener,
5
5
  resetElementTemplatePatchListener,
6
6
  } from '../../../../src/element-template/native/patch-listener.js';
7
- import { setPipeline } from '../../../../src/core/performance.js';
7
+ import { globalPipelineOptions, setPipeline } from '../../../../src/core/performance.js';
8
8
  import { ElementTemplateUpdateOps } from '../../../../src/element-template/protocol/opcodes.js';
9
9
  import { ElementTemplateLifecycleConstant } from '../../../../src/element-template/protocol/lifecycle-constant.js';
10
10
  import {
@@ -105,6 +105,7 @@ describe('ElementTemplate update timing (main thread patch)', () => {
105
105
  ['pipelineID', 'patchChangesEnd'],
106
106
  ['pipelineID', 'mtsRenderEnd'],
107
107
  ]);
108
+ expect(globalPipelineOptions).toBeUndefined();
108
109
  });
109
110
 
110
111
  it('handles updates without pipeline options', () => {
@@ -154,6 +155,7 @@ describe('ElementTemplate update timing (main thread patch)', () => {
154
155
  ['pipelineID', 'parseChangesEnd'],
155
156
  ['pipelineID', 'mtsRenderEnd'],
156
157
  ]);
158
+ expect(globalPipelineOptions).toBeUndefined();
157
159
  });
158
160
 
159
161
  it('marks parse timing for empty update envelopes with pipeline options', () => {
@@ -179,6 +181,7 @@ describe('ElementTemplate update timing (main thread patch)', () => {
179
181
  ['pipelineID', 'parseChangesEnd'],
180
182
  ['pipelineID', 'mtsRenderEnd'],
181
183
  ]);
184
+ expect(globalPipelineOptions).toBeUndefined();
182
185
  });
183
186
 
184
187
  it('accepts update payloads from the current reload version', () => {
@@ -61,7 +61,10 @@ beforeEach(() => {
61
61
  compiledTemplate: {
62
62
  kind: 'element',
63
63
  type: 'view',
64
- attributesArray: [{ kind: 'slot', key: 'id', attrSlotIndex: 0 }],
64
+ attributesArray: [
65
+ { kind: 'slot', key: 'id', attrSlotIndex: 0 },
66
+ { kind: 'slot', key: 'main-thread:ref', attrSlotIndex: 1 },
67
+ ],
65
68
  },
66
69
  },
67
70
  ]);
@@ -46,7 +46,10 @@ beforeEach(() => {
46
46
  compiledTemplate: {
47
47
  kind: 'element',
48
48
  type: 'view',
49
- attributesArray: [{ kind: 'slot', key: 'id', attrSlotIndex: 0 }],
49
+ attributesArray: [
50
+ { kind: 'slot', key: 'id', attrSlotIndex: 0 },
51
+ { kind: 'slot', key: 'main-thread:ref', attrSlotIndex: 1 },
52
+ ],
50
53
  },
51
54
  },
52
55
  ]);
@@ -15,9 +15,11 @@ import {
15
15
  clearEtAttrPlanMap,
16
16
  } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
17
17
  import {
18
+ applyPendingMainThreadRefAttachments,
18
19
  clearMainThreadDynamicAttrState,
19
20
  getMainThreadDynamicAttrState,
20
21
  initializeMainThreadDynamicAttrSlots,
22
+ updateMainThreadRefAttrSlot,
21
23
  } from '../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
22
24
  import { elementTemplateRegistry } from '../../../../src/element-template/runtime/template/registry.js';
23
25
 
@@ -50,6 +52,7 @@ describe('renderMainThread', () => {
50
52
  vi.stubGlobal('__InsertNodeToElementTemplate', vi.fn());
51
53
  vi.stubGlobal('__SetAttributeOfElementTemplate', vi.fn());
52
54
  vi.stubGlobal('__SerializeElementTemplate', vi.fn());
55
+ vi.stubGlobal('__FlushElementTree', vi.fn());
53
56
  elementTemplateRegistry.clear();
54
57
  destroyAllElementTemplateListStates();
55
58
  clearMainThreadDynamicAttrState();
@@ -281,6 +284,8 @@ describe('renderMainThread', () => {
281
284
 
282
285
  it('attaches root MTRef state only after page insertion succeeds', () => {
283
286
  const rootRef = { type: 'root-ref' } as unknown as ElementTemplateHandle;
287
+ const target = { type: 'actual-element' } as unknown as ElementRef;
288
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
284
289
  const ref = { _wvid: 80 };
285
290
  const updateWorkletRef = vi.fn();
286
291
  const previousWorkletImpl = globalThis.lynxWorkletImpl;
@@ -312,7 +317,12 @@ describe('renderMainThread', () => {
312
317
  try {
313
318
  renderMainThread();
314
319
 
315
- expect(updateWorkletRef).toHaveBeenCalledWith(ref, rootRef);
320
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, target);
321
+ expect(__FlushElementTree).not.toHaveBeenCalled();
322
+ expect(updateWorkletRef.mock.invocationCallOrder[0]).toBeLessThan(
323
+ vi.mocked(__SerializeElementTemplate).mock.invocationCallOrder[0]!,
324
+ );
325
+ expect(updateWorkletRef.mock.invocationCallOrder[0]).toBeLessThan(dispatchEvent.mock.invocationCallOrder[0]!);
316
326
  expect(vi.mocked(__InsertNodeToElementTemplate).mock.invocationCallOrder[0]).toBeLessThan(
317
327
  updateWorkletRef.mock.invocationCallOrder[0]!,
318
328
  );
@@ -325,6 +335,96 @@ describe('renderMainThread', () => {
325
335
  }
326
336
  });
327
337
 
338
+ it('delivers first-screen callback effects before a synchronous hydrate roundtrip', () => {
339
+ const rootRef = {} as ElementTemplateHandle;
340
+ const target = {} as ElementRef;
341
+ const first = { _wkltId: 'ref-callback', initialized: false, delayIndices: [] as number[] };
342
+ const hydrated = { _wkltId: 'ref-callback' };
343
+ const events: string[] = [];
344
+ const previousRunWorklet = globalThis.runWorklet;
345
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
346
+ globalThis.runWorklet = vi.fn().mockImplementationOnce(() => {
347
+ events.push('first attach');
348
+ first.initialized = true;
349
+ first.delayIndices.push(0);
350
+ return () => events.push('first cleanup');
351
+ }).mockImplementation(() => {
352
+ events.push('hydrated attach');
353
+ });
354
+ globalThis.lynxWorkletImpl = {
355
+ ...previousWorkletImpl,
356
+ _hydrateCtx: vi.fn((_next, previous) => {
357
+ expect(previous).toBe(first);
358
+ expect(first.initialized).toBe(true);
359
+ expect(first.delayIndices).toEqual([0]);
360
+ events.push('hydrate context');
361
+ }),
362
+ } as typeof globalThis.lynxWorkletImpl;
363
+ __etAttrPlanMap._et_ref = [0, adaptMTRefAttrSlot];
364
+ initializeMainThreadDynamicAttrSlots(-1, '_et_ref', [{ type: 'main-thread-ref', value: first }]);
365
+ vi.mocked(mockRender).mockReturnValue({
366
+ pageAttributes: null,
367
+ rootRefs: [rootRef],
368
+ rootSubtreeHandles: [[{ uid: -1, ref: rootRef }]],
369
+ });
370
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
371
+ vi.mocked(__SerializeElementTemplate).mockImplementation(() => {
372
+ events.push('serialize');
373
+ return {} as ReturnType<typeof __SerializeElementTemplate>;
374
+ });
375
+ dispatchEvent.mockImplementation(() => {
376
+ events.push('dispatch hydrate');
377
+ updateMainThreadRefAttrSlot(-1, 0, { type: 'main-thread-ref', value: hydrated }, rootRef, true);
378
+ applyPendingMainThreadRefAttachments();
379
+ });
380
+ try {
381
+ renderMainThread();
382
+ expect(events).toEqual([
383
+ 'first attach',
384
+ 'serialize',
385
+ 'dispatch hydrate',
386
+ 'first cleanup',
387
+ 'hydrate context',
388
+ 'hydrated attach',
389
+ ]);
390
+ clearMainThreadDynamicAttrState();
391
+ } finally {
392
+ globalThis.runWorklet = previousRunWorklet;
393
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
394
+ }
395
+ });
396
+
397
+ it.each(['missing target', 'callback throws'])(
398
+ 'does not serialize or dispatch hydration when first-screen %s',
399
+ failure => {
400
+ const rootRef = {} as ElementTemplateHandle;
401
+ const previousRunWorklet = globalThis.runWorklet;
402
+ globalThis.runWorklet = vi.fn(() => {
403
+ throw new Error('ref callback failed');
404
+ });
405
+ __etAttrPlanMap._et_ref = [0, adaptMTRefAttrSlot];
406
+ initializeMainThreadDynamicAttrSlots(-1, '_et_ref', [{
407
+ type: 'main-thread-ref',
408
+ value: { _wkltId: 'failed-ref' },
409
+ }]);
410
+ vi.mocked(mockRender).mockReturnValue({
411
+ pageAttributes: null,
412
+ rootRefs: [rootRef],
413
+ rootSubtreeHandles: [[{ uid: -1, ref: rootRef }]],
414
+ });
415
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(failure === 'missing target' ? null : {} as ElementRef);
416
+ try {
417
+ expect(() => renderMainThread()).toThrow(
418
+ failure === 'missing target' ? 'could not be materialized' : 'ref callback failed',
419
+ );
420
+ expect(__SerializeElementTemplate).not.toHaveBeenCalled();
421
+ expect(dispatchEvent).not.toHaveBeenCalled();
422
+ } finally {
423
+ globalThis.runWorklet = previousRunWorklet;
424
+ }
425
+ },
426
+ );
427
+
328
428
  it('keeps root MTRef state blocked when page insertion throws', () => {
329
429
  const rootRef = { type: 'root-ref' } as unknown as ElementTemplateHandle;
330
430
  const ref = { _wvid: 81 };
@@ -22,6 +22,7 @@ import { applyElementTemplateUpdateCommands } from '../../../../src/element-temp
22
22
  import { ElementTemplateUpdateOps } from '../../../../src/element-template/protocol/opcodes.js';
23
23
  import { renderToElementTemplate } from '../../../../src/element-template/runtime/render/render-direct.js';
24
24
  import { resetElementTemplatePatchListener } from '../../../../src/element-template/native/patch-listener.js';
25
+ import { resetElementTemplateRefInitValueListener } from '../../../../src/element-template/main-thread-ref-init-value.js';
25
26
  import { resetTemplateId } from '../../../../src/element-template/runtime/template/handle.js';
26
27
  import { elementTemplateRegistry } from '../../../../src/element-template/runtime/template/registry.js';
27
28
  import type {
@@ -181,6 +182,7 @@ async function compileAndRender(
181
182
  };
182
183
  } finally {
183
184
  resetElementTemplatePatchListener();
185
+ resetElementTemplateRefInitValueListener();
184
186
  fs.rmSync(tempDir, { recursive: true, force: true });
185
187
  }
186
188
  }
@@ -23,6 +23,7 @@ import {
23
23
  clearMainThreadDynamicAttrState,
24
24
  getMainThreadDynamicAttrState,
25
25
  } from '../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
26
+ import { JsFunctionLifecycleManager } from '../../../../src/worklet-runtime/jsFunctionLifecycle.js';
26
27
  import { resetTemplateId } from '../../../../src/element-template/runtime/template/handle.js';
27
28
 
28
29
  describe('ElementTemplateHandle', () => {
@@ -102,6 +103,37 @@ describe('ElementTemplateHandle', () => {
102
103
  });
103
104
  });
104
105
 
106
+ it.each(['success', 'null', 'throw'])(
107
+ 'prepares and tracks create input before Native %s without advancing failed state',
108
+ (outcome) => {
109
+ const previous = globalThis.lynxWorkletImpl;
110
+ const manager = new JsFunctionLifecycleManager();
111
+ globalThis.lynxWorkletImpl = { ...previous, _jsFunctionLifecycleManager: manager };
112
+ const ctx = { _wkltId: 'tap', _execId: 21 };
113
+ const retain = vi.spyOn(manager, 'addRef');
114
+ const error = new Error('create failed');
115
+ __etAttrPlanMap['__Card__:_et_test'] = [0, adaptMTEventAttrSlot];
116
+ mockCreateCompiledElementTemplate.mockImplementationOnce((_key, _url, slots) => {
117
+ expect(slots[0].value).toBe(ctx);
118
+ expect(retain).toHaveBeenCalledExactlyOnceWith(21, ctx);
119
+ expect(getMainThreadDynamicAttrState(-1, 0)).toBeUndefined();
120
+ if (outcome === 'throw') throw error;
121
+ return outcome === 'null' ? null : mockCreatedNativeRef;
122
+ });
123
+ try {
124
+ const create = () =>
125
+ createElementTemplateWithReservedHandle(-1, '_et_test', null, [{ type: 'worklet', value: ctx }], null);
126
+ if (outcome === 'throw') expect(create).toThrow(error);
127
+ else expect(create()).toBe(outcome === 'null' ? null : mockCreatedNativeRef);
128
+ expect(retain).toHaveBeenCalledExactlyOnceWith(21, ctx);
129
+ expect(getMainThreadDynamicAttrState(-1, 0)?.nativeHeldValue).toBe(outcome === 'success' ? ctx : undefined);
130
+ } finally {
131
+ manager.dispose();
132
+ globalThis.lynxWorkletImpl = previous;
133
+ }
134
+ },
135
+ );
136
+
105
137
  it('records dynamic-entry main-thread dynamic attr state with the full template identity', () => {
106
138
  const id = reserveElementTemplateId();
107
139
  const ctx = { _wkltId: 'tap' };