@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
@@ -7,9 +7,21 @@ import type { Worklet } from '@lynx-js/react/worklet-runtime/bindings';
7
7
 
8
8
  import { getMainThreadDynamicAttrSlotKinds } from './attr-slot-plan.js';
9
9
  import { retainMainThreadBackgroundFunctionCtx } from './main-thread-background-function.js';
10
+ import { isMTEventNativeWrapper } from './main-thread-event-ctx.js';
10
11
  import type { MTEventCtx, MTEventNativeWrapper } from './main-thread-event-ctx.js';
11
- import { attachMTRefValue, cleanupMTRefValue, hydrateMTRefValue, retainMTRefValue } from './main-thread-ref-ctx.js';
12
+ import {
13
+ attachMTRefValue,
14
+ cleanupMTRefValue,
15
+ hydrateMTRefValue,
16
+ isMTRefNativeWrapper,
17
+ retainMTRefValue,
18
+ } from './main-thread-ref-ctx.js';
12
19
  import type { MTRefNativeWrapper, MTRefValue } from './main-thread-ref-ctx.js';
20
+ import { isMainThreadFunction } from '../../../core/main-thread-function.js';
21
+ import { isMainThreadRef } from '../../../core/main-thread-ref.js';
22
+ import { hydrateGestureCallbacks, isGestureSlotValue, retainGestureCallbacks } from '../../prop-adapters/gesture.js';
23
+ import type { GestureSlotValue } from '../../prop-adapters/gesture.js';
24
+ import { isMainThreadEventPropKey, isSpreadRecord } from '../../prop-adapters/spread.js';
13
25
  import type { SerializableValue } from '../../protocol/types.js';
14
26
 
15
27
  type MainThreadDynamicAttrState =
@@ -18,9 +30,27 @@ type MainThreadDynamicAttrState =
18
30
 
19
31
  type MainThreadDynamicAttrStateMap<T> = Map<number, Map<number, T>>;
20
32
 
33
+ interface SpreadSlotDynamicState {
34
+ events?: Map<string, MTEventCtx> | undefined;
35
+ gesture?: GestureSlotValue | undefined;
36
+ }
37
+
38
+ interface AttachedMTRefEntry {
39
+ slotIndex: number;
40
+ value: MTRefValue;
41
+ }
42
+
43
+ const gestureAttrState: MainThreadDynamicAttrStateMap<GestureSlotValue> = new Map();
21
44
  const mtEventAttrState: MainThreadDynamicAttrStateMap<MTEventCtx> = new Map();
22
45
  const mtRefAttrState: MainThreadDynamicAttrStateMap<MTRefValue> = new Map();
23
- const materializedMTRefHandleIds = new Set<number>();
46
+ const mtRefSlotSpecs: MainThreadDynamicAttrStateMap<MTRefValue | null> = new Map();
47
+ const spreadAttrSlots = new Map<number, Set<number>>();
48
+ const spreadSlotState: MainThreadDynamicAttrStateMap<SpreadSlotDynamicState> = new Map();
49
+ const unhydratedSpreadMTRefSlots = new Map<number, Set<number>>();
50
+ const attachedMTRefHandles = new Map<number, ElementTemplateHandle>();
51
+ const attachedMTRefsByElement = new Map<number, Map<ElementRef, AttachedMTRefEntry>>();
52
+
53
+ let pendingMTRefReconciliations = new Map<number, ElementTemplateHandle>();
24
54
 
25
55
  export interface MainThreadDynamicAttrSubtreeHandle {
26
56
  uid: number;
@@ -42,12 +72,6 @@ function deleteSlotState<T>(
42
72
  }
43
73
  }
44
74
 
