@lynx-js/react 0.129.0 → 0.129.2

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 (282) hide show
  1. package/CHANGELOG.md +24 -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/globalProps.test.ts +27 -6
  6. package/runtime/__test__/core/hooks/useLynxGlobalEventListener.test.jsx +1 -1
  7. package/runtime/__test__/element-template/fixtures/gesture/direct/index.tsx +33 -0
  8. package/runtime/__test__/element-template/fixtures/spread/mts/index.tsx +197 -0
  9. package/runtime/__test__/element-template/imports/no-legacy-imports.test.ts +7 -6
  10. package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +1 -2
  11. package/runtime/__test__/element-template/native/callDestroyLifetimeFun.test.ts +89 -3
  12. package/runtime/__test__/element-template/native/main-thread-api.test.ts +5 -2
  13. package/runtime/__test__/element-template/native/mts-destroy.test.ts +88 -0
  14. package/runtime/__test__/element-template/native/reload-dual-thread-communication.test.ts +3 -0
  15. package/runtime/__test__/element-template/native/reload.test.ts +118 -2
  16. package/runtime/__test__/element-template/runtime/background/global-props/compiled-fixtures.test.tsx +5 -5
  17. package/runtime/__test__/element-template/runtime/background/global-props.test.tsx +1 -1
  18. package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +70 -0
  19. package/runtime/__test__/element-template/runtime/background/ref/compiled-fixtures.test.tsx +3 -4
  20. package/runtime/__test__/element-template/runtime/background/reload.test.ts +74 -0
  21. package/runtime/__test__/element-template/runtime/background/update-timing.test.ts +15 -0
  22. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +212 -7
  23. package/runtime/__test__/element-template/runtime/patch/update-timing.test.ts +4 -1
  24. package/runtime/__test__/element-template/runtime/prop-adapters/gesture-compiled.test.ts +200 -0
  25. package/runtime/__test__/element-template/runtime/prop-adapters/gesture.test.ts +420 -0
  26. package/runtime/__test__/element-template/runtime/prop-adapters/spread-mts-compiled.test.ts +527 -0
  27. package/runtime/__test__/element-template/runtime/prop-adapters/spread.test.ts +185 -6
  28. package/runtime/__test__/element-template/runtime/render/render-direct-native.test.tsx +21 -1
  29. package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +4 -1
  30. package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +4 -1
  31. package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +101 -1
  32. package/runtime/__test__/element-template/runtime/template/element-template-handle.test.ts +32 -0
  33. package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +309 -26
  34. package/runtime/__test__/element-template/test-utils/debug/envManager.ts +8 -5
  35. package/runtime/__test__/element-template/test-utils/mock/mockNativePapi.ts +35 -2
  36. package/runtime/__test__/guardrails/snapshot-containment.test.ts +45 -11
  37. package/runtime/__test__/shared/component-stack.test.ts +211 -0
  38. package/runtime/__test__/snapshot/debug/formatPatch.test.ts +0 -8
  39. package/runtime/__test__/snapshot/gesture/prepareGestureForCommit.test.js +1 -1
  40. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.event.test.jsx +2 -2
  41. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.test.jsx +3 -3
  42. package/runtime/__test__/snapshot/lynx/timing.test.jsx +3 -0
  43. package/runtime/__test__/snapshot/preact.test.jsx +0 -3
  44. package/runtime/__test__/snapshot/ref.test.jsx +30 -0
  45. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +0 -33
  46. package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +12 -0
  47. package/runtime/__test__/worklet-runtime/loadRuntime.test.js +68 -0
  48. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +30 -0
  49. package/runtime/lazy/element-template-internal.js +1 -1
  50. package/runtime/lazy/internal.js +1 -1
  51. package/runtime/lazy/jsx-dev-runtime.js +1 -1
  52. package/runtime/lazy/jsx-runtime.js +1 -1
  53. package/runtime/lazy/legacy-react-runtime.js +1 -1
  54. package/runtime/lazy/react-lepus.js +1 -1
  55. package/runtime/lazy/react.js +1 -1
  56. package/runtime/lib/core/capture-main-thread-object.js +2 -1
  57. package/runtime/lib/core/capture-main-thread-object.js.map +1 -1
  58. package/runtime/lib/core/compat/legacy-react-runtime.js +1 -1
  59. package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
  60. package/runtime/lib/core/globalProps.js +1 -1
  61. package/runtime/lib/core/globalProps.js.map +1 -1
  62. package/runtime/lib/core/lynx/lazy-bundle.js +1 -1
  63. package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
  64. package/runtime/lib/core/lynx-data-processors.d.ts +125 -1
  65. package/runtime/lib/core/lynx-data-processors.js +3 -0
  66. package/runtime/lib/core/lynx-data-processors.js.map +1 -1
  67. package/runtime/lib/core/runtime-destroy.js +1 -1
  68. package/runtime/lib/core/runtime-destroy.js.map +1 -1
  69. package/runtime/lib/document.js +0 -5
  70. package/runtime/lib/document.js.map +1 -1
  71. package/runtime/lib/element-template/background/attr-slots.js +5 -2
  72. package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
  73. package/runtime/lib/element-template/background/commit-hook.js +11 -12
  74. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  75. package/runtime/lib/element-template/background/destroy.js +16 -11
  76. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  77. package/runtime/lib/element-template/background/hydrate.js +23 -0
  78. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  79. package/runtime/lib/element-template/client/root.d.ts +1 -1
  80. package/runtime/lib/element-template/client/root.js.map +1 -1
  81. package/runtime/lib/element-template/debug/alog.d.ts +1 -1
  82. package/runtime/lib/element-template/debug/alog.js +4 -1
  83. package/runtime/lib/element-template/debug/alog.js.map +1 -1
  84. package/runtime/lib/element-template/index.d.ts +2 -2
  85. package/runtime/lib/element-template/index.js.map +1 -1
  86. package/runtime/lib/element-template/internal.d.ts +2 -2
  87. package/runtime/lib/element-template/internal.js +1 -14
  88. package/runtime/lib/element-template/internal.js.map +1 -1
  89. package/runtime/lib/element-template/lynx/env.js.map +1 -1
  90. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js +6 -1
  91. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
  92. package/runtime/lib/element-template/native/flush.d.ts +1 -0
  93. package/runtime/lib/element-template/native/flush.js +9 -0
  94. package/runtime/lib/element-template/native/flush.js.map +1 -0
  95. package/runtime/lib/element-template/native/main-thread-api.js +3 -7
  96. package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
  97. package/runtime/lib/element-template/native/mts-destroy.js +8 -2
  98. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  99. package/runtime/lib/element-template/native/patch-listener.js +57 -50
  100. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  101. package/runtime/lib/element-template/native/reload-background.js +1 -1
  102. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  103. package/runtime/lib/element-template/native/reload-main-thread.js +35 -10
  104. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  105. package/runtime/lib/element-template/prop-adapters/gesture.d.ts +23 -0
  106. package/runtime/lib/element-template/prop-adapters/gesture.js +97 -0
  107. package/runtime/lib/element-template/prop-adapters/gesture.js.map +1 -0
  108. package/runtime/lib/element-template/prop-adapters/spread.d.ts +6 -1
  109. package/runtime/lib/element-template/prop-adapters/spread.js +72 -2
  110. package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
  111. package/runtime/lib/element-template/protocol/opcodes.d.ts +1 -0
  112. package/runtime/lib/element-template/protocol/opcodes.js +1 -0
  113. package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
  114. package/runtime/lib/element-template/protocol/types.d.ts +7 -1
  115. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  116. package/runtime/lib/element-template/runtime/page/root-instance.d.ts +0 -1
  117. package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
  118. package/runtime/lib/element-template/runtime/patch.js +31 -2
  119. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  120. package/runtime/lib/element-template/runtime/render/render-components.js +4 -4
  121. package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
  122. package/runtime/lib/element-template/runtime/render/render-main-thread.js +2 -0
  123. package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
  124. package/runtime/lib/element-template/runtime/template/attr-slot-plan.d.ts +4 -2
  125. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +17 -4
  126. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
  127. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +8 -1
  128. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  129. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +7 -1
  130. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +380 -50
  131. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  132. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +3 -2
  133. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js +10 -1
  134. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  135. package/runtime/lib/element-template/runtime/template/typed-attributes.js +3 -3
  136. package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
  137. package/runtime/lib/internal.d.ts +1 -1
  138. package/runtime/lib/internal.js +1 -1
  139. package/runtime/lib/internal.js.map +1 -1
  140. package/runtime/lib/lynx-api.d.ts +2 -125
  141. package/runtime/lib/lynx-api.js.map +1 -1
  142. package/runtime/lib/shared/component-stack.d.ts +0 -4
  143. package/runtime/lib/shared/component-stack.js +21 -5
  144. package/runtime/lib/shared/component-stack.js.map +1 -1
  145. package/runtime/lib/snapshot/gesture/{processGestureBagkround.js → processGestureBackground.js} +1 -1
  146. package/runtime/lib/snapshot/gesture/processGestureBackground.js.map +1 -0
  147. package/runtime/lib/snapshot/lifecycle/destroy.js +23 -17
  148. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  149. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.d.ts +0 -1
  150. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js +0 -5
  151. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
  152. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +0 -10
  153. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  154. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +56 -51
  155. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  156. package/runtime/lib/snapshot/lifecycle/ref/delay.d.ts +5 -0
  157. package/runtime/lib/snapshot/lifecycle/ref/delay.js +1 -1
  158. package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
  159. package/runtime/lib/snapshot/list/listUpdateInfo.js +0 -30
  160. package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
  161. package/runtime/lib/snapshot/lynx/calledByNative.js +11 -8
  162. package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
  163. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js +0 -52
  164. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
  165. package/runtime/lib/snapshot/renderToOpcodes/index.js +10 -10
  166. package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
  167. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js +0 -51
  168. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
  169. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js +1 -1
  170. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
  171. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  172. package/runtime/lib/worklet-runtime/bindings/index.d.ts +1 -0
  173. package/runtime/lib/worklet-runtime/bindings/index.js +1 -0
  174. package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
  175. package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +8 -0
  176. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +23 -0
  177. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +1 -0
  178. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.d.ts +2 -0
  179. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +6 -0
  180. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
  181. package/runtime/lib/worklet-runtime/workletRuntime.js +2 -0
  182. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  183. package/runtime/package.json +2 -3
  184. package/runtime/src/core/capture-main-thread-object.ts +2 -1
  185. package/runtime/src/core/compat/legacy-react-runtime.ts +1 -1
  186. package/runtime/src/core/globalProps.ts +1 -1
  187. package/runtime/src/core/lynx/lazy-bundle.ts +1 -1
  188. package/runtime/src/core/lynx-data-processors.ts +124 -1
  189. package/runtime/src/core/runtime-destroy.ts +1 -1
  190. package/runtime/src/document.ts +0 -6
  191. package/runtime/src/element-template/background/attr-slots.ts +5 -1
  192. package/runtime/src/element-template/background/commit-hook.ts +18 -12
  193. package/runtime/src/element-template/background/destroy.ts +16 -13
  194. package/runtime/src/element-template/background/hydrate.ts +23 -0
  195. package/runtime/src/element-template/client/root.ts +1 -3
  196. package/runtime/src/element-template/debug/alog.ts +5 -2
  197. package/runtime/src/element-template/index.ts +2 -2
  198. package/runtime/src/element-template/internal.ts +1 -20
  199. package/runtime/src/element-template/lynx/env.ts +1 -1
  200. package/runtime/src/element-template/native/callDestroyLifetimeFun.ts +5 -1
  201. package/runtime/src/element-template/native/flush.ts +10 -0
  202. package/runtime/src/element-template/native/main-thread-api.ts +3 -6
  203. package/runtime/src/element-template/native/mts-destroy.ts +7 -2
  204. package/runtime/src/element-template/native/patch-listener.ts +62 -57
  205. package/runtime/src/element-template/native/reload-background.ts +1 -1
  206. package/runtime/src/element-template/native/reload-main-thread.ts +32 -11
  207. package/runtime/src/element-template/prop-adapters/gesture.ts +144 -0
  208. package/runtime/src/element-template/prop-adapters/spread.ts +110 -2
  209. package/runtime/src/element-template/protocol/opcodes.ts +1 -0
  210. package/runtime/src/element-template/protocol/types.ts +8 -13
  211. package/runtime/src/element-template/runtime/page/root-instance.ts +0 -1
  212. package/runtime/src/element-template/runtime/patch.ts +38 -0
  213. package/runtime/src/element-template/runtime/render/render-components.ts +3 -3
  214. package/runtime/src/element-template/runtime/render/render-main-thread.ts +2 -0
  215. package/runtime/src/element-template/runtime/template/attr-slot-plan.ts +31 -4
  216. package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -1
  217. package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +456 -50
  218. package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +14 -4
  219. package/runtime/src/element-template/runtime/template/typed-attributes.ts +3 -3
  220. package/runtime/src/element-template/types.d.ts +6 -0
  221. package/runtime/src/internal.ts +1 -1
  222. package/runtime/src/lynx-api.ts +2 -124
  223. package/runtime/src/shared/component-stack.ts +22 -5
  224. package/runtime/src/snapshot/lifecycle/destroy.ts +23 -18
  225. package/runtime/src/snapshot/lifecycle/patch/snapshotPatch.ts +0 -5
  226. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +0 -10
  227. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +62 -58
  228. package/runtime/src/snapshot/lifecycle/ref/delay.ts +1 -1
  229. package/runtime/src/snapshot/list/listUpdateInfo.ts +0 -52
  230. package/runtime/src/snapshot/lynx/calledByNative.ts +11 -9
  231. package/runtime/src/snapshot/renderToOpcodes/hydrate.ts +0 -58
  232. package/runtime/src/snapshot/renderToOpcodes/index.ts +8 -8
  233. package/runtime/src/snapshot/renderToOpcodes/opcodes.ts +0 -57
  234. package/runtime/src/snapshot/snapshot/backgroundSnapshot.ts +1 -1
  235. package/runtime/src/worklet-runtime/README.md +30 -10
  236. package/runtime/src/worklet-runtime/bindings/index.ts +2 -0
  237. package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +25 -0
  238. package/runtime/src/worklet-runtime/jsFunctionLifecycle.ts +6 -0
  239. package/runtime/src/worklet-runtime/workletRuntime.ts +2 -0
  240. package/runtime/types/types.d.ts +2 -1
  241. package/runtime/vitest.config.ts +0 -2
  242. package/testing-library/.turbo/turbo-build.log +7 -7
  243. package/testing-library/src/__tests__/act.test.jsx +0 -7
  244. package/testing-library/src/__tests__/alog.test.jsx +12 -54
  245. package/testing-library/src/__tests__/end-to-end.test.jsx +0 -7
  246. package/testing-library/src/__tests__/renderComponent.test.jsx +0 -14
  247. package/testing-library/src/__tests__/worklet.test.jsx +49 -0
  248. package/transform/.turbo/turbo-build$colon$wasm.log +133 -297
  249. package/transform/__test__/defines.spec.js +30 -0
  250. package/transform/__test__/fixture.spec.js +11 -11
  251. package/transform/__test__/main-thread-object-compat.spec.js +111 -0
  252. package/transform/__test__/object-methods.spec.js +79 -0
  253. package/transform/crates/swc_plugin_element_template/lib.rs +11 -0
  254. package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +113 -0
  255. package/transform/crates/swc_plugin_worklet/extract_ident.rs +99 -49
  256. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +63 -57
  257. package/transform/crates/swc_plugin_worklet/lib.rs +393 -589
  258. package/transform/crates/swc_plugin_worklet/napi.rs +4 -6
  259. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +3 -3
  260. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +3 -3
  261. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +11 -6
  262. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +12 -6
  263. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +14 -5
  264. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +17 -6
  265. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +5 -5
  266. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +5 -5
  267. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +1 -1
  268. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +1 -1
  269. package/transform/dist/wasm.cjs +1 -1
  270. package/transform/package.json +0 -1
  271. package/transform/src/lib.rs +1 -1
  272. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +167 -69
  273. package/transform/swc-plugin-reactlynx/src/lib.rs +1 -4
  274. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  275. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +226 -132
  276. package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +0 -1
  277. package/runtime/lib/snapshot/worklet/hmr.d.ts +0 -1
  278. package/runtime/lib/snapshot/worklet/hmr.js +0 -28
  279. package/runtime/lib/snapshot/worklet/hmr.js.map +0 -1
  280. package/runtime/src/snapshot/worklet/hmr.ts +0 -34
  281. /package/runtime/lib/snapshot/gesture/{processGestureBagkround.d.ts → processGestureBackground.d.ts} +0 -0
  282. /package/runtime/src/snapshot/gesture/{processGestureBagkround.ts → processGestureBackground.ts} +0 -0
