@lynx-js/react 0.129.1 → 0.129.3

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 (129) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +1 -1
  4. package/refresh/.turbo/turbo-build.log +1 -1
  5. package/runtime/__test__/core/globalProps.test.ts +27 -6
  6. package/runtime/__test__/element-template/fixtures/gesture/direct/index.tsx +33 -0
  7. package/runtime/__test__/element-template/fixtures/spread/mts/index.tsx +197 -0
  8. package/runtime/__test__/element-template/native/callDestroyLifetimeFun.test.ts +89 -3
  9. package/runtime/__test__/element-template/native/mts-destroy.test.ts +8 -3
  10. package/runtime/__test__/element-template/native/reload.test.ts +87 -0
  11. package/runtime/__test__/element-template/runtime/background/global-props/compiled-fixtures.test.tsx +5 -5
  12. package/runtime/__test__/element-template/runtime/background/global-props.test.tsx +1 -1
  13. package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +70 -0
  14. package/runtime/__test__/element-template/runtime/background/ref/compiled-fixtures.test.tsx +3 -4
  15. package/runtime/__test__/element-template/runtime/background/reload.test.ts +74 -0
  16. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +15 -0
  17. package/runtime/__test__/element-template/runtime/prop-adapters/gesture-compiled.test.ts +200 -0
  18. package/runtime/__test__/element-template/runtime/prop-adapters/gesture.test.ts +420 -0
  19. package/runtime/__test__/element-template/runtime/prop-adapters/spread-mts-compiled.test.ts +527 -0
  20. package/runtime/__test__/element-template/runtime/prop-adapters/spread.test.ts +185 -6
  21. package/runtime/__test__/element-template/runtime/render/render-direct-native.test.tsx +21 -1
  22. package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +188 -0
  23. package/runtime/__test__/snapshot/compat/export.test.jsx +8 -2
  24. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.event.test.jsx +2 -2
  25. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.test.jsx +3 -3
  26. package/runtime/__test__/snapshot/ref.test.jsx +30 -0
  27. package/runtime/__test__/worklet-runtime/initEntry.test.js +12 -0
  28. package/runtime/compat/index.d.ts +17 -1
  29. package/runtime/compat/index.js +3 -2
  30. package/runtime/lazy/compat.js +1 -0
  31. package/runtime/lazy/element-template-internal.js +1 -0
  32. package/runtime/lib/core/globalProps.js +1 -1
  33. package/runtime/lib/core/globalProps.js.map +1 -1
  34. package/runtime/lib/element-template/background/attr-slots.js +5 -2
  35. package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
  36. package/runtime/lib/element-template/background/destroy.js +16 -11
  37. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  38. package/runtime/lib/element-template/background/hydrate.js +23 -0
  39. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  40. package/runtime/lib/element-template/debug/alog.d.ts +1 -1
  41. package/runtime/lib/element-template/debug/alog.js +4 -1
  42. package/runtime/lib/element-template/debug/alog.js.map +1 -1
  43. package/runtime/lib/element-template/internal.d.ts +1 -1
  44. package/runtime/lib/element-template/internal.js +1 -1
  45. package/runtime/lib/element-template/internal.js.map +1 -1
  46. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js +6 -1
  47. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
  48. package/runtime/lib/element-template/native/patch-listener.js +1 -1
  49. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  50. package/runtime/lib/element-template/native/reload-background.js +1 -1
  51. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  52. package/runtime/lib/element-template/native/reload-main-thread.js +33 -9
  53. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  54. package/runtime/lib/element-template/prop-adapters/gesture.d.ts +23 -0
  55. package/runtime/lib/element-template/prop-adapters/gesture.js +97 -0
  56. package/runtime/lib/element-template/prop-adapters/gesture.js.map +1 -0
  57. package/runtime/lib/element-template/prop-adapters/spread.d.ts +6 -1
  58. package/runtime/lib/element-template/prop-adapters/spread.js +72 -2
  59. package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
  60. package/runtime/lib/element-template/protocol/opcodes.d.ts +1 -0
  61. package/runtime/lib/element-template/protocol/opcodes.js +1 -0
  62. package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
  63. package/runtime/lib/element-template/protocol/types.d.ts +7 -1
  64. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  65. package/runtime/lib/element-template/runtime/patch.js +27 -2
  66. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  67. package/runtime/lib/element-template/runtime/template/attr-slot-plan.d.ts +4 -2
  68. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +17 -4
  69. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
  70. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +7 -2
  71. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +368 -58
  72. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  73. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +2 -1
  74. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js +10 -1
  75. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  76. package/runtime/lib/element-template/runtime/template/typed-attributes.js +3 -3
  77. package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
  78. package/runtime/lib/snapshot/lifecycle/destroy.js +23 -17
  79. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  80. package/runtime/lib/snapshot/lifecycle/ref/delay.d.ts +5 -0
  81. package/runtime/lib/snapshot/lifecycle/ref/delay.js +1 -1
  82. package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
  83. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  84. package/runtime/lib/worklet-runtime/utils/version.js +4 -1
  85. package/runtime/lib/worklet-runtime/utils/version.js.map +1 -1
  86. package/runtime/src/core/globalProps.ts +1 -1
  87. package/runtime/src/element-template/background/attr-slots.ts +5 -1
  88. package/runtime/src/element-template/background/destroy.ts +16 -13
  89. package/runtime/src/element-template/background/hydrate.ts +23 -0
  90. package/runtime/src/element-template/debug/alog.ts +5 -2
  91. package/runtime/src/element-template/internal.ts +1 -0
  92. package/runtime/src/element-template/native/callDestroyLifetimeFun.ts +5 -1
  93. package/runtime/src/element-template/native/patch-listener.ts +1 -1
  94. package/runtime/src/element-template/native/reload-background.ts +1 -1
  95. package/runtime/src/element-template/native/reload-main-thread.ts +30 -10
  96. package/runtime/src/element-template/prop-adapters/gesture.ts +144 -0
  97. package/runtime/src/element-template/prop-adapters/spread.ts +110 -2
  98. package/runtime/src/element-template/protocol/opcodes.ts +1 -0
  99. package/runtime/src/element-template/protocol/types.ts +8 -0
  100. package/runtime/src/element-template/runtime/patch.ts +33 -0
  101. package/runtime/src/element-template/runtime/template/attr-slot-plan.ts +31 -4
  102. package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +441 -65
  103. package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +12 -2
  104. package/runtime/src/element-template/runtime/template/typed-attributes.ts +3 -3
  105. package/runtime/src/snapshot/lifecycle/destroy.ts +23 -18
  106. package/runtime/src/snapshot/lifecycle/ref/delay.ts +1 -1
  107. package/runtime/src/worklet-runtime/utils/version.ts +4 -1
  108. package/runtime/types/types.d.ts +2 -1
  109. package/testing-library/.turbo/turbo-build.log +7 -7
  110. package/transform/.turbo/turbo-build$colon$wasm.log +72 -71
  111. package/transform/__test__/fixture.spec.js +11 -11
  112. package/transform/__test__/main-thread-object-compat.spec.js +111 -0
  113. package/transform/crates/swc_plugin_element_template/lib.rs +11 -0
  114. package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +113 -0
  115. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +7 -9
  116. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +3 -3
  117. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +3 -3
  118. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +7 -7
  119. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +7 -7
  120. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +8 -8
  121. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +8 -8
  122. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +5 -5
  123. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +5 -5
  124. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +1 -1
  125. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +1 -1
  126. package/transform/dist/wasm.cjs +1 -1
  127. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +97 -97
  128. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  129. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +177 -178
@@ -4,11 +4,22 @@
4
4
  import { hydrateWorkletCtx } from '@lynx-js/react/worklet-runtime/bindings';
5
5
  import { getMainThreadDynamicAttrSlotKinds } from './attr-slot-plan.js';
6
6
  import { retainMainThreadBackgroundFunctionCtx } from './main-thread-background-function.js';
7
- import { attachMTRefValue, cleanupMTRefValue, hydrateMTRefValue, retainMTRefValue } from './main-thread-ref-ctx.js';
7
+ import { isMTEventNativeWrapper } from './main-thread-event-ctx.js';
8
+ import { attachMTRefValue, cleanupMTRefValue, hydrateMTRefValue, isMTRefNativeWrapper, retainMTRefValue, } from './main-thread-ref-ctx.js';
9
+ import { isMainThreadFunction } from '../../../core/main-thread-function.js';
10
+ import { isMainThreadRef } from '../../../core/main-thread-ref.js';
11
+ import { hydrateGestureCallbacks, isGestureSlotValue, retainGestureCallbacks } from '../../prop-adapters/gesture.js';
12
+ import { isMainThreadEventPropKey, isSpreadRecord } from '../../prop-adapters/spread.js';
13
+ const gestureAttrState = new Map();
8
14
  const mtEventAttrState = new Map();