45
- function cleanupMTRefHandleState(handleState: Map<number, MTRefValue>): void {
46
- for (const value of handleState.values()) {
47
- cleanupMTRefValue(value);
48
- }
49
- }
50
-
51
75
  function setSlotState<T>(
52
76
  state: MainThreadDynamicAttrStateMap<T>,
53
77
  handleId: number,
@@ -62,18 +86,159 @@ function setSlotState<T>(
62
86
  handleState.set(attrSlotIndex, value);
63
87
  }
64
88
 
89
+ function registerHandleSlot(
90
+ slotsByHandle: Map<number, Set<number>>,
91
+ handleId: number,
92
+ attrSlotIndex: number,
93
+ ): void {
94
+ let slots = slotsByHandle.get(handleId);
95
+ if (!slots) {
96
+ slots = new Set();
97
+ slotsByHandle.set(handleId, slots);
98
+ }
99
+ slots.add(attrSlotIndex);
100
+ }
101
+
102
+ function deleteHandleSlot(
103
+ slotsByHandle: Map<number, Set<number>>,
104
+ handleId: number,
105
+ attrSlotIndex: number,
106
+ ): void {
107
+ const slots = slotsByHandle.get(handleId);
108
+ if (!slots) {
109
+ return;
110
+ }
111
+ slots.delete(attrSlotIndex);
112
+ if (slots.size === 0) {
113
+ slotsByHandle.delete(handleId);
114
+ }
115
+ }
116
+
117
+ function hasNonNullMTRefSpec(handleId: number): boolean {
118
+ const specs = mtRefSlotSpecs.get(handleId);
119
+ if (!specs) {
120
+ return false;
121
+ }
122
+ for (const value of specs.values()) {
123
+ if (value !== null) {
124
+ return true;
125
+ }
126
+ }
127
+ return false;
128
+ }
129
+
130
+ function isSameMTRefSpec(
131
+ prev: MTRefValue | null | undefined,
132
+ next: MTRefValue | null | undefined,
133
+ isHydration: boolean,
134
+ ): boolean {
135
+ if (prev === next) {
136
+ return true;
137
+ }
138
+ if (prev == null || next == null) {
139
+ return false;
140
+ }
141
+ if (isMainThreadRef(prev) && isMainThreadRef(next)) {
142
+ return prev._wvid === next._wvid;
143
+ }
144
+ return !isHydration
145
+ && isMainThreadFunction(prev)
146
+ && isMainThreadFunction(next)
147
+ && prev._execId !== undefined
148
+ && prev._execId === next._execId;
149
+ }
150
+
151
+ function cleanupAttachedElementsForHandle(handleId: number): void {
152
+ const attachedElements = attachedMTRefsByElement.get(handleId);
153
+ if (!attachedElements) {
154
+ return;
155
+ }
156
+ attachedMTRefsByElement.delete(handleId);
157
+ for (const entry of attachedElements.values()) {
158
+ cleanupMTRefValue(entry.value);
159
+ }
160
+ }
161
+
162
+ function cleanupAttachedElementsForSlot(handleId: number, attrSlotIndex: number): void {
163
+ const attachedElements = attachedMTRefsByElement.get(handleId);
164
+ if (!attachedElements) {
165
+ return;
166
+ }
167
+ const toCleanup: MTRefValue[] = [];
168
+ for (const [element, entry] of attachedElements) {
169
+ if (entry.slotIndex === attrSlotIndex) {
170
+ attachedElements.delete(element);
171
+ toCleanup.push(entry.value);
172
+ }
173
+ }
174
+ if (attachedElements.size === 0) {
175
+ attachedMTRefsByElement.delete(handleId);
176
+ }
177
+ for (const value of toCleanup) {
178
+ cleanupMTRefValue(value);
179
+ }
180
+ }
181
+
65
182
  function setMTEventSlotState(handleId: number, attrSlotIndex: number, nativeHeldValue: MTEventCtx): void {
66
- retainMainThreadBackgroundFunctionCtx(nativeHeldValue);
67
183
  setSlotState(mtEventAttrState, handleId, attrSlotIndex, nativeHeldValue);
68
184
  }
69
185
 
70
- function setMTRefSlotState(
186
+ function setDirectMTRefSlotState(
71
187
  handleId: number,
72
188
  attrSlotIndex: number,
73
189
  value: MTRefValue,
74
190
  ): void {
75
191
  retainMTRefValue(value);
76
192
  setSlotState(mtRefAttrState, handleId, attrSlotIndex, value);
193
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, value);
194
+ }
195
+
196
+ export function isSpreadDynamicAttrSlot(
197
+ handleId: number,
198
+ attrSlotIndex: number,
199
+ ): boolean {
200
+ return spreadAttrSlots.get(handleId)?.has(attrSlotIndex) === true;
201
+ }
202
+
203
+ export function prepareSpreadAttrSlotForNative(
204
+ value: unknown,
205
+ ): SerializableValue | null {
206
+ if (!isSpreadRecord(value)) {
207
+ return null;
208
+ }
209
+ const keys = Object.keys(value);
210
+ let hasOwnMTRef = false;
211
+ let activeMTRefWrapper: MTRefNativeWrapper | undefined;
212
+ for (const key of keys) {
213
+ const entry = value[key];
214
+ if (key === 'main-thread:ref') {
215
+ hasOwnMTRef = true;
216
+ if (isMTRefNativeWrapper(entry)) {
217
+ activeMTRefWrapper = entry;
218
+ retainMTRefValue(entry.value);
219
+ }
220
+ } else if (key === 'main-thread:gesture') {
221
+ if (isGestureSlotValue(entry)) {
222
+ retainGestureCallbacks(entry);
223
+ }
224
+ } else if (isMainThreadEventPropKey(key) && isMTEventNativeWrapper(entry)) {
225
+ retainMainThreadBackgroundFunctionCtx(entry.value);
226
+ }
227
+ }
228
+ if (!hasOwnMTRef) {
229
+ return value as SerializableValue;
230
+ }
231
+ const nativeSpread: Record<string, SerializableValue> = {};
232
+ for (const key of keys) {
233
+ if (key !== 'main-thread:ref') {
234
+ nativeSpread[key] = value[key] as SerializableValue;
235
+ }
236
+ }
237
+ if (activeMTRefWrapper) {
238
+ nativeSpread['has-react-ref'] = true;
239
+ nativeSpread['flatten'] = false;
240
+ }
241
+ return nativeSpread;
77
242
  }
78
243
 
79
244
  export function prepareMainThreadDynamicAttrSlotsForNative(
@@ -89,6 +254,22 @@ export function prepareMainThreadDynamicAttrSlotsForNative(
89
254
  }
90
255
  let nativeSlots: SerializableValue[] | undefined;
91
256
  for (const [attrSlotIndex, kind] of slotKinds) {
257
+ if (kind === 'gesture' && attributeSlots[attrSlotIndex] != null) {
258
+ retainGestureCallbacks(attributeSlots[attrSlotIndex] as GestureSlotValue);
259
+ }
260
+ if (kind === 'mt-event' && attributeSlots[attrSlotIndex] != null) {
261
+ retainMainThreadBackgroundFunctionCtx((attributeSlots[attrSlotIndex] as MTEventNativeWrapper).value);
262
+ continue;
263
+ }
264
+ if (kind === 'spread') {
265
+ const slotValue = attributeSlots[attrSlotIndex];
266
+ const nativeSpread = prepareSpreadAttrSlotForNative(slotValue);
267
+ if (nativeSpread !== slotValue) {
268
+ nativeSlots ??= attributeSlots.slice() as SerializableValue[];
269
+ nativeSlots[attrSlotIndex] = nativeSpread;
270
+ }
271
+ continue;
272
+ }
92
273
  if (kind !== 'mt-ref') {
93
274
  continue;
94
275
  }
@@ -99,14 +280,23 @@ export function prepareMainThreadDynamicAttrSlotsForNative(
99
280
  }
100
281
 
101
282
  export function clearMainThreadDynamicAttrState(): void {
102
- for (const [handleId, handleState] of mtRefAttrState) {
103
- if (materializedMTRefHandleIds.has(handleId)) {
104
- cleanupMTRefHandleState(handleState);
283
+ const attachedHandleIds = Array.from(attachedMTRefHandles.keys());
284
+ attachedMTRefHandles.clear();
285
+ unhydratedSpreadMTRefSlots.clear();
286
+ pendingMTRefReconciliations.clear();
287
+ try {
288
+ for (const handleId of attachedHandleIds) {
289
+ cleanupAttachedElementsForHandle(handleId);
105
290
  }
291
+ } finally {
292
+ gestureAttrState.clear();
293
+ mtEventAttrState.clear();
294
+ mtRefAttrState.clear();
295
+ mtRefSlotSpecs.clear();
296
+ spreadAttrSlots.clear();
297
+ spreadSlotState.clear();
298
+ attachedMTRefsByElement.clear();
106
299
  }
107
- mtEventAttrState.clear();
108
- mtRefAttrState.clear();
109
- materializedMTRefHandleIds.clear();
110
300
  }
111
301
 
112
302
  export function getMainThreadDynamicAttrState(
@@ -121,10 +311,85 @@ export function getMainThreadDynamicAttrState(
121
311
  return ref ? { kind: 'mt-ref', value: ref } : undefined;
122
312
  }
123
313
 
314
+ function commitSpreadSlotState(
315
+ handleId: number,
316
+ attrSlotIndex: number,
317
+ value: unknown,
318
+ isHydration: boolean,
319
+ cleanupBeforeRefHydrate = false,
320
+ ): { prevSpec: MTRefValue | null | undefined; nextSpec: MTRefValue | null | undefined } {
321
+ deleteHandleSlot(unhydratedSpreadMTRefSlots, handleId, attrSlotIndex);
322
+ const prevSpread = isHydration ? spreadSlotState.get(handleId)?.get(attrSlotIndex) : undefined;
323
+ const prevSpec = mtRefSlotSpecs.get(handleId)?.get(attrSlotIndex);
324
+
325
+ let nextEvents: Map<string, MTEventCtx> | undefined;
326
+ let nextGesture: GestureSlotValue | undefined;
327
+ let hasOwnMTRef = false;
328
+ let nextRefValue: MTRefValue | null = null;
329
+
330
+ if (isSpreadRecord(value)) {
331
+ for (const key of Object.keys(value)) {
332
+ const entry = value[key];
333
+ if (key === 'main-thread:ref') {
334
+ hasOwnMTRef = true;
335
+ nextRefValue = isMTRefNativeWrapper(entry) ? entry.value : null;
336
+ } else if (key === 'main-thread:gesture') {
337
+ if (isGestureSlotValue(entry)) {
338
+ nextGesture = entry;
339
+ }
340
+ } else if (isMainThreadEventPropKey(key) && isMTEventNativeWrapper(entry)) {
341
+ (nextEvents ??= new Map()).set(key, entry.value);
342
+ }
343
+ }
344
+ }
345
+
346
+ if (nextEvents || nextGesture) {
347
+ setSlotState(spreadSlotState, handleId, attrSlotIndex, {
348
+ events: nextEvents,
349
+ gesture: nextGesture,
350
+ });
351
+ } else {
352
+ deleteSlotState(spreadSlotState, handleId, attrSlotIndex);
353
+ }
354
+
355
+ const rawNextSpec = hasOwnMTRef ? nextRefValue : undefined;
356
+ const nextSpec = isSameMTRefSpec(prevSpec, rawNextSpec, isHydration) ? prevSpec : rawNextSpec;
357
+
358
+ if (cleanupBeforeRefHydrate && prevSpec !== nextSpec) {
359
+ cleanupAttachedElementsForSlot(handleId, attrSlotIndex);
360
+ }
361
+
362
+ if (isHydration) {
363
+ if (nextEvents && prevSpread?.events) {
364
+ for (const [eventKey, nextCtx] of nextEvents) {
365
+ const prevCtx = prevSpread.events.get(eventKey);
366
+ if (prevCtx) {
367
+ hydrateWorkletCtx(nextCtx as Worklet, prevCtx as Worklet);
368
+ }
369
+ }
370
+ }
371
+ if (nextGesture && prevSpread?.gesture) {
372
+ hydrateGestureCallbacks(nextGesture, prevSpread.gesture);
373
+ }
374
+ if (nextRefValue !== null) {
375
+ hydrateMTRefValue(nextRefValue, prevSpec);
376
+ }
377
+ }
378
+
379
+ if (nextSpec === undefined) {
380
+ deleteSlotState(mtRefSlotSpecs, handleId, attrSlotIndex);
381
+ } else {
382
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, nextSpec);
383
+ }
384
+
385
+ return { prevSpec, nextSpec };
386
+ }
387
+
124
388
  export function initializeMainThreadDynamicAttrSlots(
125
389
  handleId: number,
126
390
  templateType: string,
127
391
  attributeSlots: readonly unknown[] | null | undefined,
392
+ isCreatedByPatch = false,
128
393
  ): void {
129
394
  const slotKinds = getMainThreadDynamicAttrSlotKinds(templateType);
130
395
  if (!slotKinds) {
@@ -132,14 +397,44 @@ export function initializeMainThreadDynamicAttrSlots(
132
397
  }
133
398
  for (const [attrSlotIndex, kind] of slotKinds) {
134
399
  const value = attributeSlots?.[attrSlotIndex];
135
- if (kind === 'mt-event' && value != null) {
400
+ if (kind === 'gesture' && value != null) {
401
+ updateGestureAttrSlot(handleId, attrSlotIndex, value);
402
+ } else if (kind === 'mt-event' && value != null) {
136
403
  setMTEventSlotState(handleId, attrSlotIndex, (value as MTEventNativeWrapper).value);
137
- } else if (kind === 'mt-ref' && value != null) {
138
- setMTRefSlotState(handleId, attrSlotIndex, (value as MTRefNativeWrapper).value);
404
+ } else if (kind === 'mt-ref') {
405
+ if (value == null) {
406
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, null);
407
+ } else {
408
+ setDirectMTRefSlotState(handleId, attrSlotIndex, (value as MTRefNativeWrapper).value);
409
+ }
410
+ } else if (kind === 'spread') {
411
+ registerHandleSlot(spreadAttrSlots, handleId, attrSlotIndex);
412
+ const { nextSpec } = commitSpreadSlotState(handleId, attrSlotIndex, value, false);
413
+ if (!isCreatedByPatch && nextSpec !== undefined) {
414
+ registerHandleSlot(unhydratedSpreadMTRefSlots, handleId, attrSlotIndex);
415
+ }
139
416
  }
140
417
  }
141
418
  }
142
419
 
420
+ export function updateGestureAttrSlot(
421
+ handleId: number,
422
+ attrSlotIndex: number,
423
+ value: unknown,
424
+ isHydration = false,
425
+ ): void {
426
+ const previous = isHydration ? gestureAttrState.get(handleId)?.get(attrSlotIndex) : undefined;
427
+ if (value == null) {
428
+ deleteSlotState(gestureAttrState, handleId, attrSlotIndex);
429
+ return;
430
+ }
431
+ const next = value as GestureSlotValue;
432
+ if (previous) {
433
+ hydrateGestureCallbacks(next, previous);
434
+ }
435
+ setSlotState(gestureAttrState, handleId, attrSlotIndex, next);
436
+ }
437
+
143
438
  export function updateMainThreadEventAttrSlot(
144
439
  handleId: number,
145
440
  attrSlotIndex: number,
@@ -171,42 +466,144 @@ export function updateMainThreadRefAttrSlot(
171
466
  isHydration = false,
172
467
  ): void {
173
468
  const previousState = mtRefAttrState.get(handleId)?.get(attrSlotIndex);
174
- const isMaterialized = materializedMTRefHandleIds.has(handleId);
469
+ const isAttached = attachedMTRefHandles.has(handleId);
470
+ if (isAttached) {
471
+ cleanupAttachedElementsForSlot(handleId, attrSlotIndex);
472
+ }
175
473
  if (value == null) {
176
- if (previousState && isMaterialized) {
177
- cleanupMTRefValue(previousState);
178
- }
179
474
  deleteSlotState(mtRefAttrState, handleId, attrSlotIndex);
475
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, null);
180
476
  return;
181
477
  }
182
- if (previousState && isMaterialized) {
183
- cleanupMTRefValue(previousState);
184
- }
185
478
  const nextValue = (value as MTRefNativeWrapper).value;
186
- setMTRefSlotState(handleId, attrSlotIndex, nextValue);
479
+ setDirectMTRefSlotState(handleId, attrSlotIndex, nextValue);
187
480
  if (isHydration) {
188
481
  hydrateMTRefValue(nextValue, previousState);
189
482
  }
190
- if (isMaterialized) {
191
- attachMTRefValue(nextValue, nativeRef);
483
+ if (isAttached) {
484
+ pendingMTRefReconciliations.set(handleId, nativeRef);
192
485
  }
193
486
  }
194
487
 
488
+ export function updateSpreadAttrSlot(
489
+ handleId: number,
490
+ attrSlotIndex: number,
491
+ value: unknown,
492
+ nativeRef: ElementTemplateHandle,
493
+ isHydration = false,
494
+ ): void {
495
+ const isAttached = attachedMTRefHandles.has(handleId);
496
+ const { prevSpec, nextSpec } = commitSpreadSlotState(
497
+ handleId,
498
+ attrSlotIndex,
499
+ value,
500
+ isHydration,
501
+ isAttached,
502
+ );
503
+ if (prevSpec === nextSpec) {
504
+ return;
505
+ }
506
+ if (isAttached && hasNonNullMTRefSpec(handleId)) {
507
+ pendingMTRefReconciliations.set(handleId, nativeRef);
508
+ }
509
+ }
510
+
511
+ export function reconcileUnhydratedSpreadMTRefSlots(): void {
512
+ if (unhydratedSpreadMTRefSlots.size === 0) {
513
+ return;
514
+ }
515
+ for (const [handleId, slotIndices] of unhydratedSpreadMTRefSlots) {
516
+ for (const attrSlotIndex of slotIndices) {
517
+ cleanupAttachedElementsForSlot(handleId, attrSlotIndex);
518
+ deleteSlotState(mtRefSlotSpecs, handleId, attrSlotIndex);
519
+ }
520
+ const nativeRef = attachedMTRefHandles.get(handleId);
521
+ if (nativeRef && hasNonNullMTRefSpec(handleId)) {
522
+ pendingMTRefReconciliations.set(handleId, nativeRef);
523
+ }
524
+ }
525
+ unhydratedSpreadMTRefSlots.clear();
526
+ }
527
+
195
528
  export function attachMainThreadDynamicAttrRefsForSubtree(
196
529
  handles: readonly MainThreadDynamicAttrSubtreeHandle[],
197
530
  ): void {
198
531
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
199
532
  const handle = handles[handleIndex]!;
200
- if (materializedMTRefHandleIds.has(handle.uid)) {
533
+ if (attachedMTRefHandles.has(handle.uid)) {
201
534
  continue;
202
535
  }
203
- materializedMTRefHandleIds.add(handle.uid);
204
- const handleState = mtRefAttrState.get(handle.uid);
205
- if (!handleState) {
206
- continue;
536
+ attachedMTRefHandles.set(handle.uid, handle.ref);
537
+ if (hasNonNullMTRefSpec(handle.uid)) {
538
+ pendingMTRefReconciliations.set(handle.uid, handle.ref);
207
539
  }
208
- for (const value of handleState.values()) {
209
- attachMTRefValue(value, handle.ref);
540
+ }
541
+ }
542
+
543
+ // Consume one queue snapshot, matching Snapshot's ref delivery boundary.
544
+ export function applyPendingMainThreadRefAttachments(isHydration = false): void {
545
+ if (isHydration) {
546
+ reconcileUnhydratedSpreadMTRefSlots();
547
+ }
548
+ if (pendingMTRefReconciliations.size === 0) {
549
+ return;
550
+ }
551
+ const pending = pendingMTRefReconciliations;
552
+ pendingMTRefReconciliations = new Map();
553
+
554
+ for (const [handleId, nativeRef] of pending) {
555
+ const specs = mtRefSlotSpecs.get(handleId);
556
+ const desiredByElement = new Map<ElementRef, AttachedMTRefEntry | null>();
557
+ if (specs && specs.size > 0) {
558
+ const sortedEntries = specs.size === 1
559
+ ? Array.from(specs.entries())
560
+ : Array.from(specs.entries()).sort((a, b) => a[0] - b[0]);
561
+ for (const [slotIndex, refValue] of sortedEntries) {
562
+ const element = __GetElementByAttrSlot(nativeRef, slotIndex);
563
+ if (!element) {
564
+ throw new Error(
565
+ `ElementTemplate main-thread:ref target ${handleId}:${slotIndex} could not be materialized.`,
566
+ );
567
+ }
568
+ desiredByElement.set(
569
+ element,
570
+ refValue === null ? null : { slotIndex, value: refValue },
571
+ );
572
+ }
573
+ }
574
+
575
+ let attachedElements = attachedMTRefsByElement.get(handleId);
576
+ if (attachedElements) {
577
+ const staleValues: MTRefValue[] = [];
578
+ for (const [element, prevEntry] of attachedElements) {
579
+ const desired = desiredByElement.get(element) ?? null;
580
+ if (!desired || desired.slotIndex !== prevEntry.slotIndex || desired.value !== prevEntry.value) {
581
+ attachedElements.delete(element);
582
+ staleValues.push(prevEntry.value);
583
+ }
584
+ }
585
+ if (attachedElements.size === 0) {
586
+ attachedMTRefsByElement.delete(handleId);
587
+ attachedElements = undefined;
588
+ }
589
+ for (const staleValue of staleValues) {
590
+ cleanupMTRefValue(staleValue);
591
+ }
592
+ }
593
+
594
+ for (const [element, desired] of desiredByElement) {
595
+ if (!desired) {
596
+ continue;
597
+ }
598
+ if (attachedElements?.has(element)) {
599
+ continue;
600
+ }
601
+ attachMTRefValue(desired.value, element);
602
+ if (!attachedElements) {
603
+ attachedElements = new Map();
604
+ attachedMTRefsByElement.set(handleId, attachedElements);
605
+ }
606
+ attachedElements.set(element, desired);
210
607
  }
211
608
  }
212
609
  }
@@ -216,27 +613,36 @@ export function detachMainThreadDynamicAttrRefsForSubtree(
216
613
  ): void {
217
614
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
218
615
  const handleId = handles[handleIndex]!.uid;
219
- if (!materializedMTRefHandleIds.has(handleId)) {
616
+ if (!attachedMTRefHandles.has(handleId)) {
220
617
  continue;
221
618
  }
222
- materializedMTRefHandleIds.delete(handleId);
223
- const handleState = mtRefAttrState.get(handleId);
224
- if (handleState) {
225
- cleanupMTRefHandleState(handleState);
226
- }
619
+ attachedMTRefHandles.delete(handleId);
620
+ pendingMTRefReconciliations.delete(handleId);
621
+ cleanupAttachedElementsForHandle(handleId);
227
622
  }
228
623
  }
229
624
 
230
625
  export function deleteMainThreadDynamicAttrStateForSubtree(
231
626
  handleIds: readonly number[],
232
627
  ): void {
233
- for (const handleId of handleIds) {
234
- const handleState = mtRefAttrState.get(handleId);
235
- if (handleState && materializedMTRefHandleIds.has(handleId)) {
236
- cleanupMTRefHandleState(handleState);
237
- }
238
- materializedMTRefHandleIds.delete(handleId);
239
- mtEventAttrState.delete(handleId);
240
- mtRefAttrState.delete(handleId);
628
+ try {
629
+ for (const handleId of handleIds) {
630
+ if (attachedMTRefHandles.has(handleId)) {
631
+ cleanupAttachedElementsForHandle(handleId);
632
+ }
633
+ }
634
+ } finally {
635
+ for (const handleId of handleIds) {
636
+ attachedMTRefHandles.delete(handleId);
637
+ unhydratedSpreadMTRefSlots.delete(handleId);
638
+ pendingMTRefReconciliations.delete(handleId);
639
+ attachedMTRefsByElement.delete(handleId);
640
+ gestureAttrState.delete(handleId);
641
+ mtEventAttrState.delete(handleId);
642
+ mtRefAttrState.delete(handleId);
643
+ mtRefSlotSpecs.delete(handleId);
644
+ spreadAttrSlots.delete(handleId);
645
+ spreadSlotState.delete(handleId);
646
+ }
241
647
  }
242
648
  }
@@ -24,6 +24,14 @@ export function isMTRefValue(value: unknown): value is MTRefValue {
24
24
  return isMainThreadRef(value) || isMainThreadFunction(value);
25
25
  }
26
26
 
27
+ export function isMTRefNativeWrapper(value: unknown): value is MTRefNativeWrapper {
28
+ return value !== null
29
+ && typeof value === 'object'
30
+ && !Array.isArray(value)
31
+ && (value as { type?: unknown }).type === 'main-thread-ref'
32
+ && isMTRefValue((value as { value?: unknown }).value);
33
+ }
34
+
27
35
  export function retainMTRefValue(value: MTRefValue): void {
28
36
  if (isMainThreadFunction(value)) {
29
37
  retainMainThreadBackgroundFunctionCtx(value);
@@ -32,7 +40,7 @@ export function retainMTRefValue(value: MTRefValue): void {
32
40
 
33
41
  export function hydrateMTRefValue(
34
42
  value: MTRefValue,
35
- previousValue: MTRefValue | undefined,
43
+ previousValue: MTRefValue | null | undefined,
36
44
  ): void {
37
45
  if (isMainThreadFunction(value) && previousValue && isMainThreadFunction(previousValue)) {
38
46
  hydrateWorkletCtx(value, previousValue);
@@ -41,10 +49,10 @@ export function hydrateMTRefValue(
41
49
 
42
50
  export function attachMTRefValue(
43
51
  value: MTRefValue,
44
- nativeRef: ElementTemplateHandle,
52
+ nativeRef: ElementRef,
45
53
  ): void {
46
54
  if (isMainThreadRef(value)) {
47
- updateWorkletRef(value, nativeRef as unknown as ElementNode);
55
+ updateWorkletRef(value, nativeRef);
48
56
  return;
49
57
  }
50
58
  value._unmount = runWorkletCtx(
@@ -59,7 +67,9 @@ export function cleanupMTRefValue(value: MTRefValue): void {
59
67
  return;
60
68
  }
61
69
  if (typeof value._unmount === 'function') {
62
- value._unmount();
70
+ const unmount = value._unmount;
71
+ delete value._unmount;
72
+ unmount();
63
73
  return;
64
74
  }
65
75
  runWorkletCtx(value, [null]);
@@ -2,7 +2,7 @@
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 { adaptSpreadAttrSlot } from './attr-slot-plan.js';
5
+ import { adaptTypedSpreadAttrSlot } from './attr-slot-plan.js';
6
6
  import type { EtAttrPlan } from './attr-slot-plan.js';
7
7
  import type { RuntimeTypedElementAttributes, TypedElementAttributesCommand } from '../../protocol/types.js';
8
8
 
@@ -10,14 +10,14 @@ export const TYPED_ELEMENT_ATTRIBUTES_SLOT_INDEX = 0;
10
10
 
11
11
  export const TYPED_ELEMENT_ATTR_PLAN: EtAttrPlan = [
12
12
  TYPED_ELEMENT_ATTRIBUTES_SLOT_INDEX,
13
- adaptSpreadAttrSlot,
13
+ adaptTypedSpreadAttrSlot,
14
14
  ];
15
15
 
16
16
  export function prepareTypedElementAttributes(
17
17
  handleId: number,
18
18
  attributes: RuntimeTypedElementAttributes | null | undefined,
19
19
  ): TypedElementAttributesCommand | null {
20
- return adaptSpreadAttrSlot(
20
+ return adaptTypedSpreadAttrSlot(
21
21
  handleId,
22
22
  TYPED_ELEMENT_ATTRIBUTES_SLOT_INDEX,
23
23
  attributes,