@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
@@ -2,28 +2,51 @@
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
- import { afterEach, describe, expect, it, vi } from 'vitest';
5
+ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
6
 
7
+ import { flushElementTemplate } from '../../../../src/element-template/native/flush.js';
7
8
  import {
8
9
  __etAttrPlanMap,
9
10
  adaptMTEventAttrSlot,
10
11
  adaptMTRefAttrSlot,
12
+ adaptSpreadAttrSlot,
11
13
  clearEtAttrPlanMap,
12
14
  } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
13
15
  import {
14
16
  attachMainThreadDynamicAttrRefsForSubtree,
17
+ applyPendingMainThreadRefAttachments,
15
18
  clearMainThreadDynamicAttrState,
16
19
  deleteMainThreadDynamicAttrStateForSubtree,
17
20
  detachMainThreadDynamicAttrRefsForSubtree,
18
21
  getMainThreadDynamicAttrState,
19
22
  initializeMainThreadDynamicAttrSlots,
23
+ isSpreadDynamicAttrSlot,
24
+ prepareMainThreadDynamicAttrSlotsForNative,
25
+ prepareSpreadAttrSlotForNative,
20
26
  updateMainThreadEventAttrSlot as setMTEventSlot,
21
27
  updateMainThreadRefAttrSlot as setMTRefSlotImpl,
28
+ updateSpreadAttrSlot,
22
29
  } from '../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
23
30
 
31
+ import { retainMainThreadBackgroundFunctionCtx } from '../../../../src/element-template/runtime/template/main-thread-background-function.js';
32
+ import { prepareMTEventCtxForNative } from '../../../../src/element-template/runtime/template/main-thread-event-ctx.js';
33
+ import type { MTEventNativeWrapper } from '../../../../src/element-template/runtime/template/main-thread-event-ctx.js';
34
+ import type { Worklet } from '../../../../src/worklet-runtime/bindings/types.js';
35
+ import { initWorklet } from '../../../../src/worklet-runtime/workletRuntime.js';
36
+
24
37
  const MT_EVENT_TEMPLATE = '_et_mt_event';
25
38
  const MT_REF_TEMPLATE = '_et_mt_ref';
26
- const TEST_NATIVE_REF = { id: 'dynamic-attr-target' } as unknown as ElementTemplateHandle;
39
+ const TEST_NATIVE_REF = { id: 'dynamic-attr-template' } as unknown as ElementTemplateHandle;
40
+ const TEST_ELEMENT_REF = { id: 'dynamic-attr-element' } as unknown as ElementRef;
41
+
42
+ function completeRefAttachments(): void {
43
+ applyPendingMainThreadRefAttachments();
44
+ }
45
+
46
+ function attachAndComplete(handles: Parameters<typeof attachMainThreadDynamicAttrRefsForSubtree>[0]): void {
47
+ attachMainThreadDynamicAttrRefsForSubtree(handles);
48
+ completeRefAttachments();
49
+ }
27
50
 
