@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
@@ -5,6 +5,7 @@ import { BackgroundElementTemplateInstance } from '../../../../../src/element-te
5
5
  import { backgroundElementTemplateInstanceManager } from '../../../../../src/element-template/background/manager.js';
6
6
  import { root } from '../../../../../src/element-template/index.js';
7
7
  import { resetElementTemplatePatchListener } from '../../../../../src/element-template/native/patch-listener.js';
8
+ import { resetElementTemplateRefInitValueListener } from '../../../../../src/element-template/main-thread-ref-init-value.js';
8
9
  import { __root, setRoot } from '../../../../../src/element-template/runtime/page/root-instance.js';
9
10
  import { serializeBackgroundTree } from '../../../test-utils/debug/serializer.js';
10
11
 
@@ -25,6 +26,7 @@ function setup(): void {
25
26
 
26
27
  function teardown(): void {
27
28
  resetElementTemplatePatchListener();
29
+ resetElementTemplateRefInitValueListener();
28
30
  resetElementTemplateHydrationListener();
29
31
  vi.unstubAllGlobals();
30
32
  (globalThis as { __LYNX_REPORT_ERROR_CALLS?: Error[] }).__LYNX_REPORT_ERROR_CALLS = [];
@@ -19,16 +19,17 @@ const entryFiles = [
19
19
  ];
20
20
 
21
21
  const forbiddenExact = new Set([
22
- path.resolve(runtimeSrc, 'snapshot.ts'),
23
- path.resolve(runtimeSrc, 'backgroundSnapshot.ts'),
24
- path.resolve(runtimeSrc, 'listUpdateInfo.ts'),
25
- path.resolve(runtimeSrc, 'pendingListUpdates.ts'),
22
+ path.resolve(runtimeSrc, 'document.ts'),
23
+ path.resolve(runtimeSrc, 'index.ts'),
24
+ path.resolve(runtimeSrc, 'internal.ts'),
25
+ path.resolve(runtimeSrc, 'lynx-api.ts'),
26
+ path.resolve(runtimeSrc, 'lynx.ts'),
27
+ path.resolve(runtimeSrc, 'root.ts'),
28
+ path.resolve(runtimeSrc, 'runtime-marker.ts'),
26
29
  ]);
27
30
 
28
31
  const forbiddenDirs = [
29
- path.resolve(runtimeSrc, 'lynx') + path.sep,
30
32
  path.resolve(runtimeSrc, 'snapshot') + path.sep,
31
- path.resolve(runtimeSrc, 'lifecycle', 'patch') + path.sep,
32
33
  ];
33
34
 
34
35
  function isForbidden(filePath: string): boolean {
@@ -25,8 +25,7 @@ describe('legacy internal guardrail', () => {
25
25
  expect(ElementTemplateInternal.captureMainThreadObject({ value: 1 })).toBeUndefined();
26
26
  expect('captureMainThreadObject' in ElementTemplateRuntime).toBe(false);
27
27
 
28
- expect(ElementTemplateInternal.registerWorkletOnBackground).toBeTypeOf('function');
29
- expect(() => ElementTemplateInternal.registerWorkletOnBackground('main-thread', 'hash', () => {})).not.toThrow();
28
+ expect('registerWorkletOnBackground' in ElementTemplateInternal).toBe(false);
30
29
  expect('registerWorkletOnBackground' in ElementTemplateRuntime).toBe(false);
31
30
  expect(ElementTemplateInternal.transformToWorklet).toBeTypeOf('function');
32
31
  expect('transformToWorklet' in ElementTemplateRuntime).toBe(false);
@@ -3,6 +3,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
3
3
  import { ElementTemplateEnvManager } from '../test-utils/debug/envManager.js';
4
4
 
5
5
  const envManager = new ElementTemplateEnvManager();
6
+ const installElementTemplateRefInitValueListener = vi.fn();
6
7
 
7
8
  describe('element-template native index wiring', () => {
8
9
  const originalNodeEnv = process.env['NODE_ENV'];
@@ -10,6 +11,10 @@ describe('element-template native index wiring', () => {
10
11
  beforeEach(() => {
11
12
  vi.resetModules();
12
13
  vi.clearAllMocks();
14
+ vi.doMock('../../../src/element-template/main-thread-ref-init-value.js', async (importOriginal) => ({
15
+ ...await importOriginal<typeof import('../../../src/element-template/main-thread-ref-init-value.js')>(),
16
+ installElementTemplateRefInitValueListener,
17
+ }));
13
18
  globalThis.__ALOG_ELEMENT_API__ = undefined;
14
19
  });
15
20
 
@@ -17,6 +22,7 @@ describe('element-template native index wiring', () => {
17
22
  process.env['NODE_ENV'] = originalNodeEnv;
18
23
  globalThis.__ALOG_ELEMENT_API__ = undefined;
19
24
  vi.resetModules();
25
+ vi.doUnmock('../../../src/element-template/main-thread-ref-init-value.js');
20
26
  vi.doUnmock('../../../src/element-template/native/main-thread-api.js');
21
27
  vi.doUnmock('../../../src/element-template/native/patch-listener.js');
22
28
  vi.doUnmock('../../../src/element-template/native/mts-destroy.js');
@@ -98,6 +104,7 @@ describe('element-template native index wiring', () => {
98
104
 
99
105
  expect(initElementTemplatePAPICallAlog).toHaveBeenCalledTimes(1);
100
106
  expect(injectCalledByNative).toHaveBeenCalledTimes(1);
107
+ expect(installElementTemplateRefInitValueListener).toHaveBeenCalledTimes(1);
101
108
  expect(installElementTemplatePatchListener).toHaveBeenCalledTimes(1);
102
109
  expect(installOnMtsDestruction).toHaveBeenCalledTimes(1);
103
110
  expect(initProfileHook).toHaveBeenCalledTimes(1);
@@ -218,6 +225,7 @@ describe('element-template native index wiring', () => {
218
225
  );
219
226
 
220
227
  expect(injectCalledByNative).not.toHaveBeenCalled();
228
+ expect(installElementTemplateRefInitValueListener).not.toHaveBeenCalled();
221
229
  expect(installElementTemplatePatchListener).not.toHaveBeenCalled();
222
230
  expect(installOnMtsDestruction).not.toHaveBeenCalled();
223
231
  });
@@ -4,6 +4,7 @@ import { injectCalledByNative } from '../../../src/element-template/native/main-
4
4
  import { reloadMainThread } from '../../../src/element-template/native/reload-main-thread.js';
5
5
  import { createElementTemplatePage, setupPage } from '../../../src/element-template/runtime/page/page.js';
6
6
  import { renderMainThread } from '../../../src/element-template/runtime/render/render-main-thread.js';
7
+ import { clearMainThreadDynamicAttrState } from '../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
7
8
 
8
9
  const mockedPageModuleState = vi.hoisted(() => ({
9
10
  page: undefined as unknown,
@@ -42,6 +43,7 @@ describe('injectCalledByNative', () => {
42
43
  });
43
44
 
44
45
  afterEach(() => {
46
+ clearMainThreadDynamicAttrState();
45
47
  vi.unstubAllGlobals();
46
48
  vi.clearAllMocks();
47
49
  });
@@ -82,7 +84,7 @@ describe('injectCalledByNative', () => {
82
84
  expect(vi.mocked(reloadMainThread)).not.toHaveBeenCalled();
83
85
  });
84
86
 
85
- it('flushes updateGlobalProps without page or options when options are absent', () => {
87
+ it('flushes updateGlobalProps with default root options when options are absent', () => {
86
88
  injectCalledByNative();
87
89
  const globalAny = globalThis as typeof globalThis & {
88
90
  updateGlobalProps: (data?: Record<string, unknown>, options?: UpdatePageOption) => void;
@@ -92,7 +94,7 @@ describe('injectCalledByNative', () => {
92
94
 
93
95
  globalAny.updateGlobalProps({ theme: 'light' });
94
96
 
95
- expect(__FlushElementTree).toHaveBeenCalledWith();
97
+ expect(__FlushElementTree).toHaveBeenCalledWith(undefined, {});
96
98
  expect(globalThis.lynx.__globalProps).toEqual({ theme: 'dark' });
97
99
  expect(vi.mocked(renderMainThread)).not.toHaveBeenCalled();
98
100
  expect(vi.mocked(reloadMainThread)).not.toHaveBeenCalled();
@@ -110,6 +112,7 @@ describe('injectCalledByNative', () => {
110
112
  expect(vi.mocked(createElementTemplatePage)).toHaveBeenCalledTimes(1);
111
113
  expect(vi.mocked(setupPage)).toHaveBeenCalledWith({ type: 'page', id: '0', children: [] });
112
114
  expect(vi.mocked(renderMainThread)).toHaveBeenCalledTimes(1);
115
+ expect(__FlushElementTree).not.toHaveBeenCalled();
113
116
  });
114
117
 
115
118
  it('merges updatePage data into initData and flushes at the root', () => {
@@ -0,0 +1,100 @@
1
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
+
3
+ import { MainThreadRef, clearMainThreadRefLastIdForTesting } from '../../../src/core/main-thread-ref.js';
4
+ import { takeMainThreadRefInitValuePatch } from '../../../src/core/main-thread-ref-init-value.js';
5
+ import { clearMtsConfigCacheForTesting } from '../../../src/core/mts-capability.js';
6
+ import { increaseReloadVersion } from '../../../src/core/reload-version.js';
7
+ import {
8
+ installElementTemplateRefInitValueListener,
9
+ resetElementTemplateRefInitValueListener,
10
+ sendMTRefInitValueToMainThread,
11
+ } from '../../../src/element-template/main-thread-ref-init-value.js';
12
+ import {
13
+ installElementTemplatePatchListener,
14
+ resetElementTemplatePatchListener,
15
+ } from '../../../src/element-template/native/patch-listener.js';
16
+ import { destroyElementTemplateMainThreadRuntime } from '../../../src/element-template/native/mts-destroy.js';
17
+ import { ElementTemplateLifecycleConstant } from '../../../src/element-template/protocol/lifecycle-constant.js';
18
+ import { getFromWorkletRefMap, initWorkletRef } from '../../../src/worklet-runtime/workletRef.js';
19
+ import { ElementTemplateEnvManager } from '../test-utils/debug/envManager.js';
20
+
21
+ describe('ElementTemplate ref initialization event', () => {
22
+ const env = new ElementTemplateEnvManager();
23
+ let previousImpl: typeof globalThis.lynxWorkletImpl;
24
+ let previousSdkVersion: string;
25
+
26
+ beforeEach(() => {
27
+ env.resetEnv('background');
28
+ previousSdkVersion = SystemInfo.lynxSdkVersion;
29
+ SystemInfo.lynxSdkVersion = '4.0';
30
+ clearMtsConfigCacheForTesting();
31
+ clearMainThreadRefLastIdForTesting();
32
+ takeMainThreadRefInitValuePatch();
33
+ previousImpl = globalThis.lynxWorkletImpl;
34
+ globalThis.lynxWorkletImpl = { ...previousImpl, _refImpl: initWorkletRef() };
35
+ });
36
+
37
+ afterEach(() => {
38
+ env.switchToMainThread();
39
+ resetElementTemplateRefInitValueListener();
40
+ globalThis.lynxWorkletImpl = previousImpl;
41
+ SystemInfo.lynxSdkVersion = previousSdkVersion;
42
+ clearMtsConfigCacheForTesting();
43
+ takeMainThreadRefInitValuePatch();
44
+ vi.unstubAllGlobals();
45
+ });
46
+
47
+ it.each([true, false, undefined])('initializes a retained ref after reload (alog: %s)', (alog) => {
48
+ vi.stubGlobal('__ALOG__', alog);
49
+ const log = vi.spyOn(console, 'alog');
50
+ env.switchToMainThread();
51
+ installElementTemplateRefInitValueListener();
52
+ env.switchToBackground();
53
+ const ref = new MainThreadRef(233);
54
+ const handle = JSON.parse(JSON.stringify(ref));
55
+ sendMTRefInitValueToMainThread();
56
+ // Advance after BTS sent the event, before MTS receives it.
57
+ increaseReloadVersion();
58
+ env.switchToMainThread();
59
+ expect(getFromWorkletRefMap(handle)).toMatchObject({ current: 233 });
60
+ if (alog) {
61
+ expect(log).toHaveBeenCalledWith('[ReactLynxDebug] ElementTemplate BTS -> MTS sendMTRefInitValue:\n[[1,233]]');
62
+ expect(log).toHaveBeenCalledWith('[ReactLynxDebug] ElementTemplate MTS updateMTRefInitValue:\n[[1,233]]');
63
+ } else {
64
+ expect(log.mock.calls.some(([message]) => String(message).includes('MTRefInitValue'))).toBe(false);
65
+ }
66
+ });
67
+
68
+ it('does not send an empty queue or resend initialization', () => {
69
+ const dispatch = vi.spyOn(lynx.getCoreContext(), 'dispatchEvent');
70
+ sendMTRefInitValueToMainThread();
71
+ expect(dispatch).not.toHaveBeenCalled();
72
+ new MainThreadRef('once');
73
+ sendMTRefInitValueToMainThread();
74
+ sendMTRefInitValueToMainThread();
75
+ expect(dispatch).toHaveBeenCalledExactlyOnceWith({
76
+ type: ElementTemplateLifecycleConstant.updateMTRefInitValue,
77
+ data: JSON.stringify([[1, 'once']]),
78
+ });
79
+ });
80
+
81
+ it('keeps its receiver when the UI listener resets and removes it on destruction', () => {
82
+ env.switchToMainThread();
83
+ const apply = vi.spyOn(globalThis.lynxWorkletImpl._refImpl, 'updateWorkletRefInitValueChanges');
84
+ installElementTemplateRefInitValueListener();
85
+ installElementTemplateRefInitValueListener();
86
+ installElementTemplatePatchListener();
87
+ resetElementTemplatePatchListener();
88
+ env.switchToBackground();
89
+ new MainThreadRef('first');
90
+ sendMTRefInitValueToMainThread();
91
+ env.switchToMainThread();
92
+ expect(apply).toHaveBeenCalledOnce();
93
+ destroyElementTemplateMainThreadRuntime();
94
+ env.switchToBackground();
95
+ new MainThreadRef('after-destroy');
96
+ sendMTRefInitValueToMainThread();
97
+ env.switchToMainThread();
98
+ expect(apply).toHaveBeenCalledOnce();
99
+ });
100
+ });
@@ -1,5 +1,6 @@
1
1
  import { afterEach, describe, expect, it, vi } from 'vitest';
2
2
 
3
+ import { JsFunctionLifecycleManager } from '../../../src/worklet-runtime/jsFunctionLifecycle.js';
3
4
  import { installOnMtsDestruction, onMtsDestruction } from '../../../src/element-template/native/mts-destroy.js';
4
5
  import {
5
6
  composeElementTemplateListAttributes,
@@ -7,8 +8,13 @@ import {
7
8
  registerElementTemplateListItem,
8
9
  registerElementTemplateListState,
9
10
  } from '../../../src/element-template/runtime/list/list.js';
11
+ import {
12
+ getNextElementTemplateId,
13
+ reserveElementTemplateId,
14
+ } from '../../../src/element-template/runtime/template/handle.js';
10
15
  import {
11
16
  attachMainThreadDynamicAttrRefsForSubtree,
17
+ applyPendingMainThreadRefAttachments,
12
18
  clearMainThreadDynamicAttrState,
13
19
  getMainThreadDynamicAttrState,
14
20
  initializeMainThreadDynamicAttrSlots,
@@ -92,6 +98,37 @@ describe('mts-destroy', () => {
92
98
  }
93
99
  });
94
100
 
101
+ it('retires callback owners before a new runtime can reuse background execution IDs', () => {
102
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
103
+ const previous = new JsFunctionLifecycleManager();
104
+ const oldCtx = { _wkltId: 'tap', _execId: 3 };
105
+ previous.addRef(3, oldCtx);
106
+ globalThis.lynxWorkletImpl = {
107
+ ...previousWorkletImpl,
108
+ _jsFunctionLifecycleManager: previous,
109
+ } as typeof globalThis.lynxWorkletImpl;
110
+ const dispatch = vi.spyOn(lynx.getJSContext(), 'dispatchEvent');
111
+ try {
112
+ onMtsDestruction();
113
+ const current = globalThis.lynxWorkletImpl._jsFunctionLifecycleManager!;
114
+ const next = { _wkltId: 'tap', _execId: 3 };
115
+ current.addRef(3, next);
116
+ expect(current).not.toBe(previous);
117
+ previous.removeRef(3);
118
+ previous.fire();
119
+ expect(dispatch).not.toHaveBeenCalled();
120
+ current.removeRef(3);
121
+ current.fire();
122
+ expect(dispatch).toHaveBeenCalledExactlyOnceWith({
123
+ type: 'Lynx.Worklet.releaseBackgroundWorkletCtx',
124
+ data: [3],
125
+ });
126
+ } finally {
127
+ dispatch.mockRestore();
128
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
129
+ }
130
+ });
131
+
95
132
  it('clears the element template registry on destruction', () => {
96
133
  const registryRef = {} as ElementTemplateHandle;
97
134
  const mtRef = { _wvid: 7 };
@@ -120,6 +157,8 @@ describe('mts-destroy', () => {
120
157
  ],
121
158
  );
122
159
  attachMainThreadDynamicAttrRefsForSubtree([{ uid: -1, ref: registryRef }]);
160
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue({ id: 'actual-element' } as unknown as ElementRef);
161
+ applyPendingMainThreadRefAttachments();
123
162
 
124
163
  try {
125
164
  expect(elementTemplateRegistry.get(-1)).toBe(registryRef);
@@ -157,6 +196,50 @@ describe('mts-destroy', () => {
157
196
  expect(delayedBackgroundFunctionArray).toHaveLength(0);
158
197
  });
159
198
 
199
+ it('resets main-thread template id counter on destruction', () => {
200
+ expect(reserveElementTemplateId()).toBe(-1);
201
+ expect(reserveElementTemplateId()).toBe(-2);
202
+ expect(getNextElementTemplateId()).toBe(-3);
203
+
204
+ onMtsDestruction();
205
+
206
+ expect(getNextElementTemplateId()).toBe(-1);
207
+ });
208
+
209
+ it('still clears dynamic attr state and resets template id when a materialized main-thread ref cleanup throws', () => {
210
+ const registryRef = {} as ElementTemplateHandle;
211
+ const cleanupError = new Error('mt-ref cleanup failed');
212
+ const throwingRef: { _wkltId: string; _unmount?: () => void } = {
213
+ _wkltId: 'ref-cleanup',
214
+ };
215
+ expect(reserveElementTemplateId()).toBe(-1);
216
+ elementTemplateRegistry.set(-1, registryRef);
217
+ __etAttrPlanMap._et_throwing_destroy = [0, adaptMTEventAttrSlot, 1, adaptMTRefAttrSlot];
218
+ initializeMainThreadDynamicAttrSlots(
219
+ -1,
220
+ '_et_throwing_destroy',
221
+ [
222
+ {
223
+ type: 'worklet',
224
+ value: { _wkltId: 'tap' },
225
+ },
226
+ {
227
+ type: 'main-thread-ref',
228
+ value: throwingRef,
229
+ },
230
+ ],
231
+ );
232
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: -1, ref: registryRef }]);
233
+ throwingRef._unmount = vi.fn(() => {
234
+ throw cleanupError;
235
+ });
236
+
237
+ expect(() => onMtsDestruction()).toThrow(cleanupError);
238
+ expect(getMainThreadDynamicAttrState(-1, 0)).toBeUndefined();
239
+ expect(getMainThreadDynamicAttrState(-1, 1)).toBeUndefined();
240
+ expect(getNextElementTemplateId()).toBe(-1);
241
+ });
242
+
160
243
  it('marks list callbacks destroyed on main-thread runtime destruction', () => {
161
244
  const listRef = { __isNativeRef: true, id: 'list', __mockNativeId: 100 } as unknown as ElementTemplateHandle;
162
245
  const itemRef = { __isNativeRef: true, id: 'item', __mockNativeId: 101 } as unknown as ElementTemplateHandle;
@@ -0,0 +1,104 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+ import { afterEach, describe, expect, it, vi } from 'vitest';
5
+
6
+ import {
7
+ registerBackgroundFunctionCtx,
8
+ runOnBackground,
9
+ } from '../../../src/core/background-function/run-on-background.js';
10
+ import { destroyElementTemplateBackgroundRuntime } from '../../../src/element-template/background/destroy.js';
11
+ import { getReloadVersion } from '../../../src/core/reload-version.js';
12
+ import { WorkletEvents } from '../../../src/worklet-runtime/bindings/events.js';
13
+ import { reloadMainThread } from '../../../src/element-template/native/reload-main-thread.js';
14
+ import { createElementTemplatePage, setupPage } from '../../../src/element-template/runtime/page/page.js';
15
+ import {
16
+ __etAttrPlanMap,
17
+ adaptMTRefAttrSlot,
18
+ clearEtAttrPlanMap,
19
+ } from '../../../src/element-template/runtime/template/attr-slot-plan.js';
20
+ import {
21
+ initializeMainThreadDynamicAttrSlots,
22
+ attachMainThreadDynamicAttrRefsForSubtree,
23
+ applyPendingMainThreadRefAttachments,
24
+ } from '../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
25
+ import { reloadBackground } from '../../../src/element-template/native/reload-background.js';
26
+ import { resetElementTemplateMainThreadBackgroundFunctionRuntime } from '../../../src/element-template/runtime/template/main-thread-background-function.js';
27
+ import { ElementTemplateEnvManager } from '../test-utils/debug/envManager.js';
28
+
29
+ // ET ordinary reload retains the entry, but clears its background function table.
30
+ // Queue a real MTS request, reload before BTS delivery, then deliver the request.
31
+ describe('ElementTemplate dual-thread communication delivery across ordinary reload', () => {
32
+ const env = new ElementTemplateEnvManager();
33
+ afterEach(() => {
34
+ env.switchToMainThread(() => resetElementTemplateMainThreadBackgroundFunctionRuntime());
35
+ env.switchToBackground(() => destroyElementTemplateBackgroundRuntime());
36
+ clearEtAttrPlanMap();
37
+ vi.unstubAllGlobals();
38
+ });
39
+ it('stamps calls from old main-thread ref cleanup with the old version', () => {
40
+ env.resetEnv('main');
41
+ SystemInfo.lynxSdkVersion = '4.0';
42
+ const page = createElementTemplatePage();
43
+ setupPage(page);
44
+ const oldVersion = getReloadVersion();
45
+ const dispatch = vi.spyOn(lynx.getJSContext(), 'dispatchEvent');
46
+ const cleanup = vi.fn(() => {
47
+ void runOnBackground(Object.assign(() => {}, { _execId: 2, _jsFnId: 1 }))();
48
+ });
49
+ vi.stubGlobal('runWorklet', vi.fn(() => cleanup));
50
+ __etAttrPlanMap._reload_dual_thread = [0, adaptMTRefAttrSlot];
51
+ initializeMainThreadDynamicAttrSlots(-1, '_reload_dual_thread', [
52
+ { type: 'main-thread-ref', value: { _wkltId: 'ref-cleanup' } },
53
+ ]);
54
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: -1, ref: page }]);
55
+ vi.mocked(__GetElementByAttrSlot).mockReturnValueOnce({ id: 'actual-element' } as unknown as ElementRef);
56
+ applyPendingMainThreadRefAttachments();
57
+
58
+ reloadMainThread({}, { reloadTemplate: true });
59
+
60
+ expect(cleanup).toHaveBeenCalledOnce();
61
+ const request = dispatch.mock.calls.find(([event]) => event.type === WorkletEvents.runOnBackground)![0];
62
+ expect(JSON.parse(request.data as string).reloadVersion).toBe(oldVersion);
63
+ expect(getReloadVersion()).toBe(oldVersion + 1);
64
+ });
65
+
66
+ it.each([1, 2])('drops a stale request from %i retired contexts before looking up a function', async (ctxCount) => {
67
+ env.resetEnv('background');
68
+ SystemInfo.lynxSdkVersion = '4.0';
69
+ const oldFn = vi.fn();
70
+ let oldCtx;
71
+ for (let i = 0; i < ctxCount; i++) {
72
+ oldCtx = { _wkltId: 'ctx', _c: { fn: { _jsFnId: 1, _fn: oldFn } }, _execId: 0 };
73
+ registerBackgroundFunctionCtx(oldCtx);
74
+ }
75
+ const oldId = oldCtx!._execId;
76
+ env.switchToMainThread();
77
+ void runOnBackground(Object.assign(() => {}, { _execId: oldId, _jsFnId: 1 }))();
78
+
79
+ // The harness shares core modules across threads. Retire MTS resolvers first,
80
+ // as its ordinary reload does, before exercising the real BTS reload below.
81
+ resetElementTemplateMainThreadBackgroundFunctionRuntime();
82
+
83
+ const freshFn = vi.fn(() => 'fresh');
84
+ const freshCtx = { _wkltId: 'ctx', _c: { fn: { _jsFnId: 1, _fn: freshFn } }, _execId: 0 };
85
+ expect(() =>
86
+ env.switchToBackground(() => {
87
+ reloadBackground({});
88
+ registerBackgroundFunctionCtx(freshCtx);
89
+ })
90
+ ).not.toThrow();
91
+ expect(freshCtx._execId).toBeGreaterThan(oldId);
92
+ // A stale request must be dropped before looking up its retired context.
93
+ expect(freshFn).not.toHaveBeenCalled();
94
+ expect(oldFn).not.toHaveBeenCalled();
95
+
96
+ env.switchToMainThread();
97
+ lynx.getJSContext().dispatchEvent({ type: WorkletEvents.releaseBackgroundWorkletCtx, data: [oldId] });
98
+ const result = runOnBackground(Object.assign(() => {}, { _execId: freshCtx._execId, _jsFnId: 1 }))();
99
+ env.switchToBackground();
100
+ env.switchToMainThread();
101
+ await expect(result).resolves.toBe('fresh');
102
+ expect(freshFn).toHaveBeenCalledOnce();
103
+ });
104
+ });
@@ -7,7 +7,8 @@ import { installElementTemplateHydrationListener } from '../../../src/element-te
7
7
  import { BackgroundPageRootInstance } from '../../../src/element-template/background/instance.js';
8
8
  import { profileEnd, profileStart } from '../../../src/element-template/debug/profile.js';
9
9
  import { reloadBackground } from '../../../src/element-template/native/reload-background.js';
10
- import { reloadMainThread } from '../../../src/element-template/native/reload-main-thread.js';
10
+ import { onMtsDestruction } from '../../../src/element-template/native/mts-destroy.js';
11
+ import { completeMainThreadReload, reloadMainThread } from '../../../src/element-template/native/reload-main-thread.js';
11
12
  import { resetEventStateForRuntime } from '../../../src/element-template/prop-adapters/event.js';
12
13
  import { destroyAllElementTemplateListStates } from '../../../src/element-template/runtime/list/list.js';
13
14
  import { setupPage } from '../../../src/element-template/runtime/page/page.js';
@@ -16,6 +17,7 @@ import { elementTemplateRegistry } from '../../../src/element-template/runtime/t
16
17
  import { resetTemplateId } from '../../../src/element-template/runtime/template/handle.js';
17
18
  import {
18
19
  attachMainThreadDynamicAttrRefsForSubtree,
20
+ applyPendingMainThreadRefAttachments,
19
21
  clearMainThreadDynamicAttrState,
20
22
  getMainThreadDynamicAttrState,
21
23
  initializeMainThreadDynamicAttrSlots,
@@ -110,6 +112,10 @@ vi.mock('../../../src/element-template/background/instance.js', () => {
110
112
  };
111
113
  });
112
114
 
115
+ vi.mock('../../../src/element-template/native/mts-destroy.js', () => ({
116
+ onMtsDestruction: vi.fn(),
117
+ }));
118
+
113
119
  vi.mock('../../../src/element-template/debug/profile.js', () => ({
114
120
  profileEnd: vi.fn(),
115
121
  profileStart: vi.fn(),
@@ -215,6 +221,8 @@ describe('ElementTemplate reloadMainThread', () => {
215
221
  ],
216
222
  );
217
223
  attachMainThreadDynamicAttrRefsForSubtree([{ uid: -1, ref: oldRootRef }]);
224
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue({ id: 'actual-element' } as unknown as ElementRef);
225
+ applyPendingMainThreadRefAttachments();
218
226
  expect(getMainThreadDynamicAttrState(-1, 0)).toBeDefined();
219
227
  expect(getMainThreadDynamicAttrState(-1, 1)).toBeDefined();
220
228
  vi.mocked(mockRender).mockReturnValueOnce({
@@ -295,6 +303,74 @@ describe('ElementTemplate reloadMainThread', () => {
295
303
  }
296
304
  });
297
305
 
306
+ it('evaluates the entry again and lets it render into the retained page', () => {
307
+ const supportsReEvalKey = Symbol.for('__LYNX_ENTRY_SUPPORTS_RE_EVAL__');
308
+ const reloaderKey = Symbol.for('__LYNX_MAIN_THREAD_ENTRY_RELOADER__');
309
+ const completerKey = Symbol.for('__LYNX_MAIN_THREAD_RELOAD_COMPLETER__');
310
+ const host = lynx as unknown as Record<symbol, unknown>;
311
+ vi.stubGlobal('__EXPERIMENTAL_RE_EVAL_JS_ON_RELOAD__', true);
312
+ const page = mockedState.page;
313
+ const newJsx = { type: 'NewApp' };
314
+ const events: string[] = [];
315
+ const rootRef = { type: 'new-ref' } as unknown as ElementTemplateHandle;
316
+ const element = { id: 'new-element' } as unknown as ElementRef;
317
+ const callback = { _wkltId: 'reload-ref' };
318
+ const runWorklet = vi.fn(() => {
319
+ events.push('attach');
320
+ return vi.fn();
321
+ });
322
+ const dispatchEvent = vi.fn();
323
+ vi.stubGlobal('runWorklet', runWorklet);
324
+ vi.stubGlobal('__GetElementByAttrSlot', vi.fn(() => element));
325
+ vi.mocked(lynx.getJSContext).mockReturnValue({ dispatchEvent } as ReturnType<typeof lynx.getJSContext>);
326
+ vi.mocked(mockRender).mockImplementationOnce(() => {
327
+ __etAttrPlanMap._et_reload_ref = [0, adaptMTRefAttrSlot];
328
+ initializeMainThreadDynamicAttrSlots(-1, '_et_reload_ref', [{ type: 'main-thread-ref', value: callback }]);
329
+ return { pageAttributes: null, rootRefs: [rootRef], rootSubtreeHandles: [[{ uid: -1, ref: rootRef }]] };
330
+ });
331
+ lynx.__initData = { kept: 'yes', count: 1 };
332
+ vi.mocked(onMtsDestruction).mockImplementation(() => events.push('retire'));
333
+ vi.mocked(increaseReloadVersion).mockImplementationOnce(() => {
334
+ events.push('advance-version');
335
+ return 2;
336
+ });
337
+ host[supportsReEvalKey] = true;
338
+ host[reloaderKey] = () => {
339
+ events.push('evaluate');
340
+ // What evaluating the entry does: reset init data, install its completer, render into a fresh root.
341
+ lynx.__initData = {};
342
+ host[completerKey] = completeMainThreadReload;
343
+ setRoot({ __jsx: newJsx });
344
+ };
345
+ const options = { reloadTemplate: true };
346
+
347
+ try {
348
+ reloadMainThread({ count: 2 }, options);
349
+ } finally {
350
+ delete host[supportsReEvalKey];
351
+ delete host[reloaderKey];
352
+ delete host[completerKey];
353
+ }
354
+
355
+ expect(events).toEqual(['retire', 'advance-version', 'evaluate', 'attach']);
356
+ expect(__GetElementByAttrSlot).toHaveBeenCalledWith(rootRef, 0);
357
+ expect(runWorklet).toHaveBeenCalledWith(callback, [{ elementRefptr: element }]);
358
+ expect(runWorklet.mock.invocationCallOrder[0]!).toBeLessThan(
359
+ vi.mocked(__SerializeElementTemplate).mock.invocationCallOrder[1]!,
360
+ );
361
+ expect(runWorklet.mock.invocationCallOrder[0]!).toBeLessThan(dispatchEvent.mock.invocationCallOrder[0]!);
362
+ expect(runWorklet.mock.invocationCallOrder[0]!).toBeLessThan(
363
+ vi.mocked(__FlushElementTree).mock.invocationCallOrder[0]!,
364
+ );
365
+ expect(increaseReloadVersion).toHaveBeenCalledOnce();
366
+ expect(lynx.__initData).toEqual({ kept: 'yes', count: 2 });
367
+ expect(setupPage).toHaveBeenLastCalledWith(page);
368
+ expect(mockRender).toHaveBeenCalledWith(newJsx, undefined);
369
+ expect(__FlushElementTree).toHaveBeenLastCalledWith(undefined, options);
370
+ // The retained page is not rebuilt by the previous entry.
371
+ expect(elementTemplateRegistry.clear).not.toHaveBeenCalled();
372
+ });
373
+
298
374
  it('rebuilds when the physical page has no root slot', () => {
299
375
  mockedState.root = { __jsx: null };
300
376
  vi.mocked(mockRender).mockReturnValue({
@@ -416,6 +492,8 @@ describe('ElementTemplate reloadMainThread', () => {
416
492
  { uid: -2, ref: firstChildRef },
417
493
  { uid: -3, ref: secondRootRef },
418
494
  ]);
495
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue({ id: 'actual-element' } as unknown as ElementRef);
496
+ applyPendingMainThreadRefAttachments();
419
497
  const nativeRefs = new Map([
420
498
  [-1, firstRootRef],
421
499
  [-2, firstChildRef],