@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
@@ -1,7 +1,10 @@
1
1
  import { afterEach, describe, expect, it, vi } from 'vitest';
2
2
 
3
3
  import { getEventValue } from '../../../../src/element-template/prop-adapters/event-value.js';
4
- import { prepareSpreadAttrSlot } from '../../../../src/element-template/prop-adapters/spread.js';
4
+ import {
5
+ prepareSpreadAttrSlot,
6
+ prepareTypedSpreadAttrSlot,
7
+ } from '../../../../src/element-template/prop-adapters/spread.js';
5
8
 
6
9
  afterEach(() => {
7
10
  lynx.__runtime_configs__ = { transformBuiltinAttributeNames: false };
@@ -74,7 +77,7 @@ describe('ElementTemplate spread prop adapter', () => {
74
77
  __spread: true,
75
78
  ref,
76
79
  bindtap: handleTap,
77
- 'main-thread:bindtap': unsupportedWorkletEvent,
80
+ 'worklet:bindtap': unsupportedWorkletEvent,
78
81
  onReady: unsupportedFunctionProp,
79
82
  },
80
83
  );
@@ -145,16 +148,192 @@ describe('ElementTemplate spread prop adapter', () => {
145
148
  expect(prepareSpreadAttrSlot(-4, 0, false)).toBeNull();
146
149
  });
147
150
 
148
- it('ignores non-host spread props', () => {
151
+ it('ignores non-host spread props and isolates typed host spreads from main-thread capabilities', () => {
149
152
  const prepared = prepareSpreadAttrSlot(-5, 0, {
150
153
  'worklet:ref': vi.fn(),
151
- 'main-thread:ref': vi.fn(),
152
- 'main-thread:bindtap': vi.fn(),
153
- 'main-thread:gesture': {},
154
+ 'worklet:bindtap': vi.fn(),
155
+ 'worklet:gesture': {},
156
+ 'main-thread:custom': () => {},
154
157
  onReady: vi.fn(),
155
158
  title: 'hello',
156
159
  });
157
160
 
158
161
  expect(prepared).toEqual({ title: 'hello' });
162
+
163
+ const typedPrepared = prepareTypedSpreadAttrSlot(-5, 0, {
164
+ 'main-thread:ref': { _wvid: 1 },
165
+ 'main-thread:bindtap': { _wkltId: 'tap' },
166
+ 'main-thread:gesture': { __isGesture: true, id: 1, type: 0, callbacks: {} },
167
+ title: 'typed',
168
+ });
169
+
170
+ expect(typedPrepared).toEqual({ title: 'typed' });
171
+ });
172
+
173
+ it('prepares main-thread events, MTRef, and Gesture in compiled spread slots while ignoring inherited keys', () => {
174
+ const tapWorklet = { _wkltId: 'tap-wklt' };
175
+ const captureCatchWorklet = { _wkltId: 'capture-catch-wklt' };
176
+ const globalWorklet = { _wkltId: 'global-wklt' };
177
+ const mtRef = { _wvid: 9 };
178
+ const panGesture = {
179
+ __isGesture: true,
180
+ id: 3,
181
+ type: 0,
182
+ config: { minDistance: 8 },
183
+ callbacks: { onUpdate: { _wkltId: 'pan-update' } },
184
+ };
185
+
186
+ const rawSpread = Object.assign(
187
+ Object.create({
188
+ 'main-thread:bindinherited': { _wkltId: 'inherited-event' },
189
+ 'main-thread:ref': { _wvid: 99 },
190
+ 'main-thread:gesture': panGesture,
191
+ }) as Record<string, unknown>,
192
+ {
193
+ id: 'card',
194
+ 'main-thread:bindtap': tapWorklet,
195
+ 'main-thread:capture-catchtouchmove': captureCatchWorklet,
196
+ 'main-thread:global-bindCustom': globalWorklet,
197
+ 'main-thread:catchtouchend': null,
198
+ 'main-thread:ref': mtRef,
199
+ 'main-thread:gesture': panGesture,
200
+ },
201
+ );
202
+
203
+ const firstPrepared = prepareSpreadAttrSlot(-6, 1, rawSpread) as Record<string, any>;
204
+ expect(firstPrepared).toMatchObject({
205
+ id: 'card',
206
+ 'main-thread:bindtap': { type: 'worklet', value: { _wkltId: 'tap-wklt' } },
207
+ 'main-thread:capture-catchtouchmove': { type: 'worklet', value: { _wkltId: 'capture-catch-wklt' } },
208
+ 'main-thread:global-bindCustom': { type: 'worklet', value: { _wkltId: 'global-wklt' } },
209
+ 'main-thread:catchtouchend': null,
210
+ 'main-thread:ref': { type: 'main-thread-ref', value: mtRef },
211
+ 'main-thread:gesture': {
212
+ type: 'gesture',
213
+ value: [{ id: 3, type: 0, config: { minDistance: 8 } }],
214
+ },
215
+ });
216
+ expect(firstPrepared).not.toHaveProperty('main-thread:bindinherited');
217
+
218
+ // Re-preparing with identical raw callback identities reuses prepared wrappers.
219
+ const nextRawSpread = { ...rawSpread, id: 'card-next' };
220
+ const secondPrepared = prepareSpreadAttrSlot(-6, 1, nextRawSpread, {
221
+ previousPreparedSlots: [null, firstPrepared],
222
+ previousRawSlots: [null, rawSpread],
223
+ }) as Record<string, any>;
224
+ expect(secondPrepared['main-thread:bindtap']).toBe(firstPrepared['main-thread:bindtap']);
225
+ expect(secondPrepared['main-thread:ref']).toBe(firstPrepared['main-thread:ref']);
226
+ expect(
227
+ secondPrepared['main-thread:gesture'].value[0].callbacks[0].callback,
228
+ ).toBe(firstPrepared['main-thread:gesture'].value[0].callbacks[0].callback);
229
+
230
+ expect(
231
+ prepareSpreadAttrSlot(-6, 1, {
232
+ 'main-thread:ref': undefined,
233
+ 'main-thread:gesture': false,
234
+ 'main-thread:bindtap': undefined,
235
+ }),
236
+ ).toEqual({
237
+ 'main-thread:ref': null,
238
+ 'main-thread:gesture': null,
239
+ 'main-thread:bindtap': null,
240
+ });
241
+
242
+ expect(() => prepareSpreadAttrSlot(-6, 1, { 'main-thread:ref': vi.fn() })).toThrow(
243
+ 'ElementTemplate main-thread:ref expects a MainThreadRef object or a main-thread function.',
244
+ );
245
+ expect(() => prepareSpreadAttrSlot(-6, 1, { 'main-thread:gesture': {} })).toThrow(
246
+ 'ElementTemplate main-thread:gesture expects a Gesture object.',
247
+ );
248
+
249
+ const inheritedOnlySpread = Object.create({
250
+ 'main-thread:ref': mtRef,
251
+ 'main-thread:gesture': panGesture,
252
+ 'main-thread:bindtap': tapWorklet,
253
+ }) as Record<string, unknown>;
254
+ inheritedOnlySpread.id = 'own-only';
255
+ const inheritedPrepared = prepareSpreadAttrSlot(-6, 1, inheritedOnlySpread) as Record<string, any>;
256
+ expect(inheritedPrepared).toEqual({ id: 'own-only' });
257
+
258
+ const addedMtsRaw = {
259
+ id: 'added-mts',
260
+ 'main-thread:ref': mtRef,
261
+ 'main-thread:gesture': panGesture,
262
+ 'main-thread:bindtap': tapWorklet,
263
+ };
264
+ const addedFromEmpty = prepareSpreadAttrSlot(-6, 1, addedMtsRaw, {
265
+ previousPreparedSlots: [null, { id: 'own-only', 'main-thread:gesture': null }],
266
+ previousRawSlots: [null, { id: 'own-only', 'main-thread:gesture': null }],
267
+ }) as Record<string, any>;
268
+ expect(addedFromEmpty['main-thread:ref']).toEqual({ type: 'main-thread-ref', value: mtRef });
269
+ expect(addedFromEmpty['main-thread:gesture']).toMatchObject({ type: 'gesture' });
270
+ expect(addedFromEmpty['main-thread:bindtap']).toMatchObject({ type: 'worklet' });
271
+ });
272
+
273
+ it('reports invalid main-thread event handlers in development and normalizes them to null', () => {
274
+ const oldReportError = lynx.reportError;
275
+ const reportError = vi.fn();
276
+ lynx.reportError = reportError;
277
+ const originalDev = globalThis.__DEV__;
278
+
279
+ try {
280
+ globalThis.__DEV__ = true;
281
+ expect(prepareSpreadAttrSlot(-7, 2, { 'main-thread:bindtap': () => {} })).toEqual({
282
+ 'main-thread:bindtap': null,
283
+ });
284
+ expect(reportError).toHaveBeenCalledTimes(1);
285
+ expect(String(reportError.mock.calls[0]?.[0]?.message ?? '')).toContain(
286
+ 'ElementTemplate main-thread event slot -7:2 (main-thread:bindtap) expects a worklet ctx object.',
287
+ );
288
+
289
+ reportError.mockClear();
290
+ globalThis.__DEV__ = false;
291
+ expect(prepareSpreadAttrSlot(-7, 2, { 'main-thread:bindtap': () => {} })).toEqual({
292
+ 'main-thread:bindtap': null,
293
+ });
294
+ expect(reportError).not.toHaveBeenCalled();
295
+ } finally {
296
+ globalThis.__DEV__ = originalDev;
297
+ lynx.reportError = oldReportError;
298
+ }
299
+ });
300
+
301
+ it('re-prepares MTEvent, MTRef, and Gesture when caller mutates a reused spread object in place', () => {
302
+ const firstTap = { _wkltId: 'first-tap' };
303
+ const nextTap = { _wkltId: 'next-tap' };
304
+ const firstRef = { _wvid: 1 };
305
+ const nextRef = { _wvid: 2 };
306
+ const firstGestureCb = { _wkltId: 'first-pan' };
307
+ const nextGestureCb = { _wkltId: 'next-pan' };
308
+
309
+ const spread: Record<string, any> = {
310
+ id: 'mutable',
311
+ 'main-thread:bindtap': firstTap,
312
+ 'main-thread:ref': firstRef,
313
+ 'main-thread:gesture': {
314
+ __isGesture: true,
315
+ id: 1,
316
+ type: 0,
317
+ callbacks: { onUpdate: firstGestureCb },
318
+ },
319
+ };
320
+
321
+ const firstPrepared = prepareSpreadAttrSlot(1, 0, spread) as Record<string, any>;
322
+
323
+ // Mutate the same spread object and nested gesture callbacks in place.
324
+ spread['main-thread:bindtap'] = nextTap;
325
+ spread['main-thread:ref'] = nextRef;
326
+ spread['main-thread:gesture'].callbacks.onUpdate = nextGestureCb;
327
+
328
+ const secondPrepared = prepareSpreadAttrSlot(1, 0, spread, {
329
+ previousRawSlots: [spread],
330
+ previousPreparedSlots: [firstPrepared],
331
+ }) as Record<string, any>;
332
+
333
+ expect(secondPrepared['main-thread:bindtap']).not.toBe(firstPrepared['main-thread:bindtap']);
334
+ expect(secondPrepared['main-thread:bindtap'].value._wkltId).toBe('next-tap');
335
+ expect(secondPrepared['main-thread:ref']).not.toBe(firstPrepared['main-thread:ref']);
336
+ expect(secondPrepared['main-thread:ref'].value._wvid).toBe(2);
337
+ expect(secondPrepared['main-thread:gesture'].value[0].callbacks[0].callback._wkltId).toBe('next-pan');
159
338
  });
160
339
  });
@@ -512,7 +512,7 @@ describe('direct renderer native contracts', () => {
512
512
  __etAttrPlanMap._et_spread = [0, adaptSpreadAttrSlot];
513
513
 
514
514
  renderToElementTemplate(
515
- h('_et_spread', { attributeSlots: [{ id: 'cta', ref, 'main-thread:ref': vi.fn(), 'worklet:ref': vi.fn() }] }),
515
+ h('_et_spread', { attributeSlots: [{ id: 'cta', ref, 'custom:ref': vi.fn(), 'worklet:ref': vi.fn() }] }),
516
516
  );
517
517
 
518
518
  expect(createElementTemplate).toHaveBeenCalledWith(
@@ -524,4 +524,24 @@ describe('direct renderer native contracts', () => {
524
524
  );
525
525
  expect(ref).not.toHaveBeenCalled();
526
526
  });
527
+
528
+ it('strips spread main-thread:ref from native create while adding layout-only and flatten guards', () => {
529
+ const rootRef = { kind: 'root-ref' };
530
+ const mtRef = { _wvid: 1 };
531
+ createElementTemplate.mockReturnValue(rootRef);
532
+ __etAttrPlanMap['__Card__:_et_spread'] = [0, adaptSpreadAttrSlot];
533
+
534
+ const rendered = renderToElementTemplate(
535
+ h('__Card__:_et_spread', { attributeSlots: [{ id: 'cta', 'main-thread:ref': mtRef }] }),
536
+ );
537
+
538
+ expect(createElementTemplate).toHaveBeenCalledWith(
539
+ '_et_spread',
540
+ null,
541
+ [{ id: 'cta', 'has-react-ref': true, flatten: false }],
542
+ null,
543
+ -1,
544
+ );
545
+ expect(rendered.rootSubtreeHandles).toEqual([[{ uid: -1, ref: rootRef }]]);
546
+ });
527
547
  });
@@ -9,6 +9,7 @@ import {
9
9
  __etAttrPlanMap,
10
10
  adaptMTEventAttrSlot,
11
11
  adaptMTRefAttrSlot,
12
+ adaptSpreadAttrSlot,
12
13
  clearEtAttrPlanMap,
13
14
  } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
14
15
  import {
@@ -19,9 +20,12 @@ import {
19
20
  detachMainThreadDynamicAttrRefsForSubtree,
20
21
  getMainThreadDynamicAttrState,
21
22
  initializeMainThreadDynamicAttrSlots,
23
+ isSpreadDynamicAttrSlot,
22
24
  prepareMainThreadDynamicAttrSlotsForNative,
25
+ prepareSpreadAttrSlotForNative,
23
26
  updateMainThreadEventAttrSlot as setMTEventSlot,
24
27
  updateMainThreadRefAttrSlot as setMTRefSlotImpl,
28
+ updateSpreadAttrSlot,
25
29
  } from '../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
26
30
 
27
31
  import { retainMainThreadBackgroundFunctionCtx } from '../../../../src/element-template/runtime/template/main-thread-background-function.js';
@@ -659,4 +663,188 @@ describe('main-thread dynamic attr state', () => {
659
663
  globalThis.lynxWorkletImpl = previousWorkletImpl;
660
664
  }
661
665
  });
666
+
667
+ it('retains spread MTEvent, Gesture, and callback MTRef once before Native and keeps state intact on Native rejection', () => {
668
+ const { addRef, restore } = installJsFunctionLifecycleManager();
669
+ const { updateWorkletRef, restore: restoreRefs } = installRefRuntime();
670
+ const eventCtx = { _execId: 201, _wkltId: 'spread-tap' };
671
+ const gestureCb = { _execId: 202, _wkltId: 'spread-pan' };
672
+ const refCb = { _execId: 203, _wkltId: 'spread-ref-cb' };
673
+ const initialObjectRef = { _wvid: 55 };
674
+ __etAttrPlanMap._et_spread_retain = [0, adaptSpreadAttrSlot];
675
+
676
+ try {
677
+ const initialSlot = {
678
+ id: 'card',
679
+ 'main-thread:bindtap': { type: 'worklet', value: eventCtx },
680
+ 'main-thread:gesture': {
681
+ type: 'gesture',
682
+ value: [{
683
+ id: 1,
684
+ type: 0,
685
+ config: {},
686
+ callbacks: [{ name: 'onUpdate', callback: gestureCb }],
687
+ relationMap: { waitFor: [], simultaneous: [], continueWith: [] },
688
+ }],
689
+ },
690
+ 'main-thread:ref': { type: 'main-thread-ref', value: initialObjectRef },
691
+ };
692
+
693
+ const nativeSlots = prepareMainThreadDynamicAttrSlotsForNative('_et_spread_retain', [initialSlot]);
694
+ expect(nativeSlots?.[0]).toEqual({
695
+ id: 'card',
696
+ 'main-thread:bindtap': { type: 'worklet', value: eventCtx },
697
+ 'main-thread:gesture': initialSlot['main-thread:gesture'],
698
+ 'has-react-ref': true,
699
+ flatten: false,
700
+ });
701
+ expect(addRef.mock.calls).toEqual([
702
+ [201, eventCtx],
703
+ [202, gestureCb],
704
+ ]);
705
+
706
+ initializeMainThreadDynamicAttrSlots(17, '_et_spread_retain', [initialSlot]);
707
+ expect(isSpreadDynamicAttrSlot(17, 0)).toBe(true);
708
+ expect(addRef).toHaveBeenCalledTimes(2);
709
+ attachAndComplete([{ uid: 17, ref: TEST_NATIVE_REF }]);
710
+ expect(updateWorkletRef).toHaveBeenCalledExactlyOnceWith(initialObjectRef, TEST_ELEMENT_REF);
711
+ updateWorkletRef.mockClear();
712
+
713
+ // prepareSpreadAttrSlotForNative retains callbacks and strips main-thread:ref without mutating attached ref state.
714
+ const unappliedNextSlot = {
715
+ id: 'card-rejected',
716
+ 'main-thread:ref': { type: 'main-thread-ref', value: refCb },
717
+ };
718
+ expect(prepareSpreadAttrSlotForNative(null)).toBeNull();
719
+ const preparedForNative = prepareSpreadAttrSlotForNative(unappliedNextSlot);
720
+ expect(preparedForNative).toEqual({
721
+ id: 'card-rejected',
722
+ 'has-react-ref': true,
723
+ flatten: false,
724
+ });
725
+ expect(addRef).toHaveBeenLastCalledWith(203, refCb);
726
+ expect(updateWorkletRef).not.toHaveBeenCalled();
727
+
728
+ // If hydration completes without a setAttribute op for this spread slot (because BTS omitted
729
+ // 'main-thread:ref' and remaining keys matched Native), the unhydrated first-screen ref is cleaned up.
730
+ applyPendingMainThreadRefAttachments(true);
731
+ expect(updateWorkletRef).toHaveBeenCalledExactlyOnceWith(initialObjectRef, null);
732
+ updateWorkletRef.mockClear();
733
+
734
+ deleteMainThreadDynamicAttrStateForSubtree([17]);
735
+ expect(updateWorkletRef).not.toHaveBeenCalled();
736
+ } finally {
737
+ restoreRefs();
738
+ restore();
739
+ }
740
+ });
741
+
742
+ it('reconciles multi-slot MTRef overrides, key-deletion fallbacks, and JSON.parse sibling updates without disturbing distinct elements', () => {
743
+ const previousRunWorklet = globalThis.runWorklet;
744
+ const { updateWorkletRef, restore } = installRefRuntime();
745
+ const sharedElement = { id: 'shared-el' } as unknown as ElementRef;
746
+ const distinctElement = { id: 'distinct-el' } as unknown as ElementRef;
747
+ vi.mocked(__GetElementByAttrSlot).mockImplementation((_handle, slot) =>
748
+ slot === 3 ? distinctElement : sharedElement
749
+ );
750
+
751
+ const directRef = { _wvid: 1 };
752
+ const distinctRef = { _wvid: 99 };
753
+ const spread1Cleanup = vi.fn();
754
+ const spread1Cb = { _wkltId: 'spread1-cb', _execId: 301 };
755
+ const spread2Ref = { _wvid: 2 };
756
+ globalThis.runWorklet = vi.fn(() => spread1Cleanup);
757
+
758
+ __etAttrPlanMap._et_multi_mtref = [
759
+ 0,
760
+ adaptMTRefAttrSlot,
761
+ 1,
762
+ adaptSpreadAttrSlot,
763
+ 2,
764
+ adaptSpreadAttrSlot,
765
+ 3,
766
+ adaptSpreadAttrSlot,
767
+ ];
768
+
769
+ try {
770
+ initializeMainThreadDynamicAttrSlots(21, '_et_multi_mtref', [
771
+ { type: 'main-thread-ref', value: directRef },
772
+ { id: 's1', 'main-thread:ref': { type: 'main-thread-ref', value: spread1Cb } },
773
+ { id: 's2', 'main-thread:ref': { type: 'main-thread-ref', value: spread2Ref } },
774
+ { id: 's3', 'main-thread:ref': { type: 'main-thread-ref', value: distinctRef } },
775
+ ]);
776
+ attachAndComplete([{ uid: 21, ref: TEST_NATIVE_REF }]);
777
+
778
+ // Slot 2 overrides slots 0 & 1 on sharedElement; slot 3 attaches independently on distinctElement.
779
+ expect(updateWorkletRef.mock.calls).toEqual([
780
+ [spread2Ref, sharedElement],
781
+ [distinctRef, distinctElement],
782
+ ]);
783
+ expect(globalThis.runWorklet).not.toHaveBeenCalled();
784
+ updateWorkletRef.mockClear();
785
+
786
+ // Explicit null in slot 2 ({ 'main-thread:ref': undefined }) detaches sharedElement without falling back.
787
+ updateSpreadAttrSlot(21, 2, { id: 's2', 'main-thread:ref': null }, TEST_NATIVE_REF);
788
+ completeRefAttachments();
789
+ expect(updateWorkletRef.mock.calls).toEqual([[spread2Ref, null]]);
790
+ expect(globalThis.runWorklet).not.toHaveBeenCalled();
791
+ updateWorkletRef.mockClear();
792
+
793
+ // Omitting 'main-thread:ref' from slot 2 falls back to slot 1's callback ref.
794
+ updateSpreadAttrSlot(21, 2, { id: 's2-no-ref' }, TEST_NATIVE_REF);
795
+ completeRefAttachments();
796
+ expect(globalThis.runWorklet).toHaveBeenCalledExactlyOnceWith(spread1Cb, [{ elementRefptr: sharedElement }]);
797
+ expect(spread1Cleanup).not.toHaveBeenCalled();
798
+ vi.mocked(globalThis.runWorklet).mockClear();
799
+
800
+ // Sibling update in slot 1 arriving as a fresh JSON.parse clone with the same _execId does NOT remount spread1Cb.
801
+ const jsonParsedSameCbClone = JSON.parse(JSON.stringify({
802
+ id: 's1-updated',
803
+ 'main-thread:ref': { type: 'main-thread-ref', value: { _wkltId: 'spread1-cb', _execId: 301 } },
804
+ }));
805
+ updateSpreadAttrSlot(21, 1, jsonParsedSameCbClone, TEST_NATIVE_REF);
806
+ completeRefAttachments();
807
+ expect(spread1Cleanup).not.toHaveBeenCalled();
808
+ expect(globalThis.runWorklet).not.toHaveBeenCalled();
809
+
810
+ // Clearing slot 1 entirely runs spread1Cleanup synchronously and falls back to direct slot 0!
811
+ updateSpreadAttrSlot(21, 1, null, TEST_NATIVE_REF);
812
+ expect(spread1Cleanup).toHaveBeenCalledTimes(1);
813
+ completeRefAttachments();
814
+ expect(updateWorkletRef.mock.calls).toEqual([[directRef, sharedElement]]);
815
+ updateWorkletRef.mockClear();
816
+
817
+ // Subtree deletion cleans both sharedElement (directRef) and distinctElement (distinctRef).
818
+ deleteMainThreadDynamicAttrStateForSubtree([21]);
819
+ expect(updateWorkletRef.mock.calls).toEqual(expect.arrayContaining([
820
+ [directRef, null],
821
+ [distinctRef, null],
822
+ ]));
823
+ expect(updateWorkletRef).toHaveBeenCalledTimes(2);
824
+ expect(spread1Cleanup).toHaveBeenCalledTimes(1);
825
+
826
+ // Masking an attached callback ref via a later spread's explicit null cleans it once
827
+ // and does not re-run cleanup on subtree deletion.
828
+ const maskedCleanup = vi.fn();
829
+ const maskedCb = { _wkltId: 'masked-cb', _execId: 302 };
830
+ globalThis.runWorklet = vi.fn(() => maskedCleanup);
831
+ __etAttrPlanMap._et_masked_destroy = [0, adaptSpreadAttrSlot, 1, adaptSpreadAttrSlot];
832
+ initializeMainThreadDynamicAttrSlots(77, '_et_masked_destroy', [
833
+ { 'main-thread:ref': { type: 'main-thread-ref', value: maskedCb } },
834
+ {},
835
+ ]);
836
+ attachAndComplete([{ uid: 77, ref: TEST_NATIVE_REF }]);
837
+ expect(maskedCleanup).toHaveBeenCalledTimes(0);
838
+
839
+ updateSpreadAttrSlot(77, 1, { 'main-thread:ref': null }, TEST_NATIVE_REF);
840
+ completeRefAttachments();
841
+ expect(maskedCleanup).toHaveBeenCalledTimes(1);
842
+
843
+ deleteMainThreadDynamicAttrStateForSubtree([77]);
844
+ expect(maskedCleanup).toHaveBeenCalledTimes(1);
845
+ } finally {
846
+ globalThis.runWorklet = previousRunWorklet;
847
+ restore();
848
+ }
849
+ });
662
850
  });