9
15
  const mtRefAttrState = new Map();
10
- const attachedMTRefHandleIds = new Set();
11
- let pendingMTRefAttachments = [];
16
+ const mtRefSlotSpecs = new Map();
17
+ const spreadAttrSlots = new Map();
18
+ const spreadSlotState = new Map();
19
+ const unhydratedSpreadMTRefSlots = new Map();
20
+ const attachedMTRefHandles = new Map();
21
+ const attachedMTRefsByElement = new Map();
22
+ let pendingMTRefReconciliations = new Map();
12
23
  function deleteSlotState(state, handleId, attrSlotIndex) {
13
24
  const handleState = state.get(handleId);
14
25
  if (!handleState) {
@@ -19,11 +30,6 @@ function deleteSlotState(state, handleId, attrSlotIndex) {
19
30
  state.delete(handleId);
20
31
  }
21
32
  }
22
- function cleanupMTRefHandleState(handleState) {
23
- for (const value of handleState.values()) {
24
- cleanupMTRefValue(value);
25
- }
26
- }
27
33
  function setSlotState(state, handleId, attrSlotIndex, value) {
28
34
  let handleState = state.get(handleId);
29
35
  if (!handleState) {
@@ -32,12 +38,131 @@ function setSlotState(state, handleId, attrSlotIndex, value) {
32
38
  }
33
39
  handleState.set(attrSlotIndex, value);
34
40
  }
41
+ function registerHandleSlot(slotsByHandle, handleId, attrSlotIndex) {
42
+ let slots = slotsByHandle.get(handleId);
43
+ if (!slots) {
44
+ slots = new Set();
45
+ slotsByHandle.set(handleId, slots);
46
+ }
47
+ slots.add(attrSlotIndex);
48
+ }
49
+ function deleteHandleSlot(slotsByHandle, handleId, attrSlotIndex) {
50
+ const slots = slotsByHandle.get(handleId);
51
+ if (!slots) {
52
+ return;
53
+ }
54
+ slots.delete(attrSlotIndex);
55
+ if (slots.size === 0) {
56
+ slotsByHandle.delete(handleId);
57
+ }
58
+ }
59
+ function hasNonNullMTRefSpec(handleId) {
60
+ const specs = mtRefSlotSpecs.get(handleId);
61
+ if (!specs) {
62
+ return false;
63
+ }
64
+ for (const value of specs.values()) {
65
+ if (value !== null) {
66
+ return true;
67
+ }
68
+ }
69
+ return false;
70
+ }
71
+ function isSameMTRefSpec(prev, next, isHydration) {
72
+ if (prev === next) {
73
+ return true;
74
+ }
75
+ if (prev == null || next == null) {
76
+ return false;
77
+ }
78
+ if (isMainThreadRef(prev) && isMainThreadRef(next)) {
79
+ return prev._wvid === next._wvid;
80
+ }
81
+ return !isHydration
82
+ && isMainThreadFunction(prev)
83
+ && isMainThreadFunction(next)
84
+ && prev._execId !== undefined
85
+ && prev._execId === next._execId;
86
+ }
87
+ function cleanupAttachedElementsForHandle(handleId) {
88
+ const attachedElements = attachedMTRefsByElement.get(handleId);
89
+ if (!attachedElements) {
90
+ return;
91
+ }
92
+ attachedMTRefsByElement.delete(handleId);
93
+ for (const entry of attachedElements.values()) {
94
+ cleanupMTRefValue(entry.value);
95
+ }
96
+ }
97
+ function cleanupAttachedElementsForSlot(handleId, attrSlotIndex) {
98
+ const attachedElements = attachedMTRefsByElement.get(handleId);
99
+ if (!attachedElements) {
100
+ return;
101
+ }
102
+ const toCleanup = [];
103
+ for (const [element, entry] of attachedElements) {
104
+ if (entry.slotIndex === attrSlotIndex) {
105
+ attachedElements.delete(element);
106
+ toCleanup.push(entry.value);
107
+ }
108
+ }
109
+ if (attachedElements.size === 0) {
110
+ attachedMTRefsByElement.delete(handleId);
111
+ }
112
+ for (const value of toCleanup) {
113
+ cleanupMTRefValue(value);
114
+ }
115
+ }
35
116
  function setMTEventSlotState(handleId, attrSlotIndex, nativeHeldValue) {
36
117
  setSlotState(mtEventAttrState, handleId, attrSlotIndex, nativeHeldValue);
37
118
  }
38
- function setMTRefSlotState(handleId, attrSlotIndex, value) {
119
+ function setDirectMTRefSlotState(handleId, attrSlotIndex, value) {
39
120
  retainMTRefValue(value);
40
121
  setSlotState(mtRefAttrState, handleId, attrSlotIndex, value);
122
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, value);
123
+ }
124
+ export function isSpreadDynamicAttrSlot(handleId, attrSlotIndex) {
125
+ return spreadAttrSlots.get(handleId)?.has(attrSlotIndex) === true;
126
+ }
127
+ export function prepareSpreadAttrSlotForNative(value) {
128
+ if (!isSpreadRecord(value)) {
129
+ return null;
130
+ }
131
+ const keys = Object.keys(value);
132
+ let hasOwnMTRef = false;
133
+ let activeMTRefWrapper;
134
+ for (const key of keys) {
135
+ const entry = value[key];
136
+ if (key === 'main-thread:ref') {
137
+ hasOwnMTRef = true;
138
+ if (isMTRefNativeWrapper(entry)) {
139
+ activeMTRefWrapper = entry;
140
+ retainMTRefValue(entry.value);
141
+ }
142
+ }
143
+ else if (key === 'main-thread:gesture') {
144
+ if (isGestureSlotValue(entry)) {
145
+ retainGestureCallbacks(entry);
146
+ }
147
+ }
148
+ else if (isMainThreadEventPropKey(key) && isMTEventNativeWrapper(entry)) {
149
+ retainMainThreadBackgroundFunctionCtx(entry.value);
150
+ }
151
+ }
152
+ if (!hasOwnMTRef) {
153
+ return value;
154
+ }
155
+ const nativeSpread = {};
156
+ for (const key of keys) {
157
+ if (key !== 'main-thread:ref') {
158
+ nativeSpread[key] = value[key];
159
+ }
160
+ }
161
+ if (activeMTRefWrapper) {
162
+ nativeSpread['has-react-ref'] = true;
163
+ nativeSpread['flatten'] = false;
164
+ }
165
+ return nativeSpread;
41
166
  }
42
167
  export function prepareMainThreadDynamicAttrSlotsForNative(templateType, attributeSlots) {
43
168
  if (attributeSlots == null) {
@@ -49,8 +174,21 @@ export function prepareMainThreadDynamicAttrSlotsForNative(templateType, attribu
49
174
  }
50
175
  let nativeSlots;
51
176
  for (const [attrSlotIndex, kind] of slotKinds) {
177
+ if (kind === 'gesture' && attributeSlots[attrSlotIndex] != null) {
178
+ retainGestureCallbacks(attributeSlots[attrSlotIndex]);
179
+ }
52
180
  if (kind === 'mt-event' && attributeSlots[attrSlotIndex] != null) {
53
181
  retainMainThreadBackgroundFunctionCtx(attributeSlots[attrSlotIndex].value);
182
+ continue;
183
+ }
184
+ if (kind === 'spread') {
185
+ const slotValue = attributeSlots[attrSlotIndex];
186
+ const nativeSpread = prepareSpreadAttrSlotForNative(slotValue);
187
+ if (nativeSpread !== slotValue) {
188
+ nativeSlots ??= attributeSlots.slice();
189
+ nativeSlots[attrSlotIndex] = nativeSpread;
190
+ }
191
+ continue;
54
192
  }
55
193
  if (kind !== 'mt-ref') {
56
194
  continue;
@@ -61,18 +199,23 @@ export function prepareMainThreadDynamicAttrSlotsForNative(templateType, attribu
61
199
  return nativeSlots ?? attributeSlots;
62
200
  }
63
201
  export function clearMainThreadDynamicAttrState() {
202
+ const attachedHandleIds = Array.from(attachedMTRefHandles.keys());
203
+ attachedMTRefHandles.clear();
204
+ unhydratedSpreadMTRefSlots.clear();
205
+ pendingMTRefReconciliations.clear();
64
206
  try {
65
- for (const [handleId, handleState] of mtRefAttrState) {
66
- if (attachedMTRefHandleIds.has(handleId)) {
67
- cleanupMTRefHandleState(handleState);
68
- }
207
+ for (const handleId of attachedHandleIds) {
208
+ cleanupAttachedElementsForHandle(handleId);
69
209
  }
70
210
  }
71
211
  finally {
212
+ gestureAttrState.clear();
72
213
  mtEventAttrState.clear();
73
214
  mtRefAttrState.clear();
74
- attachedMTRefHandleIds.clear();
75
- pendingMTRefAttachments = [];
215
+ mtRefSlotSpecs.clear();
216
+ spreadAttrSlots.clear();
217
+ spreadSlotState.clear();
218
+ attachedMTRefsByElement.clear();
76
219
  }
77
220
  }
78
221
  export function getMainThreadDynamicAttrState(handleId, attrSlotIndex) {
@@ -83,21 +226,111 @@ export function getMainThreadDynamicAttrState(handleId, attrSlotIndex) {
83
226
  const ref = mtRefAttrState.get(handleId)?.get(attrSlotIndex);
84
227
  return ref ? { kind: 'mt-ref', value: ref } : undefined;
85
228
  }
86
- export function initializeMainThreadDynamicAttrSlots(handleId, templateType, attributeSlots) {
229
+ function commitSpreadSlotState(handleId, attrSlotIndex, value, isHydration, cleanupBeforeRefHydrate = false) {
230
+ deleteHandleSlot(unhydratedSpreadMTRefSlots, handleId, attrSlotIndex);
231
+ const prevSpread = isHydration ? spreadSlotState.get(handleId)?.get(attrSlotIndex) : undefined;
232
+ const prevSpec = mtRefSlotSpecs.get(handleId)?.get(attrSlotIndex);
233
+ let nextEvents;
234
+ let nextGesture;
235
+ let hasOwnMTRef = false;
236
+ let nextRefValue = null;
237
+ if (isSpreadRecord(value)) {
238
+ for (const key of Object.keys(value)) {
239
+ const entry = value[key];
240
+ if (key === 'main-thread:ref') {
241
+ hasOwnMTRef = true;
242
+ nextRefValue = isMTRefNativeWrapper(entry) ? entry.value : null;
243
+ }
244
+ else if (key === 'main-thread:gesture') {
245
+ if (isGestureSlotValue(entry)) {
246
+ nextGesture = entry;
247
+ }
248
+ }
249
+ else if (isMainThreadEventPropKey(key) && isMTEventNativeWrapper(entry)) {
250
+ (nextEvents ??= new Map()).set(key, entry.value);
251
+ }
252
+ }
253
+ }
254
+ if (nextEvents || nextGesture) {
255
+ setSlotState(spreadSlotState, handleId, attrSlotIndex, {
256
+ events: nextEvents,
257
+ gesture: nextGesture,
258
+ });
259
+ }
260
+ else {
261
+ deleteSlotState(spreadSlotState, handleId, attrSlotIndex);
262
+ }
263
+ const rawNextSpec = hasOwnMTRef ? nextRefValue : undefined;
264
+ const nextSpec = isSameMTRefSpec(prevSpec, rawNextSpec, isHydration) ? prevSpec : rawNextSpec;
265
+ if (cleanupBeforeRefHydrate && prevSpec !== nextSpec) {
266
+ cleanupAttachedElementsForSlot(handleId, attrSlotIndex);
267
+ }
268
+ if (isHydration) {
269
+ if (nextEvents && prevSpread?.events) {
270
+ for (const [eventKey, nextCtx] of nextEvents) {
271
+ const prevCtx = prevSpread.events.get(eventKey);
272
+ if (prevCtx) {
273
+ hydrateWorkletCtx(nextCtx, prevCtx);
274
+ }
275
+ }
276
+ }
277
+ if (nextGesture && prevSpread?.gesture) {
278
+ hydrateGestureCallbacks(nextGesture, prevSpread.gesture);
279
+ }
280
+ if (nextRefValue !== null) {
281
+ hydrateMTRefValue(nextRefValue, prevSpec);
282
+ }
283
+ }
284
+ if (nextSpec === undefined) {
285
+ deleteSlotState(mtRefSlotSpecs, handleId, attrSlotIndex);
286
+ }
287
+ else {
288
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, nextSpec);
289
+ }
290
+ return { prevSpec, nextSpec };
291
+ }
292
+ export function initializeMainThreadDynamicAttrSlots(handleId, templateType, attributeSlots, isCreatedByPatch = false) {
87
293
  const slotKinds = getMainThreadDynamicAttrSlotKinds(templateType);
88
294
  if (!slotKinds) {
89
295
  return;
90
296
  }
91
297
  for (const [attrSlotIndex, kind] of slotKinds) {
92
298
  const value = attributeSlots?.[attrSlotIndex];
93
- if (kind === 'mt-event' && value != null) {
299
+ if (kind === 'gesture' && value != null) {
300
+ updateGestureAttrSlot(handleId, attrSlotIndex, value);
301
+ }
302
+ else if (kind === 'mt-event' && value != null) {
94
303
  setMTEventSlotState(handleId, attrSlotIndex, value.value);
95
304
  }
96
- else if (kind === 'mt-ref' && value != null) {
97
- setMTRefSlotState(handleId, attrSlotIndex, value.value);
305
+ else if (kind === 'mt-ref') {
306
+ if (value == null) {
307
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, null);
308
+ }
309
+ else {
310
+ setDirectMTRefSlotState(handleId, attrSlotIndex, value.value);
311
+ }
312
+ }
313
+ else if (kind === 'spread') {
314
+ registerHandleSlot(spreadAttrSlots, handleId, attrSlotIndex);
315
+ const { nextSpec } = commitSpreadSlotState(handleId, attrSlotIndex, value, false);
316
+ if (!isCreatedByPatch && nextSpec !== undefined) {
317
+ registerHandleSlot(unhydratedSpreadMTRefSlots, handleId, attrSlotIndex);
318
+ }
98
319
  }
99
320
  }
100
321
  }
322
+ export function updateGestureAttrSlot(handleId, attrSlotIndex, value, isHydration = false) {
323
+ const previous = isHydration ? gestureAttrState.get(handleId)?.get(attrSlotIndex) : undefined;
324
+ if (value == null) {
325
+ deleteSlotState(gestureAttrState, handleId, attrSlotIndex);
326
+ return;
327
+ }
328
+ const next = value;
329
+ if (previous) {
330
+ hydrateGestureCallbacks(next, previous);
331
+ }
332
+ setSlotState(gestureAttrState, handleId, attrSlotIndex, next);
333
+ }
101
334
  export function updateMainThreadEventAttrSlot(handleId, attrSlotIndex, value, isHydration = false) {
102
335
  const previousState = isHydration
103
336
  ? mtEventAttrState.get(handleId)?.get(attrSlotIndex)
@@ -114,76 +347,153 @@ export function updateMainThreadEventAttrSlot(handleId, attrSlotIndex, value, is
114
347
  }
115
348
  export function updateMainThreadRefAttrSlot(handleId, attrSlotIndex, value, nativeRef, isHydration = false) {
116
349
  const previousState = mtRefAttrState.get(handleId)?.get(attrSlotIndex);
117
- const isAttached = attachedMTRefHandleIds.has(handleId);
350
+ const isAttached = attachedMTRefHandles.has(handleId);
351
+ if (isAttached) {
352
+ cleanupAttachedElementsForSlot(handleId, attrSlotIndex);
353
+ }
118
354
  if (value == null) {
119
- if (previousState && isAttached) {
120
- cleanupMTRefValue(previousState);
121
- }
122
355
  deleteSlotState(mtRefAttrState, handleId, attrSlotIndex);
356
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, null);
123
357
  return;
124
358
  }
125
- if (previousState && isAttached) {
126
- cleanupMTRefValue(previousState);
127
- }
128
359
  const nextValue = value.value;
129
- setMTRefSlotState(handleId, attrSlotIndex, nextValue);
360
+ setDirectMTRefSlotState(handleId, attrSlotIndex, nextValue);
130
361
  if (isHydration) {
131
362
  hydrateMTRefValue(nextValue, previousState);
132
363
  }
133
364
  if (isAttached) {
134
- pendingMTRefAttachments.push({ handleId, attrSlotIndex, value: nextValue, nativeRef });
365
+ pendingMTRefReconciliations.set(handleId, nativeRef);
135
366
  }
136
367
  }
368
+ export function updateSpreadAttrSlot(handleId, attrSlotIndex, value, nativeRef, isHydration = false) {
369
+ const isAttached = attachedMTRefHandles.has(handleId);
370
+ const { prevSpec, nextSpec } = commitSpreadSlotState(handleId, attrSlotIndex, value, isHydration, isAttached);
371
+ if (prevSpec === nextSpec) {
372
+ return;
373
+ }
374
+ if (isAttached && hasNonNullMTRefSpec(handleId)) {
375
+ pendingMTRefReconciliations.set(handleId, nativeRef);
376
+ }
377
+ }
378
+ export function reconcileUnhydratedSpreadMTRefSlots() {
379
+ if (unhydratedSpreadMTRefSlots.size === 0) {
380
+ return;
381
+ }
382
+ for (const [handleId, slotIndices] of unhydratedSpreadMTRefSlots) {
383
+ for (const attrSlotIndex of slotIndices) {
384
+ cleanupAttachedElementsForSlot(handleId, attrSlotIndex);
385
+ deleteSlotState(mtRefSlotSpecs, handleId, attrSlotIndex);
386
+ }
387
+ const nativeRef = attachedMTRefHandles.get(handleId);
388
+ if (nativeRef && hasNonNullMTRefSpec(handleId)) {
389
+ pendingMTRefReconciliations.set(handleId, nativeRef);
390
+ }
391
+ }
392
+ unhydratedSpreadMTRefSlots.clear();
393
+ }
137
394
  export function attachMainThreadDynamicAttrRefsForSubtree(handles) {
138
395
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
139
396
  const handle = handles[handleIndex];
140
- if (attachedMTRefHandleIds.has(handle.uid)) {
141
- continue;
142
- }
143
- attachedMTRefHandleIds.add(handle.uid);
144
- const handleState = mtRefAttrState.get(handle.uid);
145
- if (!handleState) {
397
+ if (attachedMTRefHandles.has(handle.uid)) {
146
398
  continue;
147
399
  }
148
- for (const [attrSlotIndex, value] of handleState) {
149
- pendingMTRefAttachments.push({ handleId: handle.uid, attrSlotIndex, value, nativeRef: handle.ref });
400
+ attachedMTRefHandles.set(handle.uid, handle.ref);
401
+ if (hasNonNullMTRefSpec(handle.uid)) {
402
+ pendingMTRefReconciliations.set(handle.uid, handle.ref);
150
403
  }
151
404
  }
152
405
  }
153
406
  // Consume one queue snapshot, matching Snapshot's ref delivery boundary.
154
- export function applyPendingMainThreadRefAttachments() {
155
- const pending = pendingMTRefAttachments;
156
- pendingMTRefAttachments = [];
157
- for (const { handleId, attrSlotIndex, value, nativeRef } of pending) {
158
- const element = __GetElementByAttrSlot(nativeRef, attrSlotIndex);
159
- if (!element) {
160
- throw new Error(`ElementTemplate main-thread:ref target ${handleId}:${attrSlotIndex} could not be materialized.`);
407
+ export function applyPendingMainThreadRefAttachments(isHydration = false) {
408
+ if (isHydration) {
409
+ reconcileUnhydratedSpreadMTRefSlots();
410
+ }
411
+ if (pendingMTRefReconciliations.size === 0) {
412
+ return;
413
+ }
414
+ const pending = pendingMTRefReconciliations;
415
+ pendingMTRefReconciliations = new Map();
416
+ for (const [handleId, nativeRef] of pending) {
417
+ const specs = mtRefSlotSpecs.get(handleId);
418
+ const desiredByElement = new Map();
419
+ if (specs && specs.size > 0) {
420
+ const sortedEntries = specs.size === 1
421
+ ? Array.from(specs.entries())
422
+ : Array.from(specs.entries()).sort((a, b) => a[0] - b[0]);
423
+ for (const [slotIndex, refValue] of sortedEntries) {
424
+ const element = __GetElementByAttrSlot(nativeRef, slotIndex);
425
+ if (!element) {
426
+ throw new Error(`ElementTemplate main-thread:ref target ${handleId}:${slotIndex} could not be materialized.`);
427
+ }
428
+ desiredByElement.set(element, refValue === null ? null : { slotIndex, value: refValue });
429
+ }
430
+ }
431
+ let attachedElements = attachedMTRefsByElement.get(handleId);
432
+ if (attachedElements) {
433
+ const staleValues = [];
434
+ for (const [element, prevEntry] of attachedElements) {
435
+ const desired = desiredByElement.get(element) ?? null;
436
+ if (!desired || desired.slotIndex !== prevEntry.slotIndex || desired.value !== prevEntry.value) {
437
+ attachedElements.delete(element);
438
+ staleValues.push(prevEntry.value);
439
+ }
440
+ }
441
+ if (attachedElements.size === 0) {
442
+ attachedMTRefsByElement.delete(handleId);
443
+ attachedElements = undefined;
444
+ }
445
+ for (const staleValue of staleValues) {
446
+ cleanupMTRefValue(staleValue);
447
+ }
448
+ }
449
+ for (const [element, desired] of desiredByElement) {
450
+ if (!desired) {
451
+ continue;
452
+ }
453
+ if (attachedElements?.has(element)) {
454
+ continue;
455
+ }
456
+ attachMTRefValue(desired.value, element);
457
+ if (!attachedElements) {
458
+ attachedElements = new Map();
459
+ attachedMTRefsByElement.set(handleId, attachedElements);
460
+ }
461
+ attachedElements.set(element, desired);
161
462
  }
162
- attachMTRefValue(value, element);
163
463
  }
164
464
  }
165
465
  export function detachMainThreadDynamicAttrRefsForSubtree(handles) {
166
466
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
167
467
  const handleId = handles[handleIndex].uid;
168
- if (!attachedMTRefHandleIds.has(handleId)) {
468
+ if (!attachedMTRefHandles.has(handleId)) {
169
469
  continue;
170
470
  }
171
- attachedMTRefHandleIds.delete(handleId);
172
- const handleState = mtRefAttrState.get(handleId);
173
- if (handleState) {
174
- cleanupMTRefHandleState(handleState);
175
- }
471
+ attachedMTRefHandles.delete(handleId);
472
+ pendingMTRefReconciliations.delete(handleId);
473
+ cleanupAttachedElementsForHandle(handleId);
176
474
  }
177
475
  }
178
476
  export function deleteMainThreadDynamicAttrStateForSubtree(handleIds) {
179
- for (const handleId of handleIds) {
180
- const handleState = mtRefAttrState.get(handleId);
181
- if (handleState && attachedMTRefHandleIds.has(handleId)) {
182
- cleanupMTRefHandleState(handleState);
183
- }
184
- attachedMTRefHandleIds.delete(handleId);
185
- mtEventAttrState.delete(handleId);
186
- mtRefAttrState.delete(handleId);
477
+ try {
478
+ for (const handleId of handleIds) {
479
+ if (attachedMTRefHandles.has(handleId)) {
480
+ cleanupAttachedElementsForHandle(handleId);
481
+ }
482
+ }
483
+ }
484
+ finally {
485
+ for (const handleId of handleIds) {
486
+ attachedMTRefHandles.delete(handleId);
487
+ unhydratedSpreadMTRefSlots.delete(handleId);
488
+ pendingMTRefReconciliations.delete(handleId);
489
+ attachedMTRefsByElement.delete(handleId);
490
+ gestureAttrState.delete(handleId);
491
+ mtEventAttrState.delete(handleId);
492
+ mtRefAttrState.delete(handleId);
493
+ mtRefSlotSpecs.delete(handleId);
494
+ spreadAttrSlots.delete(handleId);
495
+ spreadSlotState.delete(handleId);
496
+ }
187
497
  }
188
498
  }
189
499
  //# sourceMappingURL=main-thread-dynamic-attr-state.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"main-thread-dynamic-attr-state.js","sourceRoot":"","sources":["../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAG5E,OAAO,EAAE,iCAAiC,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,qCAAqC,EAAE,MAAM,sCAAsC,CAAC;AAE7F,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,iBAAiB,EAAE,gBAAgB,EAAE,MAAM,0BAA0B,CAAC;AAUpH,MAAM,gBAAgB,GAA8C,IAAI,GAAG,EAAE,CAAC;AAC9E,MAAM,cAAc,GAA8C,IAAI,GAAG,EAAE,CAAC;AAC5E,MAAM,sBAAsB,GAAG,IAAI,GAAG,EAAU,CAAC;AASjD,IAAI,uBAAuB,GAA6B,EAAE,CAAC;AAO3D,SAAS,eAAe,CACtB,KAAuC,EACvC,QAAgB,EAChB,aAAqB;IAErB,MAAM,WAAW,GAAG,KAAK,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IACxC,IAAI,CAAC,WAAW,EAAE,CAAC;QACjB,OAAO;IACT,CAAC;IACD,WAAW,CAAC,MAAM,CAAC,aAAa,CAAC,CAAC;IAClC,IAAI,WAAW,CAAC,IAAI,KAAK,CAAC,EAAE,CAAC;QAC3B,KAAK,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IACzB,CAAC;AACH,CAAC;AAED,SAAS,uBAAuB,CAAC,WAAoC;IACnE,KAAK,MAAM,KAAK,IAAI,WAAW,CAAC,MAAM,EAAE,EAAE,CAAC;QACzC,iBAAiB,CAAC,KAAK,CAAC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C,CAAC;QACxC,MAAM,WAAW,GAAG,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACjD,IAAI,WAAW,EAAE,CAAC;YAChB,uBAAuB,CAAC,WAAW,CAAC,CAAC;QACvC,CAAC;IACH,CAAC;AACH,CAAC;AAED,MAAM,UAAU,0CAA0C,CACxD,SAA4B;IAE5B,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;QACjC,MAAM,WAAW,GAAG,cAAc,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;QACjD,IAAI,WAAW,IAAI,sBAAsB,CAAC,GAAG,CAAC,QAAQ,CAAC,EAAE,CAAC;YACxD,uBAAuB,CAAC,WAAW,CAAC,CAAC;QACvC,CAAC;QACD,sBAAsB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACxC,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QAClC,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;IAClC,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { hydrateWorkletCtx } from '@lynx-js/react/worklet-runtime/bindings';\nimport type { Worklet } from '@lynx-js/react/worklet-runtime/bindings';\n\nimport { getMainThreadDynamicAttrSlotKinds } from './attr-slot-plan.js';\nimport { retainMainThreadBackgroundFunctionCtx } from './main-thread-background-function.js';\nimport type { MTEventCtx, MTEventNativeWrapper } from './main-thread-event-ctx.js';\nimport { attachMTRefValue, cleanupMTRefValue, hydrateMTRefValue, retainMTRefValue } from './main-thread-ref-ctx.js';\nimport type { MTRefNativeWrapper, MTRefValue } from './main-thread-ref-ctx.js';\nimport type { SerializableValue } from '../../protocol/types.js';\n\ntype MainThreadDynamicAttrState =\n | { kind: 'mt-event'; nativeHeldValue: MTEventCtx }\n | { kind: 'mt-ref'; value: MTRefValue };\n\ntype MainThreadDynamicAttrStateMap<T> = Map<number, Map<number, T>>;\n\nconst mtEventAttrState: MainThreadDynamicAttrStateMap<MTEventCtx> = new Map();\nconst mtRefAttrState: MainThreadDynamicAttrStateMap<MTRefValue> = new Map();\nconst attachedMTRefHandleIds = new Set<number>();\n\ninterface PendingMTRefAttachment {\n handleId: number;\n attrSlotIndex: number;\n value: MTRefValue;\n nativeRef: ElementTemplateHandle;\n}\n\nlet pendingMTRefAttachments: PendingMTRefAttachment[] = [];\n\nexport interface MainThreadDynamicAttrSubtreeHandle {\n uid: number;\n ref: ElementTemplateHandle;\n}\n\nfunction deleteSlotState<T>(\n state: MainThreadDynamicAttrStateMap<T>,\n handleId: number,\n attrSlotIndex: number,\n): void {\n const handleState = state.get(handleId);\n if (!handleState) {\n return;\n }\n handleState.delete(attrSlotIndex);\n if (handleState.size === 0) {\n state.delete(handleId);\n }\n}\n\nfunction cleanupMTRefHandleState(handleState: Map<number, MTRefValue>): void {\n for (const value of handleState.values()) {\n cleanupMTRefValue(value);\n }\n}\n\nfunction setSlotState<T>(\n state: MainThreadDynamicAttrStateMap<T>,\n handleId: number,\n attrSlotIndex: number,\n value: T,\n): void {\n let handleState = state.get(handleId);\n if (!handleState) {\n handleState = new Map();\n state.set(handleId, handleState);\n }\n handleState.set(attrSlotIndex, value);\n}\n\nfunction setMTEventSlotState(handleId: number, attrSlotIndex: number, nativeHeldValue: MTEventCtx): void {\n setSlotState(mtEventAttrState, handleId, attrSlotIndex, nativeHeldValue);\n}\n\nfunction setMTRefSlotState(\n handleId: number,\n attrSlotIndex: number,\n value: MTRefValue,\n): void {\n retainMTRefValue(value);\n setSlotState(mtRefAttrState, handleId, attrSlotIndex, value);\n}\n\nexport function prepareMainThreadDynamicAttrSlotsForNative(\n templateType: string,\n attributeSlots: readonly unknown[] | null | undefined,\n): SerializableValue[] | null | undefined {\n if (attributeSlots == null) {\n return attributeSlots;\n }\n const slotKinds = getMainThreadDynamicAttrSlotKinds(templateType);\n if (!slotKinds) {\n return attributeSlots as SerializableValue[];\n }\n let nativeSlots: SerializableValue[] | undefined;\n for (const [attrSlotIndex, kind] of slotKinds) {\n if (kind === 'mt-event' && attributeSlots[attrSlotIndex] != null) {\n retainMainThreadBackgroundFunctionCtx((attributeSlots[attrSlotIndex] as MTEventNativeWrapper).value);\n }\n if (kind !== 'mt-ref') {\n continue;\n }\n nativeSlots ??= attributeSlots.slice() as SerializableValue[];\n nativeSlots[attrSlotIndex] = null;\n }\n return nativeSlots ?? attributeSlots as SerializableValue[];\n}\n\nexport function clearMainThreadDynamicAttrState(): void {\n try {\n for (const [handleId, handleState] of mtRefAttrState) {\n if (attachedMTRefHandleIds.has(handleId)) {\n cleanupMTRefHandleState(handleState);\n }\n }\n } finally {\n mtEventAttrState.clear();\n mtRefAttrState.clear();\n attachedMTRefHandleIds.clear();\n pendingMTRefAttachments = [];\n }\n}\n\nexport function getMainThreadDynamicAttrState(\n handleId: number,\n attrSlotIndex: number,\n): MainThreadDynamicAttrState | undefined {\n const event = mtEventAttrState.get(handleId)?.get(attrSlotIndex);\n if (event) {\n return { kind: 'mt-event', nativeHeldValue: event };\n }\n const ref = mtRefAttrState.get(handleId)?.get(attrSlotIndex);\n return ref ? { kind: 'mt-ref', value: ref } : undefined;\n}\n\nexport function initializeMainThreadDynamicAttrSlots(\n handleId: number,\n templateType: string,\n attributeSlots: readonly unknown[] | null | undefined,\n): void {\n const slotKinds = getMainThreadDynamicAttrSlotKinds(templateType);\n if (!slotKinds) {\n return;\n }\n for (const [attrSlotIndex, kind] of slotKinds) {\n const value = attributeSlots?.[attrSlotIndex];\n if (kind === 'mt-event' && value != null) {\n setMTEventSlotState(handleId, attrSlotIndex, (value as MTEventNativeWrapper).value);\n } else if (kind === 'mt-ref' && value != null) {\n setMTRefSlotState(handleId, attrSlotIndex, (value as MTRefNativeWrapper).value);\n }\n }\n}\n\nexport function updateMainThreadEventAttrSlot(\n handleId: number,\n attrSlotIndex: number,\n value: unknown,\n isHydration = false,\n): void {\n const previousState = isHydration\n ? mtEventAttrState.get(handleId)?.get(attrSlotIndex)\n : undefined;\n if (value == null) {\n deleteSlotState(mtEventAttrState, handleId, attrSlotIndex);\n return;\n }\n const nextValue = (value as MTEventNativeWrapper).value;\n setMTEventSlotState(handleId, attrSlotIndex, nextValue);\n if (previousState) {\n hydrateWorkletCtx(\n nextValue as Worklet,\n previousState as Worklet,\n );\n }\n}\n\nexport function updateMainThreadRefAttrSlot(\n handleId: number,\n attrSlotIndex: number,\n value: unknown,\n nativeRef: ElementTemplateHandle,\n isHydration = false,\n): void {\n const previousState = mtRefAttrState.get(handleId)?.get(attrSlotIndex);\n const isAttached = attachedMTRefHandleIds.has(handleId);\n if (value == null) {\n if (previousState && isAttached) {\n cleanupMTRefValue(previousState);\n }\n deleteSlotState(mtRefAttrState, handleId, attrSlotIndex);\n return;\n }\n if (previousState && isAttached) {\n cleanupMTRefValue(previousState);\n }\n const nextValue = (value as MTRefNativeWrapper).value;\n setMTRefSlotState(handleId, attrSlotIndex, nextValue);\n if (isHydration) {\n hydrateMTRefValue(nextValue, previousState);\n }\n if (isAttached) {\n pendingMTRefAttachments.push({ handleId, attrSlotIndex, value: nextValue, nativeRef });\n }\n}\n\nexport function attachMainThreadDynamicAttrRefsForSubtree(\n handles: readonly MainThreadDynamicAttrSubtreeHandle[],\n): void {\n for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {\n const handle = handles[handleIndex]!;\n if (attachedMTRefHandleIds.has(handle.uid)) {\n continue;\n }\n attachedMTRefHandleIds.add(handle.uid);\n const handleState = mtRefAttrState.get(handle.uid);\n if (!handleState) {\n continue;\n }\n for (const [attrSlotIndex, value] of handleState) {\n pendingMTRefAttachments.push({ handleId: handle.uid, attrSlotIndex, value, nativeRef: handle.ref });\n }\n }\n}\n\n// Consume one queue snapshot, matching Snapshot's ref delivery boundary.\nexport function applyPendingMainThreadRefAttachments(): void {\n const pending = pendingMTRefAttachments;\n pendingMTRefAttachments = [];\n for (const { handleId, attrSlotIndex, value, nativeRef } of pending) {\n const element = __GetElementByAttrSlot(nativeRef, attrSlotIndex);\n if (!element) {\n throw new Error(\n `ElementTemplate main-thread:ref target ${handleId}:${attrSlotIndex} could not be materialized.`,\n );\n }\n attachMTRefValue(value, element);\n }\n}\n\nexport function detachMainThreadDynamicAttrRefsForSubtree(\n handles: readonly MainThreadDynamicAttrSubtreeHandle[],\n): void {\n for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {\n const handleId = handles[handleIndex]!.uid;\n if (!attachedMTRefHandleIds.has(handleId)) {\n continue;\n }\n attachedMTRefHandleIds.delete(handleId);\n const handleState = mtRefAttrState.get(handleId);\n if (handleState) {\n cleanupMTRefHandleState(handleState);\n }\n }\n}\n\nexport function deleteMainThreadDynamicAttrStateForSubtree(\n handleIds: readonly number[],\n): void {\n for (const handleId of handleIds) {\n const handleState = mtRefAttrState.get(handleId);\n if (handleState && attachedMTRefHandleIds.has(handleId)) {\n cleanupMTRefHandleState(handleState);\n }\n attachedMTRefHandleIds.delete(handleId);\n mtEventAttrState.delete(handleId);\n mtRefAttrState.delete(handleId);\n }\n}\n"]}
1
+ {"version":3,"file":"main-thread-dynamic-attr-state.js","sourceRoot":"","sources":["../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,yCAAyC,CAAC;AAG5E,OAAO,EAAE,iCAAiC,EAAE,MAAM,qBAAqB,CAAC;AACxE,OAAO,EAAE,qCAAqC,EAAE,MAAM,sCAAsC,CAAC;AAC7F,OAAO,EAAE,sBAAsB,EAAE,MAAM,4BAA4B,CAAC;AAEpE,OAAO,EACL,gBAAgB,EAChB,iBAAiB,EACjB,iBAAiB,EACjB,oBAAoB,EACpB,gBAAgB,GACjB,MAAM,0BAA0B,CAAC;AAElC,OAAO,EAAE,oBAAoB,EAAE,MAAM,uCAAuC,CAAC;AAC7E,OAAO,EAAE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CAAC,QAAQ,CAAC,CAAC;YAC7C,CAAC;QACH,CAAC;IACH,CAAC;YAAS,CAAC;QACT,KAAK,MAAM,QAAQ,IAAI,SAAS,EAAE,CAAC;YACjC,oBAAoB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YACtC,0BAA0B,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC5C,2BAA2B,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAC7C,uBAAuB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YACzC,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAClC,gBAAgB,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAClC,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAChC,cAAc,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YAChC,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;YACjC,eAAe,CAAC,MAAM,CAAC,QAAQ,CAAC,CAAC;QACnC,CAAC;IACH,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { hydrateWorkletCtx } from '@lynx-js/react/worklet-runtime/bindings';\nimport type { Worklet } from '@lynx-js/react/worklet-runtime/bindings';\n\nimport { getMainThreadDynamicAttrSlotKinds } from './attr-slot-plan.js';\nimport { retainMainThreadBackgroundFunctionCtx } from './main-thread-background-function.js';\nimport { isMTEventNativeWrapper } from './main-thread-event-ctx.js';\nimport type { MTEventCtx, MTEventNativeWrapper } from './main-thread-event-ctx.js';\nimport {\n attachMTRefValue,\n cleanupMTRefValue,\n hydrateMTRefValue,\n isMTRefNativeWrapper,\n retainMTRefValue,\n} from './main-thread-ref-ctx.js';\nimport type { MTRefNativeWrapper, MTRefValue } from './main-thread-ref-ctx.js';\nimport { isMainThreadFunction } from '../../../core/main-thread-function.js';\nimport { isMainThreadRef } from '../../../core/main-thread-ref.js';\nimport { hydrateGestureCallbacks, isGestureSlotValue, retainGestureCallbacks } from '../../prop-adapters/gesture.js';\nimport type { GestureSlotValue } from '../../prop-adapters/gesture.js';\nimport { isMainThreadEventPropKey, isSpreadRecord } from '../../prop-adapters/spread.js';\nimport type { SerializableValue } from '../../protocol/types.js';\n\ntype MainThreadDynamicAttrState =\n | { kind: 'mt-event'; nativeHeldValue: MTEventCtx }\n | { kind: 'mt-ref'; value: MTRefValue };\n\ntype MainThreadDynamicAttrStateMap<T> = Map<number, Map<number, T>>;\n\ninterface SpreadSlotDynamicState {\n events?: Map<string, MTEventCtx> | undefined;\n gesture?: GestureSlotValue | undefined;\n}\n\ninterface AttachedMTRefEntry {\n slotIndex: number;\n value: MTRefValue;\n}\n\nconst gestureAttrState: MainThreadDynamicAttrStateMap<GestureSlotValue> = new Map();\nconst mtEventAttrState: MainThreadDynamicAttrStateMap<MTEventCtx> = new Map();\nconst mtRefAttrState: MainThreadDynamicAttrStateMap<MTRefValue> = new Map();\nconst mtRefSlotSpecs: MainThreadDynamicAttrStateMap<MTRefValue | null> = new Map();\nconst spreadAttrSlots = new Map<number, Set<number>>();\nconst spreadSlotState: MainThreadDynamicAttrStateMap<SpreadSlotDynamicState> = new Map();\nconst unhydratedSpreadMTRefSlots = new Map<number, Set<number>>();\nconst attachedMTRefHandles = new Map<number, ElementTemplateHandle>();\nconst attachedMTRefsByElement = new Map<number, Map<ElementRef, AttachedMTRefEntry>>();\n\nlet pendingMTRefReconciliations = new Map<number, ElementTemplateHandle>();\n\nexport interface MainThreadDynamicAttrSubtreeHandle {\n uid: number;\n ref: ElementTemplateHandle;\n}\n\nfunction deleteSlotState<T>(\n state: MainThreadDynamicAttrStateMap<T>,\n handleId: number,\n attrSlotIndex: number,\n): void {\n const handleState = state.get(handleId);\n if (!handleState) {\n return;\n }\n handleState.delete(attrSlotIndex);\n if (handleState.size === 0) {\n state.delete(handleId);\n }\n}\n\nfunction setSlotState<T>(\n state: MainThreadDynamicAttrStateMap<T>,\n handleId: number,\n attrSlotIndex: number,\n value: T,\n): void {\n let handleState = state.get(handleId);\n if (!handleState) {\n handleState = new Map();\n state.set(handleId, handleState);\n }\n handleState.set(attrSlotIndex, value);\n}\n\nfunction registerHandleSlot(\n slotsByHandle: Map<number, Set<number>>,\n handleId: number,\n attrSlotIndex: number,\n): void {\n let slots = slotsByHandle.get(handleId);\n if (!slots) {\n slots = new Set();\n slotsByHandle.set(handleId, slots);\n }\n slots.add(attrSlotIndex);\n}\n\nfunction deleteHandleSlot(\n slotsByHandle: Map<number, Set<number>>,\n handleId: number,\n attrSlotIndex: number,\n): void {\n const slots = slotsByHandle.get(handleId);\n if (!slots) {\n return;\n }\n slots.delete(attrSlotIndex);\n if (slots.size === 0) {\n slotsByHandle.delete(handleId);\n }\n}\n\nfunction hasNonNullMTRefSpec(handleId: number): boolean {\n const specs = mtRefSlotSpecs.get(handleId);\n if (!specs) {\n return false;\n }\n for (const value of specs.values()) {\n if (value !== null) {\n return true;\n }\n }\n return false;\n}\n\nfunction isSameMTRefSpec(\n prev: MTRefValue | null | undefined,\n next: MTRefValue | null | undefined,\n isHydration: boolean,\n): boolean {\n if (prev === next) {\n return true;\n }\n if (prev == null || next == null) {\n return false;\n }\n if (isMainThreadRef(prev) && isMainThreadRef(next)) {\n return prev._wvid === next._wvid;\n }\n return !isHydration\n && isMainThreadFunction(prev)\n && isMainThreadFunction(next)\n && prev._execId !== undefined\n && prev._execId === next._execId;\n}\n\nfunction cleanupAttachedElementsForHandle(handleId: number): void {\n const attachedElements = attachedMTRefsByElement.get(handleId);\n if (!attachedElements) {\n return;\n }\n attachedMTRefsByElement.delete(handleId);\n for (const entry of attachedElements.values()) {\n cleanupMTRefValue(entry.value);\n }\n}\n\nfunction cleanupAttachedElementsForSlot(handleId: number, attrSlotIndex: number): void {\n const attachedElements = attachedMTRefsByElement.get(handleId);\n if (!attachedElements) {\n return;\n }\n const toCleanup: MTRefValue[] = [];\n for (const [element, entry] of attachedElements) {\n if (entry.slotIndex === attrSlotIndex) {\n attachedElements.delete(element);\n toCleanup.push(entry.value);\n }\n }\n if (attachedElements.size === 0) {\n attachedMTRefsByElement.delete(handleId);\n }\n for (const value of toCleanup) {\n cleanupMTRefValue(value);\n }\n}\n\nfunction setMTEventSlotState(handleId: number, attrSlotIndex: number, nativeHeldValue: MTEventCtx): void {\n setSlotState(mtEventAttrState, handleId, attrSlotIndex, nativeHeldValue);\n}\n\nfunction setDirectMTRefSlotState(\n handleId: number,\n attrSlotIndex: number,\n value: MTRefValue,\n): void {\n retainMTRefValue(value);\n setSlotState(mtRefAttrState, handleId, attrSlotIndex, value);\n setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, value);\n}\n\nexport function isSpreadDynamicAttrSlot(\n handleId: number,\n attrSlotIndex: number,\n): boolean {\n return spreadAttrSlots.get(handleId)?.has(attrSlotIndex) === true;\n}\n\nexport function prepareSpreadAttrSlotForNative(\n value: unknown,\n): SerializableValue | null {\n if (!isSpreadRecord(value)) {\n return null;\n }\n const keys = Object.keys(value);\n let hasOwnMTRef = false;\n let activeMTRefWrapper: MTRefNativeWrapper | undefined;\n for (const key of keys) {\n const entry = value[key];\n if (key === 'main-thread:ref') {\n hasOwnMTRef = true;\n if (isMTRefNativeWrapper(entry)) {\n activeMTRefWrapper = entry;\n retainMTRefValue(entry.value);\n }\n } else if (key === 'main-thread:gesture') {\n if (isGestureSlotValue(entry)) {\n retainGestureCallbacks(entry);\n }\n } else if (isMainThreadEventPropKey(key) && isMTEventNativeWrapper(entry)) {\n retainMainThreadBackgroundFunctionCtx(entry.value);\n }\n }\n if (!hasOwnMTRef) {\n return value as SerializableValue;\n }\n const nativeSpread: Record<string, SerializableValue> = {};\n for (const key of keys) {\n if (key !== 'main-thread:ref') {\n nativeSpread[key] = value[key] as SerializableValue;\n }\n }\n if (activeMTRefWrapper) {\n nativeSpread['has-react-ref'] = true;\n nativeSpread['flatten'] = false;\n }\n return nativeSpread;\n}\n\nexport function prepareMainThreadDynamicAttrSlotsForNative(\n templateType: string,\n attributeSlots: readonly unknown[] | null | undefined,\n): SerializableValue[] | null | undefined {\n if (attributeSlots == null) {\n return attributeSlots;\n }\n const slotKinds = getMainThreadDynamicAttrSlotKinds(templateType);\n if (!slotKinds) {\n return attributeSlots as SerializableValue[];\n }\n let nativeSlots: SerializableValue[] | undefined;\n for (const [attrSlotIndex, kind] of slotKinds) {\n if (kind === 'gesture' && attributeSlots[attrSlotIndex] != null) {\n retainGestureCallbacks(attributeSlots[attrSlotIndex] as GestureSlotValue);\n }\n if (kind === 'mt-event' && attributeSlots[attrSlotIndex] != null) {\n retainMainThreadBackgroundFunctionCtx((attributeSlots[attrSlotIndex] as MTEventNativeWrapper).value);\n continue;\n }\n if (kind === 'spread') {\n const slotValue = attributeSlots[attrSlotIndex];\n const nativeSpread = prepareSpreadAttrSlotForNative(slotValue);\n if (nativeSpread !== slotValue) {\n nativeSlots ??= attributeSlots.slice() as SerializableValue[];\n nativeSlots[attrSlotIndex] = nativeSpread;\n }\n continue;\n }\n if (kind !== 'mt-ref') {\n continue;\n }\n nativeSlots ??= attributeSlots.slice() as SerializableValue[];\n nativeSlots[attrSlotIndex] = null;\n }\n return nativeSlots ?? attributeSlots as SerializableValue[];\n}\n\nexport function clearMainThreadDynamicAttrState(): void {\n const attachedHandleIds = Array.from(attachedMTRefHandles.keys());\n attachedMTRefHandles.clear();\n unhydratedSpreadMTRefSlots.clear();\n pendingMTRefReconciliations.clear();\n try {\n for (const handleId of attachedHandleIds) {\n cleanupAttachedElementsForHandle(handleId);\n }\n } finally {\n gestureAttrState.clear();\n mtEventAttrState.clear();\n mtRefAttrState.clear();\n mtRefSlotSpecs.clear();\n spreadAttrSlots.clear();\n spreadSlotState.clear();\n attachedMTRefsByElement.clear();\n }\n}\n\nexport function getMainThreadDynamicAttrState(\n handleId: number,\n attrSlotIndex: number,\n): MainThreadDynamicAttrState | undefined {\n const event = mtEventAttrState.get(handleId)?.get(attrSlotIndex);\n if (event) {\n return { kind: 'mt-event', nativeHeldValue: event };\n }\n const ref = mtRefAttrState.get(handleId)?.get(attrSlotIndex);\n return ref ? { kind: 'mt-ref', value: ref } : undefined;\n}\n\nfunction commitSpreadSlotState(\n handleId: number,\n attrSlotIndex: number,\n value: unknown,\n isHydration: boolean,\n cleanupBeforeRefHydrate = false,\n): { prevSpec: MTRefValue | null | undefined; nextSpec: MTRefValue | null | undefined } {\n deleteHandleSlot(unhydratedSpreadMTRefSlots, handleId, attrSlotIndex);\n const prevSpread = isHydration ? spreadSlotState.get(handleId)?.get(attrSlotIndex) : undefined;\n const prevSpec = mtRefSlotSpecs.get(handleId)?.get(attrSlotIndex);\n\n let nextEvents: Map<string, MTEventCtx> | undefined;\n let nextGesture: GestureSlotValue | undefined;\n let hasOwnMTRef = false;\n let nextRefValue: MTRefValue | null = null;\n\n if (isSpreadRecord(value)) {\n for (const key of Object.keys(value)) {\n const entry = value[key];\n if (key === 'main-thread:ref') {\n hasOwnMTRef = true;\n nextRefValue = isMTRefNativeWrapper(entry) ? entry.value : null;\n } else if (key === 'main-thread:gesture') {\n if (isGestureSlotValue(entry)) {\n nextGesture = entry;\n }\n } else if (isMainThreadEventPropKey(key) && isMTEventNativeWrapper(entry)) {\n (nextEvents ??= new Map()).set(key, entry.value);\n }\n }\n }\n\n if (nextEvents || nextGesture) {\n setSlotState(spreadSlotState, handleId, attrSlotIndex, {\n events: nextEvents,\n gesture: nextGesture,\n });\n } else {\n deleteSlotState(spreadSlotState, handleId, attrSlotIndex);\n }\n\n const rawNextSpec = hasOwnMTRef ? nextRefValue : undefined;\n const nextSpec = isSameMTRefSpec(prevSpec, rawNextSpec, isHydration) ? prevSpec : rawNextSpec;\n\n if (cleanupBeforeRefHydrate && prevSpec !== nextSpec) {\n cleanupAttachedElementsForSlot(handleId, attrSlotIndex);\n }\n\n if (isHydration) {\n if (nextEvents && prevSpread?.events) {\n for (const [eventKey, nextCtx] of nextEvents) {\n const prevCtx = prevSpread.events.get(eventKey);\n if (prevCtx) {\n hydrateWorkletCtx(nextCtx as Worklet, prevCtx as Worklet);\n }\n }\n }\n if (nextGesture && prevSpread?.gesture) {\n hydrateGestureCallbacks(nextGesture, prevSpread.gesture);\n }\n if (nextRefValue !== null) {\n hydrateMTRefValue(nextRefValue, prevSpec);\n }\n }\n\n if (nextSpec === undefined) {\n deleteSlotState(mtRefSlotSpecs, handleId, attrSlotIndex);\n } else {\n setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, nextSpec);\n }\n\n return { prevSpec, nextSpec };\n}\n\nexport function initializeMainThreadDynamicAttrSlots(\n handleId: number,\n templateType: string,\n attributeSlots: readonly unknown[] | null | undefined,\n isCreatedByPatch = false,\n): void {\n const slotKinds = getMainThreadDynamicAttrSlotKinds(templateType);\n if (!slotKinds) {\n return;\n }\n for (const [attrSlotIndex, kind] of slotKinds) {\n const value = attributeSlots?.[attrSlotIndex];\n if (kind === 'gesture' && value != null) {\n updateGestureAttrSlot(handleId, attrSlotIndex, value);\n } else if (kind === 'mt-event' && value != null) {\n setMTEventSlotState(handleId, attrSlotIndex, (value as MTEventNativeWrapper).value);\n } else if (kind === 'mt-ref') {\n if (value == null) {\n setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, null);\n } else {\n setDirectMTRefSlotState(handleId, attrSlotIndex, (value as MTRefNativeWrapper).value);\n }\n } else if (kind === 'spread') {\n registerHandleSlot(spreadAttrSlots, handleId, attrSlotIndex);\n const { nextSpec } = commitSpreadSlotState(handleId, attrSlotIndex, value, false);\n if (!isCreatedByPatch && nextSpec !== undefined) {\n registerHandleSlot(unhydratedSpreadMTRefSlots, handleId, attrSlotIndex);\n }\n }\n }\n}\n\nexport function updateGestureAttrSlot(\n handleId: number,\n attrSlotIndex: number,\n value: unknown,\n isHydration = false,\n): void {\n const previous = isHydration ? gestureAttrState.get(handleId)?.get(attrSlotIndex) : undefined;\n if (value == null) {\n deleteSlotState(gestureAttrState, handleId, attrSlotIndex);\n return;\n }\n const next = value as GestureSlotValue;\n if (previous) {\n hydrateGestureCallbacks(next, previous);\n }\n setSlotState(gestureAttrState, handleId, attrSlotIndex, next);\n}\n\nexport function updateMainThreadEventAttrSlot(\n handleId: number,\n attrSlotIndex: number,\n value: unknown,\n isHydration = false,\n): void {\n const previousState = isHydration\n ? mtEventAttrState.get(handleId)?.get(attrSlotIndex)\n : undefined;\n if (value == null) {\n deleteSlotState(mtEventAttrState, handleId, attrSlotIndex);\n return;\n }\n const nextValue = (value as MTEventNativeWrapper).value;\n setMTEventSlotState(handleId, attrSlotIndex, nextValue);\n if (previousState) {\n hydrateWorkletCtx(\n nextValue as Worklet,\n previousState as Worklet,\n );\n }\n}\n\nexport function updateMainThreadRefAttrSlot(\n handleId: number,\n attrSlotIndex: number,\n value: unknown,\n nativeRef: ElementTemplateHandle,\n isHydration = false,\n): void {\n const previousState = mtRefAttrState.get(handleId)?.get(attrSlotIndex);\n const isAttached = attachedMTRefHandles.has(handleId);\n if (isAttached) {\n cleanupAttachedElementsForSlot(handleId, attrSlotIndex);\n }\n if (value == null) {\n deleteSlotState(mtRefAttrState, handleId, attrSlotIndex);\n setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, null);\n return;\n }\n const nextValue = (value as MTRefNativeWrapper).value;\n setDirectMTRefSlotState(handleId, attrSlotIndex, nextValue);\n if (isHydration) {\n hydrateMTRefValue(nextValue, previousState);\n }\n if (isAttached) {\n pendingMTRefReconciliations.set(handleId, nativeRef);\n }\n}\n\nexport function updateSpreadAttrSlot(\n handleId: number,\n attrSlotIndex: number,\n value: unknown,\n nativeRef: ElementTemplateHandle,\n isHydration = false,\n): void {\n const isAttached = attachedMTRefHandles.has(handleId);\n const { prevSpec, nextSpec } = commitSpreadSlotState(\n handleId,\n attrSlotIndex,\n value,\n isHydration,\n isAttached,\n );\n if (prevSpec === nextSpec) {\n return;\n }\n if (isAttached && hasNonNullMTRefSpec(handleId)) {\n pendingMTRefReconciliations.set(handleId, nativeRef);\n }\n}\n\nexport function reconcileUnhydratedSpreadMTRefSlots(): void {\n if (unhydratedSpreadMTRefSlots.size === 0) {\n return;\n }\n for (const [handleId, slotIndices] of unhydratedSpreadMTRefSlots) {\n for (const attrSlotIndex of slotIndices) {\n cleanupAttachedElementsForSlot(handleId, attrSlotIndex);\n deleteSlotState(mtRefSlotSpecs, handleId, attrSlotIndex);\n }\n const nativeRef = attachedMTRefHandles.get(handleId);\n if (nativeRef && hasNonNullMTRefSpec(handleId)) {\n pendingMTRefReconciliations.set(handleId, nativeRef);\n }\n }\n unhydratedSpreadMTRefSlots.clear();\n}\n\nexport function attachMainThreadDynamicAttrRefsForSubtree(\n handles: readonly MainThreadDynamicAttrSubtreeHandle[],\n): void {\n for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {\n const handle = handles[handleIndex]!;\n if (attachedMTRefHandles.has(handle.uid)) {\n continue;\n }\n attachedMTRefHandles.set(handle.uid, handle.ref);\n if (hasNonNullMTRefSpec(handle.uid)) {\n pendingMTRefReconciliations.set(handle.uid, handle.ref);\n }\n }\n}\n\n// Consume one queue snapshot, matching Snapshot's ref delivery boundary.\nexport function applyPendingMainThreadRefAttachments(isHydration = false): void {\n if (isHydration) {\n reconcileUnhydratedSpreadMTRefSlots();\n }\n if (pendingMTRefReconciliations.size === 0) {\n return;\n }\n const pending = pendingMTRefReconciliations;\n pendingMTRefReconciliations = new Map();\n\n for (const [handleId, nativeRef] of pending) {\n const specs = mtRefSlotSpecs.get(handleId);\n const desiredByElement = new Map<ElementRef, AttachedMTRefEntry | null>();\n if (specs && specs.size > 0) {\n const sortedEntries = specs.size === 1\n ? Array.from(specs.entries())\n : Array.from(specs.entries()).sort((a, b) => a[0] - b[0]);\n for (const [slotIndex, refValue] of sortedEntries) {\n const element = __GetElementByAttrSlot(nativeRef, slotIndex);\n if (!element) {\n throw new Error(\n `ElementTemplate main-thread:ref target ${handleId}:${slotIndex} could not be materialized.`,\n );\n }\n desiredByElement.set(\n element,\n refValue === null ? null : { slotIndex, value: refValue },\n );\n }\n }\n\n let attachedElements = attachedMTRefsByElement.get(handleId);\n if (attachedElements) {\n const staleValues: MTRefValue[] = [];\n for (const [element, prevEntry] of attachedElements) {\n const desired = desiredByElement.get(element) ?? null;\n if (!desired || desired.slotIndex !== prevEntry.slotIndex || desired.value !== prevEntry.value) {\n attachedElements.delete(element);\n staleValues.push(prevEntry.value);\n }\n }\n if (attachedElements.size === 0) {\n attachedMTRefsByElement.delete(handleId);\n attachedElements = undefined;\n }\n for (const staleValue of staleValues) {\n cleanupMTRefValue(staleValue);\n }\n }\n\n for (const [element, desired] of desiredByElement) {\n if (!desired) {\n continue;\n }\n if (attachedElements?.has(element)) {\n continue;\n }\n attachMTRefValue(desired.value, element);\n if (!attachedElements) {\n attachedElements = new Map();\n attachedMTRefsByElement.set(handleId, attachedElements);\n }\n attachedElements.set(element, desired);\n }\n }\n}\n\nexport function detachMainThreadDynamicAttrRefsForSubtree(\n handles: readonly MainThreadDynamicAttrSubtreeHandle[],\n): void {\n for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {\n const handleId = handles[handleIndex]!.uid;\n if (!attachedMTRefHandles.has(handleId)) {\n continue;\n }\n attachedMTRefHandles.delete(handleId);\n pendingMTRefReconciliations.delete(handleId);\n cleanupAttachedElementsForHandle(handleId);\n }\n}\n\nexport function deleteMainThreadDynamicAttrStateForSubtree(\n handleIds: readonly number[],\n): void {\n try {\n for (const handleId of handleIds) {\n if (attachedMTRefHandles.has(handleId)) {\n cleanupAttachedElementsForHandle(handleId);\n }\n }\n } finally {\n for (const handleId of handleIds) {\n attachedMTRefHandles.delete(handleId);\n unhydratedSpreadMTRefSlots.delete(handleId);\n pendingMTRefReconciliations.delete(handleId);\n attachedMTRefsByElement.delete(handleId);\n gestureAttrState.delete(handleId);\n mtEventAttrState.delete(handleId);\n mtRefAttrState.delete(handleId);\n mtRefSlotSpecs.delete(handleId);\n spreadAttrSlots.delete(handleId);\n spreadSlotState.delete(handleId);\n }\n }\n}\n"]}