@@ -8,6 +8,7 @@ import {
8
8
  } from '../../../../src/element-template/main-thread-ref-init-value.js';
9
9
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
10
10
 
11
+ import { JsFunctionLifecycleManager } from '../../../../src/worklet-runtime/jsFunctionLifecycle.js';
11
12
  import { getReloadVersion, increaseReloadVersion } from '../../../../src/core/reload-version.js';
12
13
  import type { BackgroundElementTemplateInstance } from '../../../../src/element-template/background/instance.js';
13
14
  import { root } from '../../../../src/element-template/index.js';
@@ -41,6 +42,7 @@ import {
41
42
  } from '../../../../src/element-template/runtime/list/list.js';
42
43
  import {
43
44
  attachMainThreadDynamicAttrRefsForSubtree,
45
+ applyPendingMainThreadRefAttachments,
44
46
  clearMainThreadDynamicAttrState,
45
47
  detachMainThreadDynamicAttrRefsForSubtree,
46
48
  getMainThreadDynamicAttrState,
@@ -113,6 +115,7 @@ function createMaterializedListForFrontendTest(nativeId: number): FiberElement {
113
115
 
114
116
  const MT_EVENT_TEMPLATE = '_et_mt_event';
115
117
  const MT_REF_TEMPLATE = '_et_mt_ref';
118
+ const REF_TARGET = { id: 'materialized-ref-node' } as unknown as ElementRef;
116
119
 
117
120
  function registerMTEventSlotsForTemplate(templateType: string, ...slotIndexes: number[]): void {
118
121
  __etAttrPlanMap[templateType] = slotIndexes.flatMap(slotIndex => [
@@ -158,6 +161,8 @@ function seedMTRefState(
158
161
  attributeSlots[attrSlotIndex] = { type: 'main-thread-ref', value };
159
162
  initializeMainThreadDynamicAttrSlots(handleId, MT_REF_TEMPLATE, attributeSlots);
160
163
  attachMainThreadDynamicAttrRefsForSubtree([{ uid: handleId, ref: nativeRef }]);
164
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(REF_TARGET);
165
+ applyPendingMainThreadRefAttachments();
161
166
  }
162
167
 
163
168
  function installWorkletRefRuntime(): {
@@ -339,6 +344,43 @@ describe('ElementTemplate patch stream (apply)', () => {
339
344
  expect(mockFlushElementTree.mock.calls.length).toBeGreaterThan(0);
340
345
  });
341
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
+
342
384
  it('records MTEvent state after setMainThreadEvent PAPI succeeds', () => {
343
385
  const targetId = 101;
344
386
  const nativeRef = {};
@@ -606,7 +648,11 @@ describe('ElementTemplate patch stream (apply)', () => {
606
648
  ]);
607
649
 
608
650
  expect(mockInsertElementTemplateNodeIntoSlot).toHaveBeenCalledWith(parentRef, 0, nativeRef, null);
609
- 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);
610
656
  expect(getMainThreadDynamicAttrState(handleId, 0)).toEqual({
611
657
  kind: 'mt-ref',
612
658
  value: ref,
@@ -761,19 +807,19 @@ describe('ElementTemplate patch stream (apply)', () => {
761
807
 
762
808
  expect(mockSetElementTemplateAttributeSlot).not.toHaveBeenCalled();
763
809
  expect(oldCleanup).toHaveBeenCalledTimes(1);
764
- const mountedCallback = runWorklet.mock.calls[0]?.[0] as typeof nextCallback & {
765
- _unmount?: () => void;
766
- };
810
+ const mountedCallback = runWorklet.mock.calls[0]?.[0] as typeof nextCallback;
767
811
  expect(mountedCallback).toEqual(expect.objectContaining({
768
812
  _wkltId: 'next-ref-callback',
769
813
  }));
770
- expect(runWorklet.mock.calls[0]?.[1]).toEqual([{ elementRefptr: nativeRef }]);
771
- expect(mountedCallback._unmount).toBe(nextCleanup);
814
+ expect(runWorklet.mock.calls[0]?.[1]).toEqual([{ elementRefptr: REF_TARGET }]);
815
+ expect(mountedCallback).toHaveProperty('_unmount', nextCleanup);
772
816
  expect(nextCallback).not.toHaveProperty('_unmount');
773
817
  expect(getMainThreadDynamicAttrState(targetId, 0)).toEqual({
774
818
  kind: 'mt-ref',
775
819
  value: mountedCallback,
776
820
  });
821
+ applyElementTemplateUpdateCommands([ElementTemplateUpdateOps.setMainThreadRef, targetId, 0, null]);
822
+ expect(nextCleanup).toHaveBeenCalledOnce();
777
823
  } finally {
778
824
  globalThis.runWorklet = previousRunWorklet;
779
825
  }
@@ -808,10 +854,11 @@ describe('ElementTemplate patch stream (apply)', () => {
808
854
  { type: 'main-thread-ref', value: nextCallback },
809
855
  ], true);
810
856
 
857
+ applyPendingMainThreadRefAttachments();
811
858
  expect(hydrateCtx).toHaveBeenCalledWith(nextCallback, oldCallback);
812
859
  expect(runDelayedWorklet).not.toHaveBeenCalled();
813
860
  expect(oldCleanup).toHaveBeenCalledTimes(1);
814
- expect(globalThis.runWorklet).toHaveBeenCalledWith(nextCallback, [{ elementRefptr: nativeRef }]);
861
+ expect(globalThis.runWorklet).toHaveBeenCalledWith(nextCallback, [{ elementRefptr: REF_TARGET }]);
815
862
  } finally {
816
863
  globalThis.runWorklet = previousRunWorklet;
817
864
  globalThis.lynxWorkletImpl = previousWorkletImpl;
@@ -845,6 +892,7 @@ describe('ElementTemplate patch stream (apply)', () => {
845
892
  { type: 'main-thread-ref', value: nextCallback },
846
893
  ], true);
847
894
 
895
+ applyPendingMainThreadRefAttachments();
848
896
  expect(hydrateCtx).toHaveBeenCalledWith(nextCallback, oldCallback);
849
897
  expect(globalThis.runWorklet).not.toHaveBeenCalled();
850
898
  expect(getMainThreadDynamicAttrState(targetId, 0)).toEqual({
@@ -857,6 +905,21 @@ describe('ElementTemplate patch stream (apply)', () => {
857
905
  }
858
906
  });
859
907
 
908
+ it('does not send a Gesture update to Native when the target is missing', () => {
909
+ envManager.switchToMainThread();
910
+ installElementTemplatePatchListener();
911
+ envManager.switchToBackground();
912
+ dispatchElementTemplateUpdate({
913
+ ops: [ElementTemplateUpdateOps.setGesture, 124, 0, null],
914
+ flushOptions: {},
915
+ });
916
+ envManager.switchToMainThread();
917
+ expect(mockSetElementTemplateAttributeSlot).not.toHaveBeenCalled();
918
+ const reportError = (globalThis.lynx as unknown as LynxWithReportErrorMock).reportError;
919
+ expect(String(reportError.mock.calls[0]?.[0]?.message ?? '')).toContain('target handle 124 not found');
920
+ resetReportedErrors();
921
+ });
922
+
860
923
  it('does not update MTRef state when the target is missing', () => {
861
924
  const targetId = 124;
862
925
  const ref = { _wvid: 9 };
@@ -1392,6 +1455,55 @@ describe('ElementTemplate patch stream (apply)', () => {
1392
1455
  }
1393
1456
  });
1394
1457
 
1458
+ it('cleans hydration handoff and propagates a ref callback error without running later work', () => {
1459
+ const handle = { id: 'throwing-ref-template' } as unknown as ElementTemplateHandle;
1460
+ const target = { id: 'inner-node' } as unknown as ElementRef;
1461
+ const callback = { _wkltId: 'throwing-ref' };
1462
+ const callbackError = new Error('ref callback failed');
1463
+ const runDelayedBackgroundFunctions = vi.fn();
1464
+ const clearFirstScreenWorkletRefMap = vi.fn();
1465
+ const runRunOnMainThreadTask = vi.fn();
1466
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
1467
+ const previousRunWorklet = globalThis.runWorklet;
1468
+ globalThis.lynxWorkletImpl = {
1469
+ ...previousWorkletImpl,
1470
+ _refImpl: { ...createMainThreadRefImplMock(), clearFirstScreenWorkletRefMap },
1471
+ _runOnBackgroundDelayImpl: { runDelayedBackgroundFunctions },
1472
+ _runRunOnMainThreadTask: runRunOnMainThreadTask,
1473
+ };
1474
+ globalThis.runWorklet = vi.fn(() => {
1475
+ throw callbackError;
1476
+ });
1477
+ try {
1478
+ registerMTRefSlots(0);
1479
+ initializeMainThreadDynamicAttrSlots(193, MT_REF_TEMPLATE, [{ type: 'main-thread-ref', value: callback }]);
1480
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 193, ref: handle }]);
1481
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
1482
+ envManager.switchToMainThread();
1483
+ installElementTemplatePatchListener();
1484
+ mockFlushElementTree.mockClear();
1485
+ expect(() => {
1486
+ envManager.switchToBackground();
1487
+ dispatchElementTemplateUpdate({
1488
+ ops: [],
1489
+ isHydration: true,
1490
+ flushOptions: {},
1491
+ delayedRunOnMainThreadData: [{ worklet: { _wkltId: 'should-not-run' }, params: [], resolveId: 33 }],
1492
+ });
1493
+ envManager.switchToMainThread();
1494
+ }).toThrow(callbackError);
1495
+ expect(globalThis.runWorklet).toHaveBeenCalledWith(callback, [{ elementRefptr: target }]);
1496
+ expect(runDelayedBackgroundFunctions).toHaveBeenCalledTimes(1);
1497
+ expect(clearFirstScreenWorkletRefMap).toHaveBeenCalledTimes(1);
1498
+ expect(runRunOnMainThreadTask).not.toHaveBeenCalled();
1499
+ expect(mockFlushElementTree).not.toHaveBeenCalled();
1500
+ } finally {
1501
+ globalThis.runWorklet = previousRunWorklet;
1502
+ clearMainThreadDynamicAttrState();
1503
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
1504
+ }
1505
+ });
1506
+
1395
1507
  it('continues delayed lifecycle when patch apply reports a missing target', () => {
1396
1508
  const runDelayedBackgroundFunctions = vi.fn();
1397
1509
  const runRunOnMainThreadTask = vi.fn();
@@ -1443,6 +1555,99 @@ describe('ElementTemplate patch stream (apply)', () => {
1443
1555
  }
1444
1556
  });
1445
1557
 
1558
+ it('delivers pending targets before hydration handoff and delayed tasks, then flushes their writes', () => {
1559
+ const callOrder: string[] = [];
1560
+ const handle = { id: 'pending-template' } as unknown as ElementTemplateHandle;
1561
+ const target = { id: 'inner-node' } as unknown as ElementRef;
1562
+ const ref = { _wvid: 91 };
1563
+ const worklet = { _wkltId: 'uses-hydrated-ref' };
1564
+ const updateWorkletRef = vi.fn(() => callOrder.push('attach'));
1565
+ const clearFirstScreenWorkletRefMap = vi.fn(() => callOrder.push('clear-first-screen'));
1566
+ const runDelayedBackgroundFunctions = vi.fn(() => callOrder.push('background-handoff'));
1567
+ const runRunOnMainThreadTask = vi.fn(() => {
1568
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, target);
1569
+ callOrder.push('task');
1570
+ });
1571
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
1572
+ globalThis.lynxWorkletImpl = {
1573
+ ...previousWorkletImpl,
1574
+ _refImpl: { updateWorkletRef, clearFirstScreenWorkletRefMap },
1575
+ _runOnBackgroundDelayImpl: { runDelayedBackgroundFunctions },
1576
+ _eomImpl: { setShouldFlush: vi.fn() },
1577
+ _runRunOnMainThreadTask: runRunOnMainThreadTask,
1578
+ };
1579
+ try {
1580
+ registerMTRefSlots(0);
1581
+ initializeMainThreadDynamicAttrSlots(191, MT_REF_TEMPLATE, [{ type: 'main-thread-ref', value: ref }]);
1582
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 191, ref: handle }]);
1583
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
1584
+ mockFlushElementTree.mockImplementationOnce(() => callOrder.push('initial-flush'));
1585
+ envManager.switchToMainThread();
1586
+ installElementTemplatePatchListener();
1587
+ envManager.switchToBackground();
1588
+ dispatchElementTemplateUpdate({
1589
+ ops: [],
1590
+ isHydration: true,
1591
+ flushOptions: { emptyPatch: true },
1592
+ delayedRunOnMainThreadData: [{ worklet, params: [], resolveId: 31 }],
1593
+ });
1594
+ envManager.switchToMainThread();
1595
+ expect(callOrder).toEqual(['attach', 'background-handoff', 'clear-first-screen', 'task', 'initial-flush']);
1596
+ expect(runRunOnMainThreadTask).toHaveBeenCalledWith(worklet, [], 31);
1597
+ expect(mockFlushElementTree).toHaveBeenCalledOnce();
1598
+ expect(mockFlushElementTree.mock.calls[0]![1]).toEqual({ emptyPatch: true });
1599
+ } finally {
1600
+ clearMainThreadDynamicAttrState();
1601
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
1602
+ }
1603
+ });
1604
+
1605
+ it('delivers refs synchronously before a later delayed-task payload', () => {
1606
+ const handleId = 192;
1607
+ const handle = { id: 'pending-template' } as unknown as ElementTemplateHandle;
1608
+ const target = { id: 'inner-node' } as unknown as ElementRef;
1609
+ const ref = { _wvid: 92 };
1610
+ const worklet = { _wkltId: 'later-payload-uses-ref' };
1611
+ const updateWorkletRef = vi.fn();
1612
+ const runRunOnMainThreadTask = vi.fn(() => expect(updateWorkletRef).toHaveBeenCalledWith(ref, target));
1613
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
1614
+ globalThis.lynxWorkletImpl = {
1615
+ ...previousWorkletImpl,
1616
+ _refImpl: { updateWorkletRef },
1617
+ _eomImpl: { setShouldFlush: vi.fn() },
1618
+ _runRunOnMainThreadTask: runRunOnMainThreadTask,
1619
+ };
1620
+ try {
1621
+ elementTemplateRegistry.set(handleId, handle);
1622
+ registerMTRefSlots(0);
1623
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: handleId, ref: handle }]);
1624
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(target);
1625
+ envManager.switchToMainThread();
1626
+ installElementTemplatePatchListener();
1627
+ envManager.switchToBackground();
1628
+ dispatchElementTemplateUpdate({
1629
+ ops: [ElementTemplateUpdateOps.setMainThreadRef, handleId, 0, { type: 'main-thread-ref', value: ref }],
1630
+ flushOptions: {},
1631
+ });
1632
+ envManager.switchToMainThread();
1633
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, target);
1634
+ envManager.switchToBackground();
1635
+ dispatchElementTemplateUpdate({
1636
+ ops: [],
1637
+ flushOptions: { emptyPatch: true },
1638
+ delayedRunOnMainThreadData: [{ worklet, params: [], resolveId: 32 }],
1639
+ });
1640
+ envManager.switchToMainThread();
1641
+ expect(runRunOnMainThreadTask).toHaveBeenCalledWith(worklet, [], 32);
1642
+ expect(__GetElementByAttrSlot).toHaveBeenCalledOnce();
1643
+ expect(updateWorkletRef).toHaveBeenCalledOnce();
1644
+ expect(mockFlushElementTree.mock.calls).toEqual([[undefined, {}], [undefined, { emptyPatch: true }]]);
1645
+ } finally {
1646
+ clearMainThreadDynamicAttrState();
1647
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
1648
+ }
1649
+ });
1650
+
1446
1651
  it('applies independent ref initialization before delayed main-thread tasks', () => {
1447
1652
  const callOrder: string[] = [];
1448
1653
  const updateWorkletRefInitValueChanges = vi.fn(() => {
@@ -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', () => {
@@ -0,0 +1,200 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import path from 'node:path';
6
+ import { fileURLToPath } from 'node:url';
7
+
8
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
9
+
10
+ import {
11
+ installElementTemplateCommitHook,
12
+ resetElementTemplateCommitState,
13
+ } from '../../../../src/element-template/background/commit-hook.js';
14
+ import {
15
+ installElementTemplateHydrationListener,
16
+ resetElementTemplateHydrationListener,
17
+ } from '../../../../src/element-template/background/hydration-listener.js';
18
+ import {
19
+ installElementTemplatePatchListener,
20
+ resetElementTemplatePatchListener,
21
+ } from '../../../../src/element-template/native/patch-listener.js';
22
+ import type { GestureSlotValue } from '../../../../src/element-template/prop-adapters/gesture.js';
23
+ import {
24
+ installElementTemplateRefInitValueListener,
25
+ resetElementTemplateRefInitValueListener,
26
+ } from '../../../../src/element-template/main-thread-ref-init-value.js';
27
+ import { ElementTemplateLifecycleConstant } from '../../../../src/element-template/protocol/lifecycle-constant.js';
28
+ import { ElementTemplateUpdateOps } from '../../../../src/element-template/protocol/opcodes.js';
29
+ import type { ElementTemplateUpdateCommitContext } from '../../../../src/element-template/protocol/types.js';
30
+ import { parseElementTemplateUpdateEventPayload } from '../../../../src/element-template/protocol/update-event.js';
31
+ import { clearEtAttrPlanMap } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
32
+ import {
33
+ resetElementTemplateBackgroundFunctionRuntime,
34
+ resetElementTemplateMainThreadBackgroundFunctionRuntime,
35
+ } from '../../../../src/element-template/runtime/template/main-thread-background-function.js';
36
+ import { clearMainThreadDynamicAttrState } from '../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
37
+ import { initWorklet } from '../../../../src/worklet-runtime/workletRuntime.js';
38
+ import type { Worklet } from '../../../../src/worklet-runtime/bindings/types.js';
39
+ import { loadCompiledFixturePair } from '../../test-utils/debug/compiledFixtureModule.js';
40
+ import {
41
+ renderCompiledFixtureOnBackground,
42
+ renderCompiledFixtureOnMainThread,
43
+ } from '../../test-utils/debug/compiledThreadRunner.js';
44
+ import { ElementTemplateEnvManager } from '../../test-utils/debug/envManager.js';
45
+ import { lastMock } from '../../test-utils/mock/mockNativePapi.js';
46
+
47
+ const fixturePath = path.resolve(
48
+ path.dirname(fileURLToPath(import.meta.url)),
49
+ '../../fixtures/gesture/direct/index.tsx',
50
+ );
51
+ interface GestureProps {
52
+ label?: string;
53
+ enabled?: boolean;
54
+ show?: boolean;
55
+ onReport?: (label: string, count: number) => string;
56
+ }
57
+ interface FixtureModule {
58
+ App: (props: GestureProps) => JSX.Element;
59
+ }
60
+
61
+ function firstScreenGesture(): GestureSlotValue {
62
+ for (const entry of lastMock!.nativeLog) {
63
+ if (Array.isArray(entry) && entry[0] === '__CreateElementTemplate' && Array.isArray(entry[3])) {
64
+ const gesture = entry[3].find((value: unknown) =>
65
+ value != null && typeof value === 'object' && (value as GestureSlotValue).type === 'gesture'
66
+ );
67
+ if (gesture) return gesture as GestureSlotValue;
68
+ }
69
+ }
70
+ throw new Error('No compiled first-screen Gesture slot was created.');
71
+ }
72
+
73
+ function latestNativeGesture(): GestureSlotValue | null {
74
+ const entry = lastMock!.nativeLog.findLast(value =>
75
+ Array.isArray(value) && value[0] === '__SetAttributeOfElementTemplate'
76
+ ) as unknown[] | undefined;
77
+ if (!entry) throw new Error('No Gesture attribute update reached Native.');
78
+ return entry[3] as GestureSlotValue | null;
79
+ }
80
+
81
+ function callback(value: GestureSlotValue): Worklet {
82
+ expect(value.type).toBe('gesture');
83
+ const item = value.value[0]!.callbacks.find(item => item.name === 'onUpdate');
84
+ expect(item).toBeDefined();
85
+ return item!.callback as Worklet;
86
+ }
87
+
88
+ describe('Compiled ET direct Gesture worklet lifecycle', () => {
89
+ const env = new ElementTemplateEnvManager();
90
+ let sdk: string | undefined;
91
+ let updates: ElementTemplateUpdateCommitContext[];
92
+ const onUpdate = (event: { data: unknown }) => updates.push(parseElementTemplateUpdateEventPayload(event.data));
93
+
94
+ beforeEach(() => {
95
+ vi.clearAllMocks();
96
+ sdk = SystemInfo.lynxSdkVersion;
97
+ SystemInfo.lynxSdkVersion = '4.0';
98
+ updates = [];
99
+ resetElementTemplateCommitState();
100
+ clearEtAttrPlanMap();
101
+ clearMainThreadDynamicAttrState();
102
+ resetElementTemplateBackgroundFunctionRuntime();
103
+ env.resetEnv('background');
104
+ env.setUseElementTemplate(true);
105
+ installElementTemplateCommitHook();
106
+ installElementTemplateHydrationListener();
107
+ env.switchToMainThread();
108
+ initWorklet();
109
+ installElementTemplateRefInitValueListener();
110
+ installElementTemplatePatchListener();
111
+ lynx.getJSContext().addEventListener(ElementTemplateLifecycleConstant.update, onUpdate);
112
+ env.switchToBackground();
113
+ vi.stubGlobal('__LoadLepusChunk', vi.fn(() => true));
114
+ });
115
+
116
+ afterEach(() => {
117
+ env.switchToMainThread();
118
+ lynx.getJSContext().removeEventListener(ElementTemplateLifecycleConstant.update, onUpdate);
119
+ resetElementTemplatePatchListener();
120
+ resetElementTemplateRefInitValueListener();
121
+ clearMainThreadDynamicAttrState();
122
+ resetElementTemplateMainThreadBackgroundFunctionRuntime();
123
+ env.switchToBackground();
124
+ resetElementTemplateHydrationListener();
125
+ resetElementTemplateBackgroundFunctionRuntime();
126
+ env.setUseElementTemplate(false);
127
+ SystemInfo.lynxSdkVersion = sdk;
128
+ delete globalThis.lynxWorkletImpl;
129
+ // @ts-expect-error Restore globals installed by the real worklet runtime.
130
+ delete globalThis.registerWorklet;
131
+ // @ts-expect-error Restore globals installed by the real worklet runtime.
132
+ delete globalThis.registerWorkletInternal;
133
+ // @ts-expect-error Restore globals installed by the real worklet runtime.
134
+ delete globalThis.runWorklet;
135
+ vi.unstubAllGlobals();
136
+ });
137
+
138
+ it('replays pre-hydrate background calls and preserves MainThreadRef mutations in the hydrated callback', async () => {
139
+ const { backgroundModule, mainModule } = await loadCompiledFixturePair<FixtureModule>(fixturePath, {
140
+ enableWorkletTransform: true,
141
+ envManager: env,
142
+ });
143
+ const onReport = vi.fn((label: string, count: number) => `${label}:${count}`);
144
+ renderCompiledFixtureOnBackground(backgroundModule, env, { label: 'first', onReport });
145
+ renderCompiledFixtureOnMainThread(mainModule, env, { label: 'first', onReport });
146
+ const first = callback(firstScreenGesture());
147
+ let delayed: Promise<unknown>;
148
+ env.switchToMainThread(() => {
149
+ delayed = globalThis.runWorklet(first, []) as Promise<unknown>;
150
+ expect(onReport).not.toHaveBeenCalled();
151
+ expect(globalThis.lynxWorkletImpl._runOnBackgroundDelayImpl.delayedBackgroundFunctionArray).toHaveLength(1);
152
+ });
153
+ expect(updates.at(-1)?.isHydration).toBe(true);
154
+ const hydrated = callback(latestNativeGesture()!);
155
+ expect(hydrated).not.toBe(first);
156
+ expect(globalThis.lynxWorkletImpl._runOnBackgroundDelayImpl.delayedBackgroundFunctionArray).toHaveLength(0);
157
+ env.switchToBackground();
158
+ expect(onReport).toHaveBeenCalledWith('first', 1);
159
+ env.switchToMainThread();
160
+ await expect(delayed!).resolves.toBe('first:1');
161
+ const next = globalThis.runWorklet(hydrated, []) as Promise<unknown>;
162
+ env.switchToBackground();
163
+ expect(onReport).toHaveBeenLastCalledWith('first', 2);
164
+ env.switchToMainThread();
165
+ await expect(next).resolves.toBe('first:2');
166
+ });
167
+
168
+ it('uses the latest closure and propagates clearing a Gesture to Native', async () => {
169
+ const { backgroundModule, mainModule } = await loadCompiledFixturePair<FixtureModule>(fixturePath, {
170
+ enableWorkletTransform: true,
171
+ envManager: env,
172
+ });
173
+ const firstReport = vi.fn((label: string) => label);
174
+ const nextReport = vi.fn((label: string) => label);
175
+ const host = renderCompiledFixtureOnBackground(backgroundModule, env, { label: 'first', onReport: firstReport });
176
+ renderCompiledFixtureOnMainThread(mainModule, env, { label: 'first', onReport: firstReport });
177
+ env.switchToMainThread();
178
+ env.switchToBackground();
179
+ updates = [];
180
+ renderCompiledFixtureOnBackground(backgroundModule, env, { label: 'second', onReport: nextReport });
181
+ env.switchToMainThread();
182
+ expect(updates.at(-1)?.ops).toEqual([
183
+ ElementTemplateUpdateOps.setGesture,
184
+ host!.instanceId,
185
+ 0,
186
+ expect.objectContaining({ type: 'gesture' }),
187
+ ]);
188
+ const result = globalThis.runWorklet(callback(latestNativeGesture()!), []) as Promise<unknown>;
189
+ env.switchToBackground();
190
+ expect(nextReport).toHaveBeenCalledWith('second', 1);
191
+ expect(firstReport).not.toHaveBeenCalled();
192
+ env.switchToMainThread();
193
+ await expect(result).resolves.toBe('second');
194
+ env.switchToBackground();
195
+ renderCompiledFixtureOnBackground(backgroundModule, env, { enabled: false, onReport: nextReport });
196
+ env.switchToMainThread();
197
+ expect(latestNativeGesture()).toBeNull();
198
+ expect(updates.at(-1)?.ops).toEqual([ElementTemplateUpdateOps.setGesture, host!.instanceId, 0, null]);
199
+ });
200
+ });