@@ -3,6 +3,7 @@ import ReactLynx from '@lynx-js/react';
3
3
  import {
4
4
  startTransition as preactStartTransition,
5
5
  use as preactUse,
6
+ useDeferredValue as preactUseDeferredValue,
6
7
  useTransition as preactUseTransition,
7
8
  } from 'preact/compat';
8
9
 
@@ -21,6 +22,11 @@ describe('Default export', () => {
21
22
  expect(compat.use).toBe(preactUse);
22
23
  });
23
24
 
25
+ it('should include useDeferredValue', () => {
26
+ expect(compat).toHaveProperty('useDeferredValue');
27
+ expect(compat.useDeferredValue).toBe(preactUseDeferredValue);
28
+ });
29
+
24
30
  it('should include useInsertionEffect', () => {
25
31
  expect(compat).toHaveProperty('useInsertionEffect');
26
32
  expect(compat.useInsertionEffect).toBe(ReactLynx.useEffect);
@@ -35,8 +41,8 @@ describe('Default export', () => {
35
41
  });
36
42
 
37
43
  it('should have correct number of exports', () => {
38
- // +4 for startTransition, use, useInsertionEffect and useTransition
39
- const expectedExportCount = Object.keys(ReactLynx).length + 4;
44
+ // +5 for startTransition, use, useDeferredValue, useInsertionEffect and useTransition
45
+ const expectedExportCount = Object.keys(ReactLynx).length + 5;
40
46
  expect(Object.keys(compat).length).toBe(expectedExportCount);
41
47
  });
42
48
  });
