@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,3 +1,4 @@
1
+ import { createElement } from 'preact/compat';
1
2
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
3
 
3
4
  import { getReloadVersion } from '../../../src/core/reload-version.js';
@@ -8,19 +9,36 @@ import {
8
9
  globalCommitContext,
9
10
  markRemovedSubtreeForPostDispatchTeardown,
10
11
  } from '../../../src/element-template/background/commit-context.js';
11
- import { resetElementTemplateCommitState } from '../../../src/element-template/background/commit-hook.js';
12
+ import {
13
+ installElementTemplateCommitHook,
14
+ markElementTemplateHydrated,
15
+ resetElementTemplateCommitState,
16
+ uninstallElementTemplateCommitHookForTesting,
17
+ } from '../../../src/element-template/background/commit-hook.js';
18
+ import { setupBackgroundElementTemplateDocument } from '../../../src/element-template/background/document.js';
12
19
  import {
13
20
  installElementTemplateHydrationListener,
14
21
  resetElementTemplateHydrationListener,
15
22
  } from '../../../src/element-template/background/hydration-listener.js';
16
- import { BackgroundElementTemplateInstance } from '../../../src/element-template/background/instance.js';
23
+ import {
24
+ BackgroundElementTemplateInstance,
25
+ BackgroundPageRootInstance,
26
+ } from '../../../src/element-template/background/instance.js';
17
27
  import { backgroundElementTemplateInstanceManager } from '../../../src/element-template/background/manager.js';
28
+ import { root, useEffect } from '../../../src/element-template/index.js';
29
+ import { clearRefState } from '../../../src/element-template/prop-adapters/ref.js';
18
30
  import { ElementTemplateLifecycleConstant } from '../../../src/element-template/protocol/lifecycle-constant.js';
19
31
  import { ElementTemplateUpdateOps } from '../../../src/element-template/protocol/opcodes.js';
20
32
  import type { SerializedCompiledNode } from '../../../src/element-template/protocol/types.js';
21
33
  import { callDestroyLifetimeFun } from '../../../src/element-template/native/callDestroyLifetimeFun.js';
22
- import { __root } from '../../../src/element-template/runtime/page/root-instance.js';
34
+ import { __root, setRoot } from '../../../src/element-template/runtime/page/root-instance.js';
35
+ import {
36
+ __etAttrPlanMap,
37
+ adaptRefAttrSlot,
38
+ clearEtAttrPlanMap,
39
+ } from '../../../src/element-template/runtime/template/attr-slot-plan.js';
23
40
  import { ElementTemplateEnvManager } from '../test-utils/debug/envManager.js';
41
+ import { resetPerformanceMocks } from '../test-utils/mock/performance.js';
24
42
 