28
51
  function setMTRefSlot(
29
52
  handleId: number,
@@ -33,6 +56,7 @@ function setMTRefSlot(
33
56
  nativeRef: ElementTemplateHandle = TEST_NATIVE_REF,
34
57
  ): void {
35
58
  setMTRefSlotImpl(handleId, attrSlotIndex, value, nativeRef, isHydration);
59
+ completeRefAttachments();
36
60
  }
37
61
 
38
62
  function registerMTRefSlots(handleId: number, ...slotIndexes: number[]): void {
@@ -53,6 +77,9 @@ function installJsFunctionLifecycleManager(): {
53
77
  ...previousWorkletImpl,
54
78
  _jsFunctionLifecycleManager: {
55
79
  addRef,
80
+ retainWorkletCtx: (ctx: { _execId?: number }) => {
81
+ if (ctx._execId !== undefined) addRef(ctx._execId, ctx);
82
+ },
56
83
  },
57
84
  } as typeof globalThis.lynxWorkletImpl;
58
85
  return {
@@ -84,11 +111,81 @@ function installRefRuntime(): {
84
111
  }
85
112
 
86
113
  describe('main-thread dynamic attr state', () => {
114
+ beforeEach(() => {
115
+ vi.stubGlobal('__GetElementByAttrSlot', vi.fn(() => TEST_ELEMENT_REF));
116
+ });
87
117
  afterEach(() => {
88
118
  clearMainThreadDynamicAttrState();
89
119
  clearEtAttrPlanMap();
90
120
  });
91
121
 
122
+ it('executes the original Native-held event context before and after hydration', () => {
123
+ const previousWorkletImpl = globalThis.lynxWorkletImpl;
124
+ const previousSdkVersion = SystemInfo.lynxSdkVersion;
125
+ try {
126
+ SystemInfo.lynxSdkVersion = '4.0';
127
+ vi.stubGlobal('__JS__', false);
128
+ initWorklet();
129
+ globalThis.registerWorklet('main-thread', 'callback', function(this: Worklet) {
130
+ return this._jsFn;
131
+ });
132
+ const first = prepareMTEventCtxForNative({
133
+ _wkltId: 'callback',
134
+ _jsFn: { cb: { _isFirstScreen: true } },
135
+ }) as unknown as MTEventNativeWrapper;
136
+ retainMainThreadBackgroundFunctionCtx(first.value);
137
+ setMTEventSlot(17, 0, first);
138
+ const deliver = (ctx: object) => globalThis.runWorklet(ctx, []);
139
+ expect(deliver(first.value)).toBe(first.value._jsFn);
140
+ const next = { _wkltId: 'callback', _execId: 11, _jsFn: { cb: { _jsFnId: 1 } } };
141
+ retainMainThreadBackgroundFunctionCtx(next);
142
+ setMTEventSlot(17, 0, { type: 'worklet', value: next }, true);
143
+ expect(deliver(next)).toBe(next._jsFn);
144
+ expect(next._jsFn.cb).toEqual({ _jsFnId: 1, _execId: 11 });
145
+ expect(getMainThreadDynamicAttrState(17, 0)).toEqual({ kind: 'mt-event', nativeHeldValue: next });
146
+ deleteMainThreadDynamicAttrStateForSubtree([17]);
147
+ expect(getMainThreadDynamicAttrState(17, 0)).toBeUndefined();
148
+ } finally {
149
+ globalThis.lynxWorkletImpl = previousWorkletImpl;
150
+ SystemInfo.lynxSdkVersion = previousSdkVersion;
151
+ vi.unstubAllGlobals();
152
+ }
153
+ });
154
+
155
+ it('resolves distinct inner nodes before native flush without a layout hook', () => {
156
+ const { updateWorkletRef, restore } = installRefRuntime();
157
+ const flush = vi.fn();
158
+ vi.stubGlobal('__FlushElementTree', flush);
159
+ const firstRef = { _wvid: 7 };
160
+ const secondRef = { _wvid: 8 };
161
+ const secondElement = { id: 'second-inner-node' } as unknown as ElementRef;
162
+ vi.mocked(__GetElementByAttrSlot).mockImplementation((_handle, slot) =>
163
+ slot === 0 ? TEST_ELEMENT_REF : secondElement
164
+ );
165
+ try {
166
+ registerMTRefSlots(17, 0, 1);
167
+ setMTRefSlot(17, 0, { type: 'main-thread-ref', value: firstRef });
168
+ setMTRefSlot(17, 1, { type: 'main-thread-ref', value: secondRef });
169
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: TEST_NATIVE_REF }]);
170
+ expect(updateWorkletRef).not.toHaveBeenCalled();
171
+ const options = { emptyPatch: true, asyncFlush: true };
172
+ flushElementTemplate(options);
173
+ expect(updateWorkletRef.mock.calls).toEqual([[firstRef, TEST_ELEMENT_REF], [secondRef, secondElement]]);
174
+ expect(flush).toHaveBeenCalledWith(undefined, options);
175
+ expect(updateWorkletRef.mock.invocationCallOrder.at(-1)).toBeLessThan(flush.mock.invocationCallOrder[0]!);
176
+ } finally {
177
+ restore();
178
+ }
179
+ });
180
+
181
+ it('fails visibly when native cannot resolve a submitted ref target during ref delivery', () => {
182
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue(null);
183
+ registerMTRefSlots(17, 0);
184
+ setMTRefSlot(17, 0, { type: 'main-thread-ref', value: { _wvid: 7 } });
185
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: TEST_NATIVE_REF }]);
186
+ expect(() => applyPendingMainThreadRefAttachments()).toThrow('target 17:0 could not be materialized');
187
+ });
188
+
92
189
  it('records native-held MTEvent ctx by handle and slot without storing the wrapper', () => {
93
190
  const ctx = { _wkltId: 'tap' };
94
191
  const wrapper = { type: 'worklet', value: ctx };
@@ -139,16 +236,16 @@ describe('main-thread dynamic attr state', () => {
139
236
  registerMTRefSlots(17, 0);
140
237
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: firstRef });
141
238
 
142
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: nativeRef }]);
239
+ attachAndComplete([{ uid: 17, ref: nativeRef }]);
143
240
 