@@ -15,13 +15,13 @@ beforeAll(() => {
15
15
 
16
16
  beforeEach(() => {
17
17
  globalEnvManager.resetEnv();
18
- globalThis.__GLOBAL_PROPS_MODE__ = 'event';
18
+ lynx.__runtime_configs__ = { globalPropsMode: 'event' };
19
19
  });
20
20
 
21
21
  afterEach(() => {
22
22
  elementTree.clear();
23
23
  vi.restoreAllMocks();
24
- globalThis.__GLOBAL_PROPS_MODE__ = 'reactive';
24
+ lynx.__runtime_configs__ = { globalPropsMode: 'reactive' };
25
25
  });
26
26
 
27
27
  async function renderWithCurrentPreact(element, root) {
@@ -24,7 +24,7 @@ beforeEach(() => {
24
24
  afterEach(() => {
25
25
  elementTree.clear();
26
26
  vi.restoreAllMocks();
27
- globalThis.__GLOBAL_PROPS_MODE__ = 'reactive';
27
+ lynx.__runtime_configs__ = { globalPropsMode: 'reactive' };
28
28
  });
29
29
 
30
30
  async function renderWithCurrentPreact(element, root) {
@@ -308,8 +308,8 @@ describe('updateGlobalProps', () => {
308
308
  }
309
309
  });
310
310
 
311
- it('should not trigger re-render when __GLOBAL_PROPS_MODE__ is event', async () => {
312
- globalThis.__GLOBAL_PROPS_MODE__ = 'event';
311
+ it('should not trigger re-render when globalPropsMode is event', async () => {
312
+ lynx.__runtime_configs__ = { globalPropsMode: 'event' };
313
313
 
314
314
  lynx.__globalProps = { theme: 'dark' };
315
315
  const Comp = () => {
@@ -7,6 +7,7 @@ import { render } from 'preact';
7
7
  import { afterEach, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
8
8
 
9
9
  import { RefProxy, runDelayedUiOps, shouldDelayUiOps } from '../../src/snapshot/lifecycle/ref/delay';
10
+ import { destroyBackground } from '../../src/snapshot/lifecycle/destroy';
10
11
 
11
12
  import { Component, createRef, useState } from '../../src/index';
12
13
  import { clearCommitTaskId, replaceCommitHook } from '../../src/snapshot/lifecycle/patch/commit';
@@ -2365,4 +2366,33 @@ describe('runDelayedUiOps helper', () => {
2365
2366
  runDelayedUiOps();
2366
2367
  expect(shouldDelayUiOps.value).toBe(false);
2367
2368
  });
2369
+
2370
+ it('should clear pre-hydration delayed UI operations on destroyBackground even when unmount cleanup throws', () => {
2371
+ shouldDelayUiOps.value = true;
2372
+ const invoke = vi.fn();
2373
+ vi.spyOn(lynx, 'createSelectorQuery').mockReturnValue({
2374
+ selectUniqueID: () => ({ invoke }),
2375
+ });
2376
+
2377
+ const proxy = new RefProxy([1, 0]);
2378
+ proxy.invoke({ method: 'focus' }).exec();
2379
+ expect(invoke).not.toHaveBeenCalled();
2380
+
2381
+ class ThrowingUnmount extends Component {
2382
+ componentWillUnmount() {
2383
+ throw new Error('unmount boom');
2384
+ }
2385
+ render() {
2386
+ return <view />;
2387
+ }
2388
+ }
2389
+
2390
+ globalEnvManager.switchToBackground();
2391
+ render(<ThrowingUnmount />, __root);
2392
+
2393
+ expect(() => destroyBackground()).toThrow('unmount boom');
2394
+ runDelayedUiOps();
2395
+
2396
+ expect(invoke).not.toHaveBeenCalled();
2397
+ });
2368
2398
  });
@@ -61,9 +61,11 @@ describe('worklet-runtime init entry', () => {
61
61
  });
62
62
 
63
63
  it('initializes the main-thread runtime through the public entry', async () => {
64
+ globalThis.lynx.SystemInfo = { lynxSdkVersion: '2.15' };
64
65
  const { registerWorklet } = await import('@lynx-js/react/worklet-runtime/init');
65
66
 
66
67
  expect(globalThis.lynxWorkletImpl).toBeDefined();
68
+ expect(globalThis.lynxWorkletImpl._jsFunctionLifecycleManager).toBeDefined();
67
69
  expect(globalThis.registerWorklet).toBeTypeOf('function');
68
70
  expect(globalThis.registerWorkletInternal).toBeTypeOf('function');
69
71
  expect(globalThis.runWorklet).toBeTypeOf('function');
@@ -75,6 +77,16 @@ describe('worklet-runtime init entry', () => {
75
77
  expect(globalThis.runWorklet({ _wkltId: 'cold-entry' }, [41])).toBe(42);
76
78
  });
77
79
 
80
+ it('uses host SystemInfo when initialized before the global alias', async () => {
81
+ delete globalThis.SystemInfo;
82
+ globalThis.lynx.SystemInfo = { lynxSdkVersion: '2.16' };
83
+
84
+ await import('@lynx-js/react/worklet-runtime/init');
85
+
86
+ expect(globalThis.lynxWorkletImpl._jsFunctionLifecycleManager).toBeDefined();
87
+ expect('SystemInfo' in globalThis).toBe(false);
88
+ });
89
+
78
90
  it('preserves runtime state when the init entry is evaluated again', async () => {
79
91
  const initialEntry = await import('@lynx-js/react/worklet-runtime/init');
80
92
  expect(initialEntry.registerWorklet).toBe(globalThis.registerWorkletInternal);
@@ -51,6 +51,21 @@ declare function useTransition(): [false, typeof startTransition];
51
51
  */
52
52
  declare function use<T>(resource: Promise<T> | Context<T>): T;
53
53
 
54
+ /**
55
+ * Returns a deferred copy of a value.
56
+ *
57
+ * Note: This is Preact's `useDeferredValue` implementation, which differs from React's:
58
+ * - React defers the value with interruptible, low-priority rendering
59
+ * - Preact returns the previous value first and updates it in a follow-up render
60
+ * once the current one has finished, without true concurrent rendering
61
+ *
62
+ * @param value - The value to defer
63
+ * @returns The deferred value
64
+ *
65
+ * @public
66
+ */
67
+ declare function useDeferredValue<T>(value: T): T;
68
+
54
69
  /**
55
70
  * Runs an effect whose write is meant to be visible to the effects that read it.
56
71
  *
@@ -75,12 +90,13 @@ declare function use<T>(resource: Promise<T> | Context<T>): T;
75
90
  * @deprecated `useInsertionEffect` in the background thread is an alias of `useEffect` and cannot offer React's insertion-phase timing: the effect has not run when `render` returns, and its cleanup is deferred to a later flush instead of running inside the unmount commit.
76
91
  */
77
92
  declare const useInsertionEffect: typeof import('@lynx-js/react').useEffect;
78
- export { startTransition, use, useInsertionEffect, useTransition };
93
+ export { startTransition, use, useDeferredValue, useInsertionEffect, useTransition };
79
94
 
80
95
  // type for the default export
81
96
  declare const _default: typeof import('@lynx-js/react') & {
82
97
  startTransition: typeof startTransition;
83
98
  use: typeof use;
99
+ useDeferredValue: typeof useDeferredValue;
84
100
  useInsertionEffect: typeof useInsertionEffect;
85
101
  useTransition: typeof useTransition;
86
102
  };
@@ -1,7 +1,7 @@
1
1
  // Copyright 2025 The Lynx Authors. All rights reserved.
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
- import { startTransition, use, useTransition } from 'preact/compat';
4
+ import { startTransition, use, useDeferredValue, useTransition } from 'preact/compat';
5
5
 
6
6
  /* eslint-disable-next-line import/default */
7
7
  import ReactLynx, { useEffect } from '@lynx-js/react';
@@ -11,9 +11,10 @@ const useInsertionEffect = useEffect;
11
11
  export default /*#__PURE__*/ Object.assign({}, ReactLynx, {
12
12
  startTransition,
13
13
  use,
14
+ useDeferredValue,
14
15
  useInsertionEffect,
15
16
  useTransition,
16
17
  });
17
18
 
18
19
  export * from '@lynx-js/react';
19
- export { startTransition, use, useInsertionEffect, useTransition };
20
+ export { startTransition, use, useDeferredValue, useInsertionEffect, useTransition };
@@ -54,6 +54,7 @@ export const {
54
54
  // compat
55
55
  startTransition,
56
56
  use,
57
+ useDeferredValue,
57
58
  useInsertionEffect,
58
59
  useTransition,
59
60
  } = target[sExportsReactCompat];
@@ -26,6 +26,7 @@ export const {
26
26
  __etHost,
27
27
  __root,
28
28
  adaptEventAttrSlot,
29
+ adaptGestureAttrSlot,
29
30
  adaptMTEventAttrSlot,
30
31
  adaptMTRefAttrSlot,
31
32
  adaptRefAttrSlot,