25
43
  describe('callDestroyLifetimeFun', () => {
26
44
  const envManager = new ElementTemplateEnvManager();
@@ -36,8 +54,11 @@ describe('callDestroyLifetimeFun', () => {
36
54
  });
37
55
 
38
56
  afterEach(() => {
57
+ uninstallElementTemplateCommitHookForTesting();
39
58
  resetElementTemplateHydrationListener();
40
59
  resetElementTemplateCommitState();
60
+ clearRefState();
61
+ clearEtAttrPlanMap();
41
62
  takeMainThreadRefInitValuePatch();
42
63
  });
43
64
 
@@ -104,4 +125,69 @@ describe('callDestroyLifetimeFun', () => {
104
125
  expect(backgroundElementTemplateInstanceManager.get(-1)).toBeUndefined();
105
126
  expect(backgroundElementTemplateInstanceManager.values.size).toBe(0);
106
127
  });
128
+
129
+ it('unmounts a hydrated tree without dispatching removeNode patches and discards MainThreadRef init created in ref detach', () => {
130
+ setRoot(new BackgroundPageRootInstance());
131
+ setupBackgroundElementTemplateDocument();
132
+ installElementTemplateCommitHook();
133
+ const dispatch = vi.spyOn(lynx.getCoreContext(), 'dispatchEvent');
134
+
135
+ const refCalls: unknown[] = [];
136
+ const callbackRef = (value: unknown) => {
137
+ refCalls.push(value);
138
+ if (value === null) {
139
+ new MainThreadRef('created-during-destroy-detach');
140
+ }
141
+ };
142
+
143
+ root.render(createElement('view', null));
144
+ const oldView = (__root as BackgroundElementTemplateInstance).firstChild!;
145
+ __etAttrPlanMap[oldView.type] = [0, adaptRefAttrSlot];
146
+ oldView.setAttribute('attributeSlots', [callbackRef]);
147
+ oldView.markMaterializedByHydration();
148
+ markElementTemplateHydrated();
149
+ dispatch.mockClear();
150
+
151
+ vi.useFakeTimers();
152
+ try {
153
+ callDestroyLifetimeFun();
154
+
155
+ expect(refCalls).toContain(null);
156
+ expect(dispatch).not.toHaveBeenCalled();
157
+ expect(vi.getTimerCount()).toBe(0);
158
+ expect(takeMainThreadRefInitValuePatch()).toEqual([]);
159
+ expect(backgroundElementTemplateInstanceManager.values.size).toBe(0);
160
+ } finally {
161
+ vi.useRealTimers();
162
+ }
163
+ });
164
+
165
+ it('cleans up background runtime state in finally even when unmount effect cleanup throws', async () => {
166
+ setRoot(new BackgroundPageRootInstance());
167
+ setupBackgroundElementTemplateDocument();
168
+ installElementTemplateCommitHook();
169
+
170
+ const cleanupError = new Error('unmount effect cleanup failure');
171
+ function ThrowingOnCleanup() {
172
+ useEffect(() => {
173
+ return () => {
174
+ new MainThreadRef('created-before-throw');
175
+ throw cleanupError;
176
+ };
177
+ }, []);
178
+ return createElement('view', null);
179
+ }
180
+
181
+ root.render(createElement(ThrowingOnCleanup, null));
182
+ await Promise.resolve();
183
+ markElementTemplateHydrated();
184
+
185
+ try {
186
+ expect(() => callDestroyLifetimeFun()).toThrow(cleanupError);
187
+ expect(backgroundElementTemplateInstanceManager.values.size).toBe(0);
188
+ expect(takeMainThreadRefInitValuePatch()).toEqual([]);
189
+ } finally {
190
+ resetPerformanceMocks();
191
+ }
192
+ });
107
193
  });
@@ -229,10 +229,15 @@ describe('mts-destroy', () => {
229
229
  },
230
230
  ],
231
231
  );
232
+ vi.stubGlobal(
233
+ 'runWorklet',
234
+ vi.fn(() => () => {
235
+ throw cleanupError;
236
+ }),
237
+ );
232
238
  attachMainThreadDynamicAttrRefsForSubtree([{ uid: -1, ref: registryRef }]);
233
- throwingRef._unmount = vi.fn(() => {
234
- throw cleanupError;
235
- });
239
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue({ id: 'actual-element' } as unknown as ElementRef);
240
+ applyPendingMainThreadRefAttachments();
236
241
 
237
242
  expect(() => onMtsDestruction()).toThrow(cleanupError);
238
243
  expect(getMainThreadDynamicAttrState(-1, 0)).toBeUndefined();
@@ -569,6 +569,84 @@ describe('ElementTemplate reloadMainThread', () => {
569
569
  expect(lynx.__initData).toEqual({ msg: 'reset' });
570
570
  });
571
571
 