144
- expect(updateWorkletRef).toHaveBeenCalledWith(firstRef, nativeRef);
241
+ expect(updateWorkletRef).toHaveBeenCalledWith(firstRef, TEST_ELEMENT_REF);
145
242
  expect(getMainThreadDynamicAttrState(17, 0)).toEqual({
146
243
  kind: 'mt-ref',
147
244
  value: firstRef,
148
245
  });
149
246
 
150
247
  updateWorkletRef.mockClear();
151
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: nativeRef }]);
248
+ attachAndComplete([{ uid: 17, ref: nativeRef }]);
152
249
  expect(updateWorkletRef).not.toHaveBeenCalled();
153
250
 
154
251
  updateWorkletRef.mockClear();
@@ -175,9 +272,9 @@ describe('main-thread dynamic attr state', () => {
175
272
 
176
273
  expect(updateWorkletRef).not.toHaveBeenCalled();
177
274
 
178
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: nativeRef }]);
275
+ attachAndComplete([{ uid: 17, ref: nativeRef }]);
179
276
 
180
- expect(updateWorkletRef).toHaveBeenCalledWith(nextRef, nativeRef);
277
+ expect(updateWorkletRef).toHaveBeenCalledWith(nextRef, TEST_ELEMENT_REF);
181
278
  expect(getMainThreadDynamicAttrState(17, 0)).toEqual({
182
279
  kind: 'mt-ref',
183
280
  value: nextRef,
@@ -193,12 +290,12 @@ describe('main-thread dynamic attr state', () => {
193
290
 
194
291
  try {
195
292
  registerMTRefSlots(17, 0);
196
- attachMainThreadDynamicAttrRefsForSubtree([{ uid: 17, ref: TEST_NATIVE_REF }]);
293
+ attachAndComplete([{ uid: 17, ref: TEST_NATIVE_REF }]);
197
294
  expect(updateWorkletRef).not.toHaveBeenCalled();
198
295
 
199
296
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: ref });
200
297
 
201
- expect(updateWorkletRef).toHaveBeenCalledWith(ref, TEST_NATIVE_REF);
298
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, TEST_ELEMENT_REF);
202
299
  } finally {
203
300
  restore();
204
301
  }
@@ -215,11 +312,11 @@ describe('main-thread dynamic attr state', () => {
215
312
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: ref });
216
313
 
217
314
  updateWorkletRef.mockImplementationOnce(() => {
218
- attachMainThreadDynamicAttrRefsForSubtree(subtree);
315
+ attachAndComplete(subtree);
219
316
  });
220
- attachMainThreadDynamicAttrRefsForSubtree(subtree);
317
+ attachAndComplete(subtree);
221
318
  expect(updateWorkletRef).toHaveBeenCalledTimes(1);
222
- expect(updateWorkletRef).toHaveBeenCalledWith(ref, nativeRef);
319
+ expect(updateWorkletRef).toHaveBeenCalledWith(ref, TEST_ELEMENT_REF);
223
320
 
224
321
  updateWorkletRef.mockClear();
225
322
  updateWorkletRef.mockImplementationOnce(() => {
@@ -241,7 +338,7 @@ describe('main-thread dynamic attr state', () => {
241
338
  try {
242
339
  registerMTRefSlots(17, 0);
243
340
  setMTRefSlot(17, 0, { type: 'main-thread-ref', value: ref });
244
- attachMainThreadDynamicAttrRefsForSubtree(subtree);
341
+ attachAndComplete(subtree);
245
342
  detachMainThreadDynamicAttrRefsForSubtree(subtree);
246
343
 
247
344
  updateWorkletRef.mockClear();
@@ -254,31 +351,33 @@ describe('main-thread dynamic attr state', () => {
254
351
  }
255
352
  });
256
353
 
257
- it('retains native-held MTEvent ctx recorded from initial attribute slots', () => {
354
+ it('tracks prepared MTEvent input once before committing initial slots', () => {
258
355
  const { addRef, restore } = installJsFunctionLifecycleManager();
259
356
  const ctx = { _execId: 99, _wkltId: 'tap' };
260
357
  __etAttrPlanMap[MT_EVENT_TEMPLATE] = [0, adaptMTEventAttrSlot];
261
358
 
262
359
  try {
263
- initializeMainThreadDynamicAttrSlots(17, MT_EVENT_TEMPLATE, [{
264
- type: 'worklet',
265
- value: ctx,
266
- }]);
360
+ const slots = [{ type: 'worklet', value: ctx }];
361
+ prepareMainThreadDynamicAttrSlotsForNative(MT_EVENT_TEMPLATE, slots);
362
+ expect(getMainThreadDynamicAttrState(17, 0)).toBeUndefined();
363
+ initializeMainThreadDynamicAttrSlots(17, MT_EVENT_TEMPLATE, slots);
267
364
 
268
- expect(addRef).toHaveBeenCalledWith(99, ctx);
365
+ expect(addRef).toHaveBeenCalledExactlyOnceWith(99, ctx);
269
366
  } finally {
270
367
  restore();
271
368
  }
272
369
  });
273
370
 
274
- it('retains native-held MTEvent ctx recorded from updates', () => {
371
+ it('commits MTEvent updates without repeating prepared input tracking', () => {
275
372
  const { addRef, restore } = installJsFunctionLifecycleManager();
276
373
  const ctx = { _execId: 100, _wkltId: 'tap' };
277
374
 
278
375
  try {
376
+ retainMainThreadBackgroundFunctionCtx(ctx);
377
+ expect(getMainThreadDynamicAttrState(17, 3)).toBeUndefined();
279
378
  setMTEventSlot(17, 3, { type: 'worklet', value: ctx });
280
379
 
281
- expect(addRef).toHaveBeenCalledWith(100, ctx);
380
+ expect(addRef).toHaveBeenCalledExactlyOnceWith(100, ctx);
282
381
  } finally {
283
382
  restore();
284
383
  }
@@ -410,7 +509,7 @@ describe('main-thread dynamic attr state', () => {
410
509
  const previousRunWorklet = globalThis.runWorklet;
411
510
  const ref = { _wvid: 7 };
412
511
  const callbackCleanup = vi.fn();
413
- const callback = { _wkltId: 'ref-callback', _unmount: callbackCleanup };
512
+ const callback = { _wkltId: 'ref-callback' };
414
513
  globalThis.runWorklet = vi.fn(() => callbackCleanup);
415
514
 
416
515
  try {
@@ -430,7 +529,7 @@ describe('main-thread dynamic attr state', () => {
430
529
  false,
431
530
  { id: 'callback-target' } as unknown as ElementTemplateHandle,
432
531
  );
433
- attachMainThreadDynamicAttrRefsForSubtree([
532
+ attachAndComplete([
434
533
  { uid: 17, ref: { id: 'ref-target' } as unknown as ElementTemplateHandle },
435
534
  { uid: 18, ref: { id: 'callback-target' } as unknown as ElementTemplateHandle },
436
535
  ]);
@@ -439,7 +538,7 @@ describe('main-thread dynamic attr state', () => {
439
538
 
440
539
  expect(updateWorkletRef).toHaveBeenCalledWith(ref, null);
441
540
  expect(callbackCleanup).toHaveBeenCalledTimes(1);
442
- expect(globalThis.runWorklet).toHaveBeenCalledWith(callback, [{ elementRefptr: { id: 'callback-target' } }]);
541
+ expect(globalThis.runWorklet).toHaveBeenCalledWith(callback, [{ elementRefptr: TEST_ELEMENT_REF }]);
443
542
  expect(getMainThreadDynamicAttrState(17, 0)).toBeUndefined();
444
543
  expect(getMainThreadDynamicAttrState(18, 0)).toBeUndefined();
445
544
  } finally {
@@ -462,7 +561,7 @@ describe('main-thread dynamic attr state', () => {
462
561
  false,
463
562
  { id: 'callback-target' } as unknown as ElementTemplateHandle,
464
563
  );
465
- attachMainThreadDynamicAttrRefsForSubtree([
564
+ attachAndComplete([
466
565
  { uid: 17, ref: { id: 'callback-target' } as unknown as ElementTemplateHandle },
467
566
  ]);
468
567
 
@@ -488,7 +587,7 @@ describe('main-thread dynamic attr state', () => {
488
587
  false,
489
588
  { id: 'target' } as unknown as ElementTemplateHandle,
490
589
  );
491
- attachMainThreadDynamicAttrRefsForSubtree([
590
+ attachAndComplete([
492
591
  { uid: 17, ref: { id: 'target' } as unknown as ElementTemplateHandle },
493
592
  ]);
494
593
 
@@ -564,4 +663,188 @@ describe('main-thread dynamic attr state', () => {
564
663
  globalThis.lynxWorkletImpl = previousWorkletImpl;
565
664
  }
566
665
  });
666
+
667
+ it('retains spread MTEvent, Gesture, and callback MTRef once before Native and keeps state intact on Native rejection', () => {
668
+ const { addRef, restore } = installJsFunctionLifecycleManager();
669
+ const { updateWorkletRef, restore: restoreRefs } = installRefRuntime();
670
+ const eventCtx = { _execId: 201, _wkltId: 'spread-tap' };
671
+ const gestureCb = { _execId: 202, _wkltId: 'spread-pan' };
672
+ const refCb = { _execId: 203, _wkltId: 'spread-ref-cb' };
673
+ const initialObjectRef = { _wvid: 55 };
674
+ __etAttrPlanMap._et_spread_retain = [0, adaptSpreadAttrSlot];
675
+
676
+ try {
677
+ const initialSlot = {
678
+ id: 'card',
679
+ 'main-thread:bindtap': { type: 'worklet', value: eventCtx },
680
+ 'main-thread:gesture': {
681
+ type: 'gesture',
682
+ value: [{
683
+ id: 1,
684
+ type: 0,
685
+ config: {},
686
+ callbacks: [{ name: 'onUpdate', callback: gestureCb }],
687
+ relationMap: { waitFor: [], simultaneous: [], continueWith: [] },
688
+ }],
689
+ },
690
+ 'main-thread:ref': { type: 'main-thread-ref', value: initialObjectRef },
691
+ };
692
+
693
+ const nativeSlots = prepareMainThreadDynamicAttrSlotsForNative('_et_spread_retain', [initialSlot]);
694
+ expect(nativeSlots?.[0]).toEqual({
695
+ id: 'card',
696
+ 'main-thread:bindtap': { type: 'worklet', value: eventCtx },
697
+ 'main-thread:gesture': initialSlot['main-thread:gesture'],
698
+ 'has-react-ref': true,
699
+ flatten: false,
700
+ });
701
+ expect(addRef.mock.calls).toEqual([
702
+ [201, eventCtx],
703
+ [202, gestureCb],
704
+ ]);
705
+
706
+ initializeMainThreadDynamicAttrSlots(17, '_et_spread_retain', [initialSlot]);
707
+ expect(isSpreadDynamicAttrSlot(17, 0)).toBe(true);
708
+ expect(addRef).toHaveBeenCalledTimes(2);
709
+ attachAndComplete([{ uid: 17, ref: TEST_NATIVE_REF }]);
710
+ expect(updateWorkletRef).toHaveBeenCalledExactlyOnceWith(initialObjectRef, TEST_ELEMENT_REF);
711
+ updateWorkletRef.mockClear();
712
+
713
+ // prepareSpreadAttrSlotForNative retains callbacks and strips main-thread:ref without mutating attached ref state.
714
+ const unappliedNextSlot = {
715
+ id: 'card-rejected',
716
+ 'main-thread:ref': { type: 'main-thread-ref', value: refCb },
717
+ };
718
+ expect(prepareSpreadAttrSlotForNative(null)).toBeNull();
719
+ const preparedForNative = prepareSpreadAttrSlotForNative(unappliedNextSlot);
720
+ expect(preparedForNative).toEqual({
721
+ id: 'card-rejected',
722
+ 'has-react-ref': true,
723
+ flatten: false,
724
+ });
725
+ expect(addRef).toHaveBeenLastCalledWith(203, refCb);
726
+ expect(updateWorkletRef).not.toHaveBeenCalled();
727
+
728
+ // If hydration completes without a setAttribute op for this spread slot (because BTS omitted
729
+ // 'main-thread:ref' and remaining keys matched Native), the unhydrated first-screen ref is cleaned up.
730
+ applyPendingMainThreadRefAttachments(true);
731
+ expect(updateWorkletRef).toHaveBeenCalledExactlyOnceWith(initialObjectRef, null);
732
+ updateWorkletRef.mockClear();
733
+
734
+ deleteMainThreadDynamicAttrStateForSubtree([17]);
735
+ expect(updateWorkletRef).not.toHaveBeenCalled();
736
+ } finally {
737
+ restoreRefs();
738
+ restore();
739
+ }
740
+ });
741
+
742
+ it('reconciles multi-slot MTRef overrides, key-deletion fallbacks, and JSON.parse sibling updates without disturbing distinct elements', () => {
743
+ const previousRunWorklet = globalThis.runWorklet;
744
+ const { updateWorkletRef, restore } = installRefRuntime();
745
+ const sharedElement = { id: 'shared-el' } as unknown as ElementRef;
746
+ const distinctElement = { id: 'distinct-el' } as unknown as ElementRef;
747
+ vi.mocked(__GetElementByAttrSlot).mockImplementation((_handle, slot) =>
748
+ slot === 3 ? distinctElement : sharedElement
749
+ );
750
+
751
+ const directRef = { _wvid: 1 };
752
+ const distinctRef = { _wvid: 99 };
753
+ const spread1Cleanup = vi.fn();
754
+ const spread1Cb = { _wkltId: 'spread1-cb', _execId: 301 };
755
+ const spread2Ref = { _wvid: 2 };
756
+ globalThis.runWorklet = vi.fn(() => spread1Cleanup);
757
+
758
+ __etAttrPlanMap._et_multi_mtref = [
759
+ 0,
760
+ adaptMTRefAttrSlot,
761
+ 1,
762
+ adaptSpreadAttrSlot,
763
+ 2,
764
+ adaptSpreadAttrSlot,
765
+ 3,
766
+ adaptSpreadAttrSlot,
767
+ ];
768
+
769
+ try {
770
+ initializeMainThreadDynamicAttrSlots(21, '_et_multi_mtref', [
771
+ { type: 'main-thread-ref', value: directRef },
772
+ { id: 's1', 'main-thread:ref': { type: 'main-thread-ref', value: spread1Cb } },
773
+ { id: 's2', 'main-thread:ref': { type: 'main-thread-ref', value: spread2Ref } },
774
+ { id: 's3', 'main-thread:ref': { type: 'main-thread-ref', value: distinctRef } },
775
+ ]);
776
+ attachAndComplete([{ uid: 21, ref: TEST_NATIVE_REF }]);
777
+
778
+ // Slot 2 overrides slots 0 & 1 on sharedElement; slot 3 attaches independently on distinctElement.
779
+ expect(updateWorkletRef.mock.calls).toEqual([
780
+ [spread2Ref, sharedElement],
781
+ [distinctRef, distinctElement],
782
+ ]);
783
+ expect(globalThis.runWorklet).not.toHaveBeenCalled();
784
+ updateWorkletRef.mockClear();
785
+
786
+ // Explicit null in slot 2 ({ 'main-thread:ref': undefined }) detaches sharedElement without falling back.
787
+ updateSpreadAttrSlot(21, 2, { id: 's2', 'main-thread:ref': null }, TEST_NATIVE_REF);
788
+ completeRefAttachments();
789
+ expect(updateWorkletRef.mock.calls).toEqual([[spread2Ref, null]]);
790
+ expect(globalThis.runWorklet).not.toHaveBeenCalled();
791
+ updateWorkletRef.mockClear();
792
+
793
+ // Omitting 'main-thread:ref' from slot 2 falls back to slot 1's callback ref.
794
+ updateSpreadAttrSlot(21, 2, { id: 's2-no-ref' }, TEST_NATIVE_REF);
795
+ completeRefAttachments();
796
+ expect(globalThis.runWorklet).toHaveBeenCalledExactlyOnceWith(spread1Cb, [{ elementRefptr: sharedElement }]);
797
+ expect(spread1Cleanup).not.toHaveBeenCalled();
798
+ vi.mocked(globalThis.runWorklet).mockClear();
799
+
800
+ // Sibling update in slot 1 arriving as a fresh JSON.parse clone with the same _execId does NOT remount spread1Cb.
801
+ const jsonParsedSameCbClone = JSON.parse(JSON.stringify({
802
+ id: 's1-updated',
803
+ 'main-thread:ref': { type: 'main-thread-ref', value: { _wkltId: 'spread1-cb', _execId: 301 } },
804
+ }));
805
+ updateSpreadAttrSlot(21, 1, jsonParsedSameCbClone, TEST_NATIVE_REF);
806
+ completeRefAttachments();
807
+ expect(spread1Cleanup).not.toHaveBeenCalled();
808
+ expect(globalThis.runWorklet).not.toHaveBeenCalled();
809
+
810
+ // Clearing slot 1 entirely runs spread1Cleanup synchronously and falls back to direct slot 0!
811
+ updateSpreadAttrSlot(21, 1, null, TEST_NATIVE_REF);
812
+ expect(spread1Cleanup).toHaveBeenCalledTimes(1);
813
+ completeRefAttachments();
814
+ expect(updateWorkletRef.mock.calls).toEqual([[directRef, sharedElement]]);
815
+ updateWorkletRef.mockClear();
816
+
817
+ // Subtree deletion cleans both sharedElement (directRef) and distinctElement (distinctRef).
818
+ deleteMainThreadDynamicAttrStateForSubtree([21]);
819
+ expect(updateWorkletRef.mock.calls).toEqual(expect.arrayContaining([
820
+ [directRef, null],
821
+ [distinctRef, null],
822
+ ]));
823
+ expect(updateWorkletRef).toHaveBeenCalledTimes(2);
824
+ expect(spread1Cleanup).toHaveBeenCalledTimes(1);
825
+
826
+ // Masking an attached callback ref via a later spread's explicit null cleans it once
827
+ // and does not re-run cleanup on subtree deletion.
828
+ const maskedCleanup = vi.fn();
829
+ const maskedCb = { _wkltId: 'masked-cb', _execId: 302 };
830
+ globalThis.runWorklet = vi.fn(() => maskedCleanup);
831
+ __etAttrPlanMap._et_masked_destroy = [0, adaptSpreadAttrSlot, 1, adaptSpreadAttrSlot];
832
+ initializeMainThreadDynamicAttrSlots(77, '_et_masked_destroy', [
833
+ { 'main-thread:ref': { type: 'main-thread-ref', value: maskedCb } },
834
+ {},
835
+ ]);
836
+ attachAndComplete([{ uid: 77, ref: TEST_NATIVE_REF }]);
837
+ expect(maskedCleanup).toHaveBeenCalledTimes(0);
838
+
839
+ updateSpreadAttrSlot(77, 1, { 'main-thread:ref': null }, TEST_NATIVE_REF);
840
+ completeRefAttachments();
841
+ expect(maskedCleanup).toHaveBeenCalledTimes(1);
842
+
843
+ deleteMainThreadDynamicAttrStateForSubtree([77]);
844
+ expect(maskedCleanup).toHaveBeenCalledTimes(1);
845
+ } finally {
846
+ globalThis.runWorklet = previousRunWorklet;
847
+ restore();
848
+ }
849
+ });
567
850
  });
@@ -4,6 +4,10 @@
4
4
 
5
5
  import { flushCoreContextEvents, flushJSContextEvents } from '../mock/mockNativePapi/context.js';
6
6
  import { installMainThreadHooks } from '../../../../src/core/hooks/mainThreadImpl.js';
7
+ import { takeMainThreadRefInitValuePatch } from '../../../../src/core/main-thread-ref-init-value.js';
8
+ import { clearMainThreadRefLastIdForTesting } from '../../../../src/core/main-thread-ref.js';
9
+ import { clearMtsConfigCacheForTesting } from '../../../../src/core/mts-capability.js';
10
+ import { resetTimingState } from '../../../../src/core/performance.js';
7
11
  import { setupBackgroundElementTemplateDocument } from '../../../../src/element-template/background/document.js';
8
12
  import { BackgroundPageRootInstance } from '../../../../src/element-template/background/instance.js';
9
13
  import { backgroundElementTemplateInstanceManager } from '../../../../src/element-template/background/manager.js';
@@ -21,11 +25,6 @@ type EnvTarget = typeof globalThis & {
21
25
  __USE_ELEMENT_TEMPLATE__: boolean | undefined;
22
26
  };
23
27
 
24
- type RootWithTemplates = RootRef & {
25
- __jsx?: unknown;
26
- __opcodes?: unknown;
27
- };
28
-
29
28
  export class ElementTemplateEnvManager {
30
29
  private mainRoot: RootRef | undefined;
31
30
  private backgroundRoot: BackgroundPageRootInstance | undefined;
@@ -77,11 +76,15 @@ export class ElementTemplateEnvManager {
77
76
  this.backgroundRoot = undefined;
78
77
  // @ts-expect-error - allow reset to undefined during tests
79
78
  setRoot(undefined);
79
+ resetTimingState();
80
80
 
81
81
  backgroundElementTemplateInstanceManager.clear();
82
82
  backgroundElementTemplateInstanceManager.nextId = 0;
83
83
  elementTemplateRegistry.clear();
84
84
  resetTemplateId();
85
+ takeMainThreadRefInitValuePatch();
86
+ clearMainThreadRefLastIdForTesting();
87
+ clearMtsConfigCacheForTesting();
85
88
 
86
89
  if (initial === 'background') {
87
90
  this.switchToBackground();
@@ -35,6 +35,7 @@ export interface MockNativePapi {
35
35
  mockRemoveElementTemplateNodeFromSlot: any;
36
36
  mockReportError: any;
37
37
  mockFlushElementTree: any;
38
+ mockGetElementByAttrSlot: any;
38
39
  cleanup: () => void;
39
40
  }
40
41
 
@@ -347,8 +348,38 @@ export function installMockNativePapi(
347
348
  },
348
349
  );
349
350
 
350
- const mockFlushElementTree = vi.fn().mockImplementation((element: unknown, options: unknown) => {
351
- nativeLog.push(['__FlushElementTree', formatNode(element), options]);
351
+ const mockFlushElementTree = vi.fn().mockImplementation((element: unknown, flushOptions?: FlushOptions) => {
352
+ nativeLog.push(['__FlushElementTree', formatNode(element), flushOptions]);
353
+ });
354
+
355
+ const rootTargets = new WeakMap<object, object>();
356
+ const mockGetElementByAttrSlot = vi.fn((handle: CompiledTemplateNode, slot: number) => {
357
+ function findTarget(definition: unknown, element: unknown): object | null {
358
+ if (!isRecord(definition) || !isRecord(element)) return null;
359
+ const descriptors = definition['attributesArray'] as Array<{ kind: string; attrSlotIndex?: number }> | undefined;
360
+ if (descriptors?.some(descriptor => descriptor.kind !== 'static' && descriptor.attrSlotIndex === slot)) {
361
+ if (element !== handle) {
362
+ if (typeof element['__mockNativeId'] !== 'number') attachMockNativeId(element);
363
+ return element;
364
+ }
365
+ // Even a root target is an ElementRef distinct from the ET handle.
366
+ let target = rootTargets.get(handle);
367
+ if (!target) {
368
+ target = Object.create(handle) as object;
369
+ attachMockNativeId(target);
370
+ rootTargets.set(handle, target);
371
+ }
372
+ return target;
373
+ }
374
+ const definitions = definition['children'] as unknown[] | undefined;
375
+ const elements = element['children'] as unknown[] | undefined;
376
+ for (const [index, child] of (definitions ?? []).entries()) {
377
+ const target = findTarget(child, elements?.[index]);
378
+ if (target) return target;
379
+ }
380
+ return null;
381
+ }
382
+ return findTarget(handle.__compiledTemplate, handle);
352
383
  });
353
384
 
354
385
  vi.stubGlobal('__CreateElementTemplate', mockCreateCompiledElementTemplate);
@@ -365,6 +396,7 @@ export function installMockNativePapi(
365
396
  vi.stubGlobal('__RemoveNodeFromElementTemplate', mockRemoveElementTemplateNodeFromSlot);
366
397
  vi.stubGlobal('__SerializeElementTemplate', mockSerializeElementTemplate);
367
398
  vi.stubGlobal('__FlushElementTree', mockFlushElementTree);
399
+ vi.stubGlobal('__GetElementByAttrSlot', mockGetElementByAttrSlot);
368
400
  const currentLynx = (globalThis as unknown as { lynx?: any }).lynx;
369
401
  const baseLynx = (currentLynx && typeof currentLynx === 'object') ? currentLynx : {};
370
402
  const baseGetNativeApp = typeof baseLynx.getNativeApp === 'function'
@@ -395,6 +427,7 @@ export function installMockNativePapi(
395
427
  mockRemoveElementTemplateNodeFromSlot: mockRemoveElementTemplateNodeFromSlot,
396
428
  mockReportError: mockReportError,
397
429
  mockFlushElementTree: mockFlushElementTree,
430
+ mockGetElementByAttrSlot,
398
431
  cleanup: (): void => {
399
432
  const errorCalls = mockReportError.mock.calls;
400
433
  if (clearTemplatesOnCleanup) {