@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
@@ -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,18 +30,27 @@ type MainThreadDynamicAttrState =
18
30
 
19
31
  type MainThreadDynamicAttrStateMap<T> = Map<number, Map<number, T>>;
20
32
 
21
- const mtEventAttrState: MainThreadDynamicAttrStateMap<MTEventCtx> = new Map();
22
- const mtRefAttrState: MainThreadDynamicAttrStateMap<MTRefValue> = new Map();
23
- const attachedMTRefHandleIds = new Set<number>();
33
+ interface SpreadSlotDynamicState {
34
+ events?: Map<string, MTEventCtx> | undefined;
35
+ gesture?: GestureSlotValue | undefined;
36
+ }
24
37
 
25
- interface PendingMTRefAttachment {
26
- handleId: number;
27
- attrSlotIndex: number;
38
+ interface AttachedMTRefEntry {
39
+ slotIndex: number;
28
40
  value: MTRefValue;
29
- nativeRef: ElementTemplateHandle;
30
41
  }
31
42
 
32
- let pendingMTRefAttachments: PendingMTRefAttachment[] = [];
43
+ const gestureAttrState: MainThreadDynamicAttrStateMap<GestureSlotValue> = new Map();
44
+ const mtEventAttrState: MainThreadDynamicAttrStateMap<MTEventCtx> = new Map();
45
+ const mtRefAttrState: MainThreadDynamicAttrStateMap<MTRefValue> = new Map();
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>();
33
54
 
34
55
  export interface MainThreadDynamicAttrSubtreeHandle {
35
56
  uid: number;
@@ -51,12 +72,6 @@ function deleteSlotState<T>(
51
72
  }
52
73
  }
53
74
 
54
- function cleanupMTRefHandleState(handleState: Map<number, MTRefValue>): void {
55
- for (const value of handleState.values()) {
56
- cleanupMTRefValue(value);
57
- }
58
- }
59
-
60
75
  function setSlotState<T>(
61
76
  state: MainThreadDynamicAttrStateMap<T>,
62
77
  handleId: number,
@@ -71,17 +86,159 @@ function setSlotState<T>(
71
86
  handleState.set(attrSlotIndex, value);
72
87
  }
73
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
+
74
182
  function setMTEventSlotState(handleId: number, attrSlotIndex: number, nativeHeldValue: MTEventCtx): void {
75
183
  setSlotState(mtEventAttrState, handleId, attrSlotIndex, nativeHeldValue);
76
184
  }
77
185
 
78
- function setMTRefSlotState(
186
+ function setDirectMTRefSlotState(
79
187
  handleId: number,
80
188
  attrSlotIndex: number,
81
189
  value: MTRefValue,
82
190
  ): void {
83
191
  retainMTRefValue(value);
84
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;
85
242
  }
86
243
 
87
244
  export function prepareMainThreadDynamicAttrSlotsForNative(
@@ -97,8 +254,21 @@ export function prepareMainThreadDynamicAttrSlotsForNative(
97
254
  }
98
255
  let nativeSlots: SerializableValue[] | undefined;
99
256
  for (const [attrSlotIndex, kind] of slotKinds) {
257
+ if (kind === 'gesture' && attributeSlots[attrSlotIndex] != null) {
258
+ retainGestureCallbacks(attributeSlots[attrSlotIndex] as GestureSlotValue);
259
+ }
100
260
  if (kind === 'mt-event' && attributeSlots[attrSlotIndex] != null) {
101
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;
102
272
  }
103
273
  if (kind !== 'mt-ref') {
104
274
  continue;
@@ -110,17 +280,22 @@ export function prepareMainThreadDynamicAttrSlotsForNative(
110
280
  }
111
281
 
112
282
  export function clearMainThreadDynamicAttrState(): void {
283
+ const attachedHandleIds = Array.from(attachedMTRefHandles.keys());
284
+ attachedMTRefHandles.clear();
285
+ unhydratedSpreadMTRefSlots.clear();
286
+ pendingMTRefReconciliations.clear();
113
287
  try {
114
- for (const [handleId, handleState] of mtRefAttrState) {
115
- if (attachedMTRefHandleIds.has(handleId)) {
116
- cleanupMTRefHandleState(handleState);
117
- }
288
+ for (const handleId of attachedHandleIds) {
289
+ cleanupAttachedElementsForHandle(handleId);
118
290
  }
119
291
  } finally {
292
+ gestureAttrState.clear();
120
293
  mtEventAttrState.clear();
121
294
  mtRefAttrState.clear();
122
- attachedMTRefHandleIds.clear();
123
- pendingMTRefAttachments = [];
295
+ mtRefSlotSpecs.clear();
296
+ spreadAttrSlots.clear();
297
+ spreadSlotState.clear();
298
+ attachedMTRefsByElement.clear();
124
299
  }
125
300
  }
126
301
 
@@ -136,10 +311,85 @@ export function getMainThreadDynamicAttrState(
136
311
  return ref ? { kind: 'mt-ref', value: ref } : undefined;
137
312
  }
138
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
+
139
388
  export function initializeMainThreadDynamicAttrSlots(
140
389
  handleId: number,
141
390
  templateType: string,
142
391
  attributeSlots: readonly unknown[] | null | undefined,
392
+ isCreatedByPatch = false,
143
393
  ): void {
144
394
  const slotKinds = getMainThreadDynamicAttrSlotKinds(templateType);
145
395
  if (!slotKinds) {
@@ -147,14 +397,44 @@ export function initializeMainThreadDynamicAttrSlots(
147
397
  }
148
398
  for (const [attrSlotIndex, kind] of slotKinds) {
149
399
  const value = attributeSlots?.[attrSlotIndex];
150
- 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) {
151
403
  setMTEventSlotState(handleId, attrSlotIndex, (value as MTEventNativeWrapper).value);
152
- } else if (kind === 'mt-ref' && value != null) {
153
- 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
+ }
154
416
  }
155
417
  }
156
418
  }
157
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
+
158
438
  export function updateMainThreadEventAttrSlot(
159
439
  handleId: number,
160
440
  attrSlotIndex: number,
@@ -186,58 +466,145 @@ export function updateMainThreadRefAttrSlot(
186
466
  isHydration = false,
187
467
  ): void {
188
468
  const previousState = mtRefAttrState.get(handleId)?.get(attrSlotIndex);
189
- const isAttached = attachedMTRefHandleIds.has(handleId);
469
+ const isAttached = attachedMTRefHandles.has(handleId);
470
+ if (isAttached) {
471
+ cleanupAttachedElementsForSlot(handleId, attrSlotIndex);
472
+ }
190
473
  if (value == null) {
191
- if (previousState && isAttached) {
192
- cleanupMTRefValue(previousState);
193
- }
194
474
  deleteSlotState(mtRefAttrState, handleId, attrSlotIndex);
475
+ setSlotState(mtRefSlotSpecs, handleId, attrSlotIndex, null);
195
476
  return;
196
477
  }
197
- if (previousState && isAttached) {
198
- cleanupMTRefValue(previousState);
199
- }
200
478
  const nextValue = (value as MTRefNativeWrapper).value;
201
- setMTRefSlotState(handleId, attrSlotIndex, nextValue);
479
+ setDirectMTRefSlotState(handleId, attrSlotIndex, nextValue);
202
480
  if (isHydration) {
203
481
  hydrateMTRefValue(nextValue, previousState);
204
482
  }
205
483
  if (isAttached) {
206
- pendingMTRefAttachments.push({ handleId, attrSlotIndex, value: nextValue, nativeRef });
484
+ pendingMTRefReconciliations.set(handleId, nativeRef);
485
+ }
486
+ }
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);
207
508
  }
208
509
  }
209
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
+
210
528
  export function attachMainThreadDynamicAttrRefsForSubtree(
211
529
  handles: readonly MainThreadDynamicAttrSubtreeHandle[],
212
530
  ): void {
213
531
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
214
532
  const handle = handles[handleIndex]!;
215
- if (attachedMTRefHandleIds.has(handle.uid)) {
533
+ if (attachedMTRefHandles.has(handle.uid)) {
216
534
  continue;
217
535
  }
218
- attachedMTRefHandleIds.add(handle.uid);
219
- const handleState = mtRefAttrState.get(handle.uid);
220
- if (!handleState) {
221
- continue;
222
- }
223
- for (const [attrSlotIndex, value] of handleState) {
224
- pendingMTRefAttachments.push({ handleId: handle.uid, attrSlotIndex, value, nativeRef: handle.ref });
536
+ attachedMTRefHandles.set(handle.uid, handle.ref);
537
+ if (hasNonNullMTRefSpec(handle.uid)) {
538
+ pendingMTRefReconciliations.set(handle.uid, handle.ref);
225
539
  }
226
540
  }
227
541
  }
228
542
 
229
543
  // Consume one queue snapshot, matching Snapshot's ref delivery boundary.
230
- export function applyPendingMainThreadRefAttachments(): void {
231
- const pending = pendingMTRefAttachments;
232
- pendingMTRefAttachments = [];
233
- for (const { handleId, attrSlotIndex, value, nativeRef } of pending) {
234
- const element = __GetElementByAttrSlot(nativeRef, attrSlotIndex);
235
- if (!element) {
236
- throw new Error(
237
- `ElementTemplate main-thread:ref target ${handleId}:${attrSlotIndex} could not be materialized.`,
238
- );
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);
239
607
  }
240
- attachMTRefValue(value, element);
241
608
  }
242
609
  }
243
610
 
@@ -246,27 +613,36 @@ export function detachMainThreadDynamicAttrRefsForSubtree(
246
613
  ): void {
247
614
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
248
615
  const handleId = handles[handleIndex]!.uid;
249
- if (!attachedMTRefHandleIds.has(handleId)) {
616
+ if (!attachedMTRefHandles.has(handleId)) {
250
617
  continue;
251
618
  }
252
- attachedMTRefHandleIds.delete(handleId);
253
- const handleState = mtRefAttrState.get(handleId);
254
- if (handleState) {
255
- cleanupMTRefHandleState(handleState);
256
- }
619
+ attachedMTRefHandles.delete(handleId);
620
+ pendingMTRefReconciliations.delete(handleId);
621
+ cleanupAttachedElementsForHandle(handleId);
257
622
  }
258
623
  }
259
624
 
260
625
  export function deleteMainThreadDynamicAttrStateForSubtree(
261
626
  handleIds: readonly number[],
262
627
  ): void {
263
- for (const handleId of handleIds) {
264
- const handleState = mtRefAttrState.get(handleId);
265
- if (handleState && attachedMTRefHandleIds.has(handleId)) {
266
- cleanupMTRefHandleState(handleState);
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);
267
646
  }
268
- attachedMTRefHandleIds.delete(handleId);
269
- mtEventAttrState.delete(handleId);
270
- mtRefAttrState.delete(handleId);
271
647
  }
272
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);
@@ -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,