572
+ it('completes main-thread reload teardown and resets template id when subtree ref cleanup throws', () => {
573
+ const firstRootRef = { type: 'first-root' } as unknown as ElementTemplateHandle;
574
+ const secondRootRef = { type: 'second-root' } as unknown as ElementTemplateHandle;
575
+ const firstCleanupError = new Error('first subtree ref cleanup failed');
576
+ const secondCleanupError = new Error('second subtree ref cleanup failed');
577
+ const firstThrowingRef: { _wkltId: string; _unmount?: () => void } = { _wkltId: 'first-ref' };
578
+ const secondThrowingRef: { _wkltId: string; _unmount?: () => void } = { _wkltId: 'second-ref' };
579
+ __etAttrPlanMap._et_first_root = [0, adaptMTRefAttrSlot];
580
+ __etAttrPlanMap._et_second_root = [0, adaptMTRefAttrSlot];
581
+ initializeMainThreadDynamicAttrSlots(-1, '_et_first_root', [{ type: 'main-thread-ref', value: firstThrowingRef }]);
582
+ initializeMainThreadDynamicAttrSlots(-2, '_et_second_root', [{
583
+ type: 'main-thread-ref',
584
+ value: secondThrowingRef,
585
+ }]);
586
+ attachMainThreadDynamicAttrRefsForSubtree([
587
+ { uid: -1, ref: firstRootRef },
588
+ { uid: -2, ref: secondRootRef },
589
+ ]);
590
+ vi.mocked(__GetElementByAttrSlot).mockReturnValue({ id: 'actual-element' } as unknown as ElementRef);
591
+ applyPendingMainThreadRefAttachments();
592
+ firstThrowingRef._unmount = vi.fn(() => {
593
+ throw firstCleanupError;
594
+ });
595
+ secondThrowingRef._unmount = vi.fn(() => {
596
+ throw secondCleanupError;
597
+ });
598
+ const nativeRefs = new Map([
599
+ [-1, firstRootRef],
600
+ [-2, secondRootRef],
601
+ ]);
602
+ vi.mocked(elementTemplateRegistry.get).mockImplementation(id => nativeRefs.get(id));
603
+ vi.mocked(__SerializeElementTemplate).mockReturnValueOnce({
604
+ tag: 'page',
605
+ attributes: null,
606
+ childSlots: [[
607
+ { templateKey: '_et_first_root', uid: -1 },
608
+ { templateKey: '_et_second_root', uid: -2 },
609
+ ]],
610
+ uid: 0,
611
+ } as ReturnType<typeof __SerializeElementTemplate>);
612
+
613
+ expect(() => reloadMainThread(undefined, { reloadTemplate: true })).toThrow(firstCleanupError);
614
+ expect(elementTemplateRegistry.clear).toHaveBeenCalledTimes(1);
615
+ expect(resetTemplateId).toHaveBeenCalledTimes(1);
616
+ expect(increaseReloadVersion).toHaveBeenCalledTimes(1);
617
+ expect(getMainThreadDynamicAttrState(-1, 0)).toBeUndefined();
618
+ expect(getMainThreadDynamicAttrState(-2, 0)).toBeUndefined();
619
+
620
+ // Also verify re-eval reload branch when subtree ref cleanup throws.
621
+ const supportsReEvalKey = Symbol.for('__LYNX_ENTRY_SUPPORTS_RE_EVAL__');
622
+ const reloaderKey = Symbol.for('__LYNX_MAIN_THREAD_ENTRY_RELOADER__');
623
+ const host = lynx as unknown as Record<symbol, unknown>;
624
+ const reloader = vi.fn();
625
+ vi.stubGlobal('__EXPERIMENTAL_RE_EVAL_JS_ON_RELOAD__', true);
626
+ host[supportsReEvalKey] = true;
627
+ host[reloaderKey] = reloader;
628
+ initializeMainThreadDynamicAttrSlots(-1, '_et_first_root', [{ type: 'main-thread-ref', value: firstThrowingRef }]);
629
+ attachMainThreadDynamicAttrRefsForSubtree([{ uid: -1, ref: firstRootRef }]);
630
+ applyPendingMainThreadRefAttachments();
631
+ firstThrowingRef._unmount = vi.fn(() => {
632
+ throw firstCleanupError;
633
+ });
634
+ vi.mocked(__SerializeElementTemplate).mockReturnValueOnce({
635
+ tag: 'page',
636
+ attributes: null,
637
+ childSlots: [[{ templateKey: '_et_first_root', uid: -1 }]],
638
+ uid: 0,
639
+ } as ReturnType<typeof __SerializeElementTemplate>);
640
+ try {
641
+ expect(() => reloadMainThread(undefined, { reloadTemplate: true })).toThrow(firstCleanupError);
642
+ expect(onMtsDestruction).toHaveBeenCalledTimes(1);
643
+ expect(reloader).not.toHaveBeenCalled();
644
+ } finally {
645
+ delete host[supportsReEvalKey];
646
+ delete host[reloaderKey];
647
+ }
648
+ });
649
+
572
650
  it('profiles main-thread reload when profiling is enabled', () => {
573
651
  vi.stubGlobal('__PROFILE__', true);
574
652
  mockedState.root = { __jsx: null };
@@ -647,4 +725,13 @@ describe('ElementTemplate reloadBackground', () => {
647
725
  expect(lynx.__initData).not.toBe(initData);
648
726
  expect(lynx.__initData).toEqual({ stable: true });
649
727
  });
728
+
729
+ it('resets background initData when resetPageData is true', () => {
730
+ mockedState.root = { __jsx: null };
731
+ lynx.__initData = { stale: 1, value: 1 };
732
+
733
+ reloadBackground({ value: 2 }, { resetPageData: true });
734
+
735
+ expect(lynx.__initData).toEqual({ value: 2 });
736
+ });
650
737
  });
@@ -6,7 +6,7 @@ import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
6
6
  import type { ElementTemplateUpdateCommitContext } from '../../../../../src/element-template/protocol/types.js';
7
7
  import type { CompiledFixtureModuleExports } from '../../../test-utils/debug/compiledFixtureModule.js';
8
8
 
9
- globalThis.__GLOBAL_PROPS_MODE__ = 'event';
9
+ lynx.__runtime_configs__ = { globalPropsMode: 'event' };
10
10
 
11
11
  const [
12
12
  { installElementTemplateCommitHook, resetElementTemplateCommitState },
@@ -138,13 +138,13 @@ describe('Compiled ET GlobalProps update fixtures', () => {
138
138
  resetElementTemplateHydrationListener();
139
139
  resetElementTemplateCommitState();
140
140
  envManager.setUseElementTemplate(false);
141
- globalThis.__GLOBAL_PROPS_MODE__ = 'event';
141
+ lynx.__runtime_configs__ = { globalPropsMode: 'event' };
142
142
  globalThis.lynx.getApp().GlobalEventEmitter = originalGlobalEventEmitter;
143
143
  vi.stubGlobal('lynx', originalLynx);
144
144
  });
145
145
 
146
146
  it('reactive updateGlobalProps force-renders direct globalProps reads into native update ops', async () => {
147
- globalThis.__GLOBAL_PROPS_MODE__ = 'reactive';
147
+ lynx.__runtime_configs__ = { globalPropsMode: 'reactive' };
148
148
  const { backgroundModule, mainModule } = await loadCompiledFixturePair<CompiledGlobalPropsModule>(
149
149
  DIRECT_READ_FIXTURE,
150
150
  );
@@ -166,7 +166,7 @@ describe('Compiled ET GlobalProps update fixtures', () => {
166
166
  });
167
167
 
168
168
  it('event mode emits without updating compiled direct globalProps reads', async () => {
169
- globalThis.__GLOBAL_PROPS_MODE__ = 'event';
169
+ lynx.__runtime_configs__ = { globalPropsMode: 'event' };
170
170
  const emitted = vi.fn();
171
171
  const { backgroundModule, mainModule } = await loadCompiledFixturePair<CompiledGlobalPropsModule>(
172
172
  DIRECT_READ_FIXTURE,
@@ -195,7 +195,7 @@ describe('Compiled ET GlobalProps update fixtures', () => {
195
195
  });
196
196
 
197
197
  it('event mode updates compiled GlobalProps hook and provider users through the changed event', async () => {
198
- globalThis.__GLOBAL_PROPS_MODE__ = 'event';
198
+ lynx.__runtime_configs__ = { globalPropsMode: 'event' };
199
199
  const changed = vi.fn();
200
200
  const { backgroundModule, mainModule } = await loadCompiledFixturePair<CompiledGlobalPropsModule>(
201
201
  EVENT_HOOK_FIXTURE,
@@ -15,7 +15,6 @@ function waitForRender(): Promise<void> {
15
15
 
16
16
  async function setupGlobalPropsRuntime(mode: 'reactive' | 'event') {
17
17
  vi.resetModules();
18
- vi.stubGlobal('__GLOBAL_PROPS_MODE__', mode);
19
18
 
20
19
  const { ElementTemplateEnvManager } = await import('../../test-utils/debug/envManager.js');
21
20
  const { ElementTemplateLifecycleConstant } = await import(
@@ -47,6 +46,7 @@ async function setupGlobalPropsRuntime(mode: 'reactive' | 'event') {
47
46
  const originalGlobalEventEmitter = globalThis.lynx.getApp().GlobalEventEmitter;
48
47
  vi.stubGlobal('lynx', {
49
48
  ...baseLynx,
49
+ __runtime_configs__: { globalPropsMode: mode },
50
50
  __globalProps: { theme: 'dark', stable: true },
51
51
  getJSModule(moduleName: string) {
52
52
  if (moduleName === 'GlobalEventEmitter') {
@@ -24,6 +24,7 @@ import {
24
24
  adaptEventAttrSlot,
25
25
  adaptMTEventAttrSlot,
26
26
  adaptMTRefAttrSlot,
27
+ adaptSpreadAttrSlot,
27
28
  clearEtAttrPlanMap,
28
29
  } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
29
30
  import { hydrateBackground as hydrate } from '../../test-utils/debug/hydrate.js';
@@ -261,6 +262,75 @@ describe('hydrate', () => {
261
262
  expect(globalCommitContext.ops).toEqual([]);
262
263
  });
263
264
 
265
+ it('forces spread slot hydration only when live main-thread capabilities or main-thread:ref keys exist', () => {
266
+ __etAttrPlanMap.root = [
267
+ 0,
268
+ adaptSpreadAttrSlot,
269
+ 1,
270
+ adaptSpreadAttrSlot,
271
+ 2,
272
+ adaptSpreadAttrSlot,
273
+ 3,
274
+ adaptSpreadAttrSlot,
275
+ 4,
276
+ adaptSpreadAttrSlot,
277
+ 5,
278
+ adaptSpreadAttrSlot,
279
+ ];
280
+ const gestureSpread = {
281
+ 'main-thread:gesture': { type: 'gesture', value: [] },
282
+ };
283
+ const eventSpread = {
284
+ 'main-thread:bindtap': { type: 'worklet', value: { _wkltId: 'tap' } },
285
+ };
286
+ const clearedRefSpread = {
287
+ 'main-thread:ref': null,
288
+ };
289
+ const clearedEventAndGestureSpread = {
290
+ id: 'same',
291
+ 'main-thread:gesture': null,
292
+ 'main-thread:bindtap': null,
293
+ };
294
+ const root = new BackgroundElementTemplateInstance('root');
295
+ root.attributeSlots = [
296
+ null,
297
+ { id: 'same' },
298
+ gestureSpread,
299
+ eventSpread,
300
+ clearedRefSpread,
301
+ clearedEventAndGestureSpread,
302
+ ];
303
+
304
+ const stream = hydrate(
305
+ createHydrationTemplate(root.instanceId, 'root', {
306
+ attributeSlots: [
307
+ null,
308
+ { id: 'same' },
309
+ { 'main-thread:gesture': { type: 'gesture', value: [] } },
310
+ { 'main-thread:bindtap': { type: 'worklet', value: { _wkltId: 'tap' } } },
311
+ { 'main-thread:ref': null },
312
+ { id: 'same', 'main-thread:gesture': null, 'main-thread:bindtap': null },
313
+ ],
314
+ }),
315
+ root,
316
+ );
317
+
318
+ expect(stream).toEqual([
319
+ ElementTemplateUpdateOps.setAttribute,
320
+ root.instanceId,
321
+ 2,
322
+ gestureSpread,
323
+ ElementTemplateUpdateOps.setAttribute,
324
+ root.instanceId,
325
+ 3,
326
+ eventSpread,
327
+ ElementTemplateUpdateOps.setAttribute,
328
+ root.instanceId,
329
+ 4,
330
+ clearedRefSpread,
331
+ ]);
332
+ });
333
+
264
334
  it('patches attribute slots while creating and inserting background-only children', () => {
265
335
  const root = new BackgroundElementTemplateInstance('root', ['background-root']);
266
336
 
@@ -175,14 +175,14 @@ describe('Compiled ordinary ref background updates', () => {
175
175
  );
176
176
  const stableRef = vi.fn();
177
177
  const newRef = vi.fn();
178
- const unsupportedMainThreadRef = vi.fn();
178
+ const unsupportedCustomRef = vi.fn();
179
179
  const unsupportedWorkletRef = vi.fn();
180
180
 
181
181
  const host = renderOnBackground(backgroundModule, {
182
182
  spread: {
183
183
  id: 'cta',
184
184
  ref: stableRef,
185
- 'main-thread:ref': unsupportedMainThreadRef,
185
+ 'custom:ref': unsupportedCustomRef,
186
186
  'worklet:ref': unsupportedWorkletRef,
187
187
  },
188
188
  });
@@ -192,7 +192,7 @@ describe('Compiled ordinary ref background updates', () => {
192
192
  spread: {
193
193
  id: 'cta',
194
194
  ref: stableRef,
195
- 'main-thread:ref': unsupportedMainThreadRef,
195
+ 'custom:ref': unsupportedCustomRef,
196
196
  'worklet:ref': unsupportedWorkletRef,
197
197
  },
198
198
  });
@@ -229,7 +229,6 @@ describe('Compiled ordinary ref background updates', () => {
229
229
  expect(newRef).toHaveBeenCalledWith(expect.objectContaining({
230
230
  selector: `[ref=${host.instanceId}-0]`,
231
231
  }));
232
- expect(unsupportedMainThreadRef).not.toHaveBeenCalled();
233
232
  expect(unsupportedWorkletRef).not.toHaveBeenCalled();
234
233
  });
235
234
 
@@ -1,6 +1,16 @@
1
+ import { createElement } from 'preact/compat';
1
2
  import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
2
3
 
4
+ import { MainThreadRef, clearMainThreadRefLastIdForTesting } from '../../../../src/core/main-thread-ref.js';
5
+ import { takeMainThreadRefInitValuePatch } from '../../../../src/core/main-thread-ref-init-value.js';
6
+ import { clearMtsConfigCacheForTesting } from '../../../../src/core/mts-capability.js';
3
7
  import { getReloadVersion } from '../../../../src/core/reload-version.js';
8
+ import {
9
+ installElementTemplateCommitHook,
10
+ markElementTemplateHydrated,
11
+ resetElementTemplateCommitState,
12
+ uninstallElementTemplateCommitHookForTesting,
13
+ } from '../../../../src/element-template/background/commit-hook.js';
4
14
  import { setupBackgroundElementTemplateDocument } from '../../../../src/element-template/background/document.js';
5
15
  import {
6
16
  installElementTemplateHydrationListener,
@@ -11,10 +21,17 @@ import {
11
21
  BackgroundPageRootInstance,
12
22
  } from '../../../../src/element-template/background/instance.js';
13
23
  import { backgroundElementTemplateInstanceManager } from '../../../../src/element-template/background/manager.js';
24
+ import { root } from '../../../../src/element-template/index.js';
14
25
  import { reloadBackground } from '../../../../src/element-template/native/reload-background.js';
26
+ import { clearRefState } from '../../../../src/element-template/prop-adapters/ref.js';
15
27
  import { ElementTemplateLifecycleConstant } from '../../../../src/element-template/protocol/lifecycle-constant.js';
16
28
  import type { SerializedCompiledNode } from '../../../../src/element-template/protocol/types.js';
17
29
  import { __root, setRoot } from '../../../../src/element-template/runtime/page/root-instance.js';
30
+ import {
31
+ __etAttrPlanMap,
32
+ adaptRefAttrSlot,
33
+ clearEtAttrPlanMap,
34
+ } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
18
35
  import { ElementTemplateEnvManager } from '../../test-utils/debug/envManager.js';
19
36
 
20
37
  function createSerializedTemplate(handleId: number, templateKey: string): SerializedCompiledNode {
@@ -40,7 +57,14 @@ describe('ElementTemplate background reload', () => {
40
57
  });
41
58
 
42
59
  afterEach(() => {
60
+ uninstallElementTemplateCommitHookForTesting();
61
+ resetElementTemplateCommitState();
43
62
  resetElementTemplateHydrationListener();
63
+ clearRefState();
64
+ clearEtAttrPlanMap();
65
+ clearMainThreadRefLastIdForTesting();
66
+ clearMtsConfigCacheForTesting();
67
+ takeMainThreadRefInitValuePatch();
44
68
  backgroundElementTemplateInstanceManager.clear();
45
69
  });
46
70
 
@@ -76,4 +100,54 @@ describe('ElementTemplate background reload', () => {
76
100
  expect(backgroundElementTemplateInstanceManager.get(oldId)).toBeUndefined();
77
101
  expect(backgroundElementTemplateInstanceManager.get(-1)).toBe(after);
78
102
  });
103
+
104
+ it('unmounts a hydrated tree on reload without dispatching removeNode patches or dropping MainThreadRef init created in ref detach', () => {
105
+ const originalLynxSdkVersion = SystemInfo.lynxSdkVersion;
106
+ SystemInfo.lynxSdkVersion = '4.0';
107
+ clearMtsConfigCacheForTesting();
108
+ installElementTemplateCommitHook();
109
+ const dispatch = vi.spyOn(lynx.getCoreContext(), 'dispatchEvent');
110
+
111
+ let retainedRefFromDetach: MainThreadRef<string> | undefined;
112
+ const refCalls: unknown[] = [];
113
+ const callbackRef = (value: unknown) => {
114
+ refCalls.push(value);
115
+ if (value === null && !retainedRefFromDetach) {
116
+ retainedRefFromDetach = new MainThreadRef('created-in-detach');
117
+ }
118
+ };
119
+
120
+ root.render(createElement('view', null));
121
+ const oldView = (__root as BackgroundElementTemplateInstance).firstChild!;
122
+ __etAttrPlanMap[oldView.type] = [0, adaptRefAttrSlot];
123
+ oldView.setAttribute('attributeSlots', [callbackRef]);
124
+ oldView.markMaterializedByHydration();
125
+ markElementTemplateHydrated();
126
+ dispatch.mockClear();
127
+
128
+ vi.useFakeTimers();
129
+ try {
130
+ reloadBackground({ next: 1 });
131
+
132
+ expect(refCalls).toContain(null);
133
+ expect(retainedRefFromDetach).toBeInstanceOf(MainThreadRef);
134
+ expect(vi.getTimerCount()).toBe(0);
135
+ expect(dispatch).toHaveBeenCalledExactlyOnceWith({
136
+ type: ElementTemplateLifecycleConstant.updateMTRefInitValue,
137
+ data: JSON.stringify([[retainedRefFromDetach!._wvid, 'created-in-detach']]),
138
+ });
139
+ } finally {
140
+ vi.useRealTimers();
141
+ SystemInfo.lynxSdkVersion = originalLynxSdkVersion;
142
+ }
143
+ });
144
+
145
+ it('forwards resetPageData options to applyUpdatePageData during ordinary background reload', () => {
146
+ lynx.__initData = { stale: 1, value: 1 };
147
+ __root.__jsx = null;
148
+
149
+ reloadBackground({ value: 2 }, { resetPageData: true });
150
+
151
+ expect(lynx.__initData).toEqual({ value: 2 });
152
+ });
79
153
  });
@@ -905,6 +905,21 @@ describe('ElementTemplate patch stream (apply)', () => {
905
905
  }
906
906
  });
907
907
 
908
+ it('does not send a Gesture update to Native when the target is missing', () => {
909
+ envManager.switchToMainThread();
910
+ installElementTemplatePatchListener();
911
+ envManager.switchToBackground();
912
+ dispatchElementTemplateUpdate({
913
+ ops: [ElementTemplateUpdateOps.setGesture, 124, 0, null],
914
+ flushOptions: {},
915
+ });
916
+ envManager.switchToMainThread();
917
+ expect(mockSetElementTemplateAttributeSlot).not.toHaveBeenCalled();
918
+ const reportError = (globalThis.lynx as unknown as LynxWithReportErrorMock).reportError;
919
+ expect(String(reportError.mock.calls[0]?.[0]?.message ?? '')).toContain('target handle 124 not found');
920
+ resetReportedErrors();
921
+ });
922
+
908
923
  it('does not update MTRef state when the target is missing', () => {
909
924
  const targetId = 124;
910
925
  const ref = { _wvid: 9 };