@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.
- package/CHANGELOG.md +18 -0
- package/components/tsconfig.tsbuildinfo +1 -1
- package/package.json +1 -1
- package/refresh/.turbo/turbo-build.log +1 -1
- package/runtime/__test__/core/globalProps.test.ts +27 -6
- package/runtime/__test__/element-template/fixtures/gesture/direct/index.tsx +33 -0
- package/runtime/__test__/element-template/fixtures/spread/mts/index.tsx +197 -0
- package/runtime/__test__/element-template/native/callDestroyLifetimeFun.test.ts +89 -3
- package/runtime/__test__/element-template/native/mts-destroy.test.ts +8 -3
- package/runtime/__test__/element-template/native/reload.test.ts +87 -0
- package/runtime/__test__/element-template/runtime/background/global-props/compiled-fixtures.test.tsx +5 -5
- package/runtime/__test__/element-template/runtime/background/global-props.test.tsx +1 -1
- package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +70 -0
- package/runtime/__test__/element-template/runtime/background/ref/compiled-fixtures.test.tsx +3 -4
- package/runtime/__test__/element-template/runtime/background/reload.test.ts +74 -0
- package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +15 -0
- package/runtime/__test__/element-template/runtime/prop-adapters/gesture-compiled.test.ts +200 -0
- package/runtime/__test__/element-template/runtime/prop-adapters/gesture.test.ts +420 -0
- package/runtime/__test__/element-template/runtime/prop-adapters/spread-mts-compiled.test.ts +527 -0
- package/runtime/__test__/element-template/runtime/prop-adapters/spread.test.ts +185 -6
- package/runtime/__test__/element-template/runtime/render/render-direct-native.test.tsx +21 -1
- package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +188 -0
- package/runtime/__test__/snapshot/compat/export.test.jsx +8 -2
- package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.event.test.jsx +2 -2
- package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.test.jsx +3 -3
- package/runtime/__test__/snapshot/ref.test.jsx +30 -0
- package/runtime/__test__/worklet-runtime/initEntry.test.js +12 -0
- package/runtime/compat/index.d.ts +17 -1
- package/runtime/compat/index.js +3 -2
- package/runtime/lazy/compat.js +1 -0
- package/runtime/lazy/element-template-internal.js +1 -0
- package/runtime/lib/core/globalProps.js +1 -1
- package/runtime/lib/core/globalProps.js.map +1 -1
- package/runtime/lib/element-template/background/attr-slots.js +5 -2
- package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
- package/runtime/lib/element-template/background/destroy.js +16 -11
- package/runtime/lib/element-template/background/destroy.js.map +1 -1
- package/runtime/lib/element-template/background/hydrate.js +23 -0
- package/runtime/lib/element-template/background/hydrate.js.map +1 -1
- package/runtime/lib/element-template/debug/alog.d.ts +1 -1
- package/runtime/lib/element-template/debug/alog.js +4 -1
- package/runtime/lib/element-template/debug/alog.js.map +1 -1
- package/runtime/lib/element-template/internal.d.ts +1 -1
- package/runtime/lib/element-template/internal.js +1 -1
- package/runtime/lib/element-template/internal.js.map +1 -1
- package/runtime/lib/element-template/native/callDestroyLifetimeFun.js +6 -1
- package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
- package/runtime/lib/element-template/native/patch-listener.js +1 -1
- package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
- package/runtime/lib/element-template/native/reload-background.js +1 -1
- package/runtime/lib/element-template/native/reload-background.js.map +1 -1
- package/runtime/lib/element-template/native/reload-main-thread.js +33 -9
- package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
- package/runtime/lib/element-template/prop-adapters/gesture.d.ts +23 -0
- package/runtime/lib/element-template/prop-adapters/gesture.js +97 -0
- package/runtime/lib/element-template/prop-adapters/gesture.js.map +1 -0
- package/runtime/lib/element-template/prop-adapters/spread.d.ts +6 -1
- package/runtime/lib/element-template/prop-adapters/spread.js +72 -2
- package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
- package/runtime/lib/element-template/protocol/opcodes.d.ts +1 -0
- package/runtime/lib/element-template/protocol/opcodes.js +1 -0
- package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
- package/runtime/lib/element-template/protocol/types.d.ts +7 -1
- package/runtime/lib/element-template/protocol/types.js.map +1 -1
- package/runtime/lib/element-template/runtime/patch.js +27 -2
- package/runtime/lib/element-template/runtime/patch.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.d.ts +4 -2
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +17 -4
- package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +7 -2
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +368 -58
- package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +2 -1
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js +10 -1
- package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/typed-attributes.js +3 -3
- package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/destroy.js +23 -17
- package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/ref/delay.d.ts +5 -0
- package/runtime/lib/snapshot/lifecycle/ref/delay.js +1 -1
- package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
- package/runtime/lib/tsconfig.tsbuildinfo +1 -1
- package/runtime/lib/worklet-runtime/utils/version.js +4 -1
- package/runtime/lib/worklet-runtime/utils/version.js.map +1 -1
- package/runtime/src/core/globalProps.ts +1 -1
- package/runtime/src/element-template/background/attr-slots.ts +5 -1
- package/runtime/src/element-template/background/destroy.ts +16 -13
- package/runtime/src/element-template/background/hydrate.ts +23 -0
- package/runtime/src/element-template/debug/alog.ts +5 -2
- package/runtime/src/element-template/internal.ts +1 -0
- package/runtime/src/element-template/native/callDestroyLifetimeFun.ts +5 -1
- package/runtime/src/element-template/native/patch-listener.ts +1 -1
- package/runtime/src/element-template/native/reload-background.ts +1 -1
- package/runtime/src/element-template/native/reload-main-thread.ts +30 -10
- package/runtime/src/element-template/prop-adapters/gesture.ts +144 -0
- package/runtime/src/element-template/prop-adapters/spread.ts +110 -2
- package/runtime/src/element-template/protocol/opcodes.ts +1 -0
- package/runtime/src/element-template/protocol/types.ts +8 -0
- package/runtime/src/element-template/runtime/patch.ts +33 -0
- package/runtime/src/element-template/runtime/template/attr-slot-plan.ts +31 -4
- package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +441 -65
- package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +12 -2
- package/runtime/src/element-template/runtime/template/typed-attributes.ts +3 -3
- package/runtime/src/snapshot/lifecycle/destroy.ts +23 -18
- package/runtime/src/snapshot/lifecycle/ref/delay.ts +1 -1
- package/runtime/src/worklet-runtime/utils/version.ts +4 -1
- package/runtime/types/types.d.ts +2 -1
- package/testing-library/.turbo/turbo-build.log +7 -7
- package/transform/.turbo/turbo-build$colon$wasm.log +72 -71
- package/transform/__test__/fixture.spec.js +11 -11
- package/transform/__test__/main-thread-object-compat.spec.js +111 -0
- package/transform/crates/swc_plugin_element_template/lib.rs +11 -0
- package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +113 -0
- package/transform/crates/swc_plugin_worklet/gen_stmt.rs +7 -9
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +3 -3
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +3 -3
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +7 -7
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +7 -7
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +8 -8
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +8 -8
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +5 -5
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +5 -5
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +1 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +1 -1
- package/transform/dist/wasm.cjs +1 -1
- package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +97 -97
- package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
- package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +177 -178
|
@@ -0,0 +1,527 @@
|
|
|
1
|
+
// Copyright 2026 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
|
|
5
|
+
import path from 'node:path';
|
|
6
|
+
import { fileURLToPath } from 'node:url';
|
|
7
|
+
|
|
8
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
installElementTemplateCommitHook,
|
|
12
|
+
resetElementTemplateCommitState,
|
|
13
|
+
} from '../../../../src/element-template/background/commit-hook.js';
|
|
14
|
+
import {
|
|
15
|
+
installElementTemplateHydrationListener,
|
|
16
|
+
resetElementTemplateHydrationListener,
|
|
17
|
+
} from '../../../../src/element-template/background/hydration-listener.js';
|
|
18
|
+
import {
|
|
19
|
+
installElementTemplateRefInitValueListener,
|
|
20
|
+
resetElementTemplateRefInitValueListener,
|
|
21
|
+
} from '../../../../src/element-template/main-thread-ref-init-value.js';
|
|
22
|
+
import {
|
|
23
|
+
installElementTemplatePatchListener,
|
|
24
|
+
resetElementTemplatePatchListener,
|
|
25
|
+
} from '../../../../src/element-template/native/patch-listener.js';
|
|
26
|
+
import type { GestureSlotValue } from '../../../../src/element-template/prop-adapters/gesture.js';
|
|
27
|
+
import { ElementTemplateLifecycleConstant } from '../../../../src/element-template/protocol/lifecycle-constant.js';
|
|
28
|
+
import type { ElementTemplateUpdateCommitContext } from '../../../../src/element-template/protocol/types.js';
|
|
29
|
+
import { parseElementTemplateUpdateEventPayload } from '../../../../src/element-template/protocol/update-event.js';
|
|
30
|
+
import { TYPED_ELEMENT_ATTRIBUTES_SLOT_INDEX } from '../../../../src/element-template/runtime/template/typed-attributes.js';
|
|
31
|
+
import { clearEtAttrPlanMap } from '../../../../src/element-template/runtime/template/attr-slot-plan.js';
|
|
32
|
+
import {
|
|
33
|
+
resetElementTemplateBackgroundFunctionRuntime,
|
|
34
|
+
resetElementTemplateMainThreadBackgroundFunctionRuntime,
|
|
35
|
+
} from '../../../../src/element-template/runtime/template/main-thread-background-function.js';
|
|
36
|
+
import { clearMainThreadDynamicAttrState } from '../../../../src/element-template/runtime/template/main-thread-dynamic-attr-state.js';
|
|
37
|
+
import type { MTEventNativeWrapper } from '../../../../src/element-template/runtime/template/main-thread-event-ctx.js';
|
|
38
|
+
import type { Worklet } from '../../../../src/worklet-runtime/bindings/types.js';
|
|
39
|
+
import { initWorklet } from '../../../../src/worklet-runtime/workletRuntime.js';
|
|
40
|
+
import type { SpreadMtsFixtureProps } from '../../fixtures/spread/mts/index.js';
|
|
41
|
+
import { loadCompiledFixturePair } from '../../test-utils/debug/compiledFixtureModule.js';
|
|
42
|
+
import {
|
|
43
|
+
renderCompiledFixtureOnBackground,
|
|
44
|
+
renderCompiledFixtureOnMainThread,
|
|
45
|
+
} from '../../test-utils/debug/compiledThreadRunner.js';
|
|
46
|
+
import { ElementTemplateEnvManager } from '../../test-utils/debug/envManager.js';
|
|
47
|
+
import { lastMock } from '../../test-utils/mock/mockNativePapi.js';
|
|
48
|
+
|
|
49
|
+
const fixturePath = path.resolve(
|
|
50
|
+
path.dirname(fileURLToPath(import.meta.url)),
|
|
51
|
+
'../../fixtures/spread/mts/index.tsx',
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
interface FixtureModule {
|
|
55
|
+
App: (props: SpreadMtsFixtureProps) => JSX.Element;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
function findCreatedRootSlots(): Record<string, unknown>[] {
|
|
59
|
+
for (const entry of lastMock!.nativeLog) {
|
|
60
|
+
if (
|
|
61
|
+
Array.isArray(entry)
|
|
62
|
+
&& entry[0] === '__CreateElementTemplate'
|
|
63
|
+
&& entry[1] !== '_et_builtin_raw_text'
|
|
64
|
+
&& Array.isArray(entry[3])
|
|
65
|
+
) {
|
|
66
|
+
return entry[3] as Record<string, unknown>[];
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
throw new Error('No compiled template create call found.');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function findLatestSetAttributeForSlot(slotIndex: number): Record<string, unknown> | null {
|
|
73
|
+
const entry = lastMock!.nativeLog.findLast(value =>
|
|
74
|
+
Array.isArray(value)
|
|
75
|
+
&& value[0] === '__SetAttributeOfElementTemplate'
|
|
76
|
+
&& value[2] === slotIndex
|
|
77
|
+
) as unknown[] | undefined;
|
|
78
|
+
if (!entry) {
|
|
79
|
+
throw new Error(`No __SetAttributeOfElementTemplate found for slot ${slotIndex}.`);
|
|
80
|
+
}
|
|
81
|
+
return entry[3] as Record<string, unknown> | null;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function extractSpreadWorklet(spread: Record<string, unknown>, key: string): Worklet {
|
|
85
|
+
const wrapper = spread[key] as MTEventNativeWrapper | undefined;
|
|
86
|
+
expect(wrapper).toMatchObject({ type: 'worklet' });
|
|
87
|
+
return wrapper!.value as Worklet;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
function extractSpreadGestureCallback(
|
|
91
|
+
spread: Record<string, unknown>,
|
|
92
|
+
callbackName = 'onUpdate',
|
|
93
|
+
): Worklet {
|
|
94
|
+
const gesture = spread['main-thread:gesture'] as GestureSlotValue | undefined;
|
|
95
|
+
expect(gesture?.type).toBe('gesture');
|
|
96
|
+
for (const descriptor of gesture!.value) {
|
|
97
|
+
const found = descriptor.callbacks.find(item => item.name === callbackName);
|
|
98
|
+
if (found) {
|
|
99
|
+
return found.callback as Worklet;
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
throw new Error(`Gesture callback '${callbackName}' not found.`);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
describe('Compiled ET spread MTEvent, MTRef, and Gesture lifecycle', () => {
|
|
106
|
+
const env = new ElementTemplateEnvManager();
|
|
107
|
+
let sdk: string | undefined;
|
|
108
|
+
let updates: ElementTemplateUpdateCommitContext[];
|
|
109
|
+
const onUpdate = (event: { data: unknown }) => updates.push(parseElementTemplateUpdateEventPayload(event.data));
|
|
110
|
+
|
|
111
|
+
beforeEach(() => {
|
|
112
|
+
vi.clearAllMocks();
|
|
113
|
+
sdk = SystemInfo.lynxSdkVersion;
|
|
114
|
+
SystemInfo.lynxSdkVersion = '4.0';
|
|
115
|
+
updates = [];
|
|
116
|
+
resetElementTemplateCommitState();
|
|
117
|
+
clearEtAttrPlanMap();
|
|
118
|
+
clearMainThreadDynamicAttrState();
|
|
119
|
+
resetElementTemplateBackgroundFunctionRuntime();
|
|
120
|
+
env.resetEnv('background');
|
|
121
|
+
env.setUseElementTemplate(true);
|
|
122
|
+
installElementTemplateCommitHook();
|
|
123
|
+
installElementTemplateHydrationListener();
|
|
124
|
+
env.switchToMainThread();
|
|
125
|
+
initWorklet();
|
|
126
|
+
installElementTemplateRefInitValueListener();
|
|
127
|
+
installElementTemplatePatchListener();
|
|
128
|
+
lynx.getJSContext().addEventListener(ElementTemplateLifecycleConstant.update, onUpdate);
|
|
129
|
+
env.switchToBackground();
|
|
130
|
+
vi.stubGlobal('__LoadLepusChunk', vi.fn(() => true));
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
afterEach(() => {
|
|
134
|
+
env.switchToMainThread();
|
|
135
|
+
lynx.getJSContext().removeEventListener(ElementTemplateLifecycleConstant.update, onUpdate);
|
|
136
|
+
resetElementTemplatePatchListener();
|
|
137
|
+
resetElementTemplateRefInitValueListener();
|
|
138
|
+
clearMainThreadDynamicAttrState();
|
|
139
|
+
resetElementTemplateMainThreadBackgroundFunctionRuntime();
|
|
140
|
+
env.switchToBackground();
|
|
141
|
+
resetElementTemplateHydrationListener();
|
|
142
|
+
resetElementTemplateBackgroundFunctionRuntime();
|
|
143
|
+
env.setUseElementTemplate(false);
|
|
144
|
+
SystemInfo.lynxSdkVersion = sdk;
|
|
145
|
+
delete globalThis.lynxWorkletImpl;
|
|
146
|
+
// @ts-expect-error Restore globals installed by the real worklet runtime.
|
|
147
|
+
delete globalThis.registerWorklet;
|
|
148
|
+
// @ts-expect-error Restore globals installed by the real worklet runtime.
|
|
149
|
+
delete globalThis.registerWorkletInternal;
|
|
150
|
+
// @ts-expect-error Restore globals installed by the real worklet runtime.
|
|
151
|
+
delete globalThis.runWorklet;
|
|
152
|
+
vi.unstubAllGlobals();
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
it('hydrates multiple spread main-thread events, composed gestures, and root/nested object MTRefs with pre-hydrate replay', async () => {
|
|
156
|
+
const { backgroundModule, mainModule } = await loadCompiledFixturePair<FixtureModule>(fixturePath, {
|
|
157
|
+
enableWorkletTransform: true,
|
|
158
|
+
envManager: env,
|
|
159
|
+
});
|
|
160
|
+
const onTapReport = vi.fn((source: string, label: string, count: number) => `${source}:${label}:${count}`);
|
|
161
|
+
const onMoveReport = vi.fn((label: string, count: number) => `move:${label}:${count}`);
|
|
162
|
+
const onGestureReport = vi.fn((source: string, label: string, count: number) => `${source}:${label}:${count}`);
|
|
163
|
+
|
|
164
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
165
|
+
label: 'first',
|
|
166
|
+
onTapReport,
|
|
167
|
+
onMoveReport,
|
|
168
|
+
onGestureReport,
|
|
169
|
+
});
|
|
170
|
+
renderCompiledFixtureOnMainThread(mainModule, env, {
|
|
171
|
+
label: 'first',
|
|
172
|
+
onTapReport,
|
|
173
|
+
onMoveReport,
|
|
174
|
+
onGestureReport,
|
|
175
|
+
});
|
|
176
|
+
|
|
177
|
+
const firstScreenSlots = findCreatedRootSlots();
|
|
178
|
+
const firstSpread = firstScreenSlots[0]!;
|
|
179
|
+
const nestedSpread = firstScreenSlots[2]!;
|
|
180
|
+
|
|
181
|
+
// Spread main-thread:ref is stripped before Native create while layout-only/flatten guards are added.
|
|
182
|
+
expect(firstSpread).not.toHaveProperty('main-thread:ref');
|
|
183
|
+
expect(firstSpread).toMatchObject({ 'has-react-ref': true, flatten: false });
|
|
184
|
+
expect(nestedSpread).not.toHaveProperty('main-thread:ref');
|
|
185
|
+
expect(nestedSpread).toMatchObject({ 'has-react-ref': true, flatten: false });
|
|
186
|
+
|
|
187
|
+
const preHydrateTap = extractSpreadWorklet(firstSpread, 'main-thread:bindtap');
|
|
188
|
+
const preHydrateMove = extractSpreadWorklet(firstSpread, 'main-thread:catchtouchmove');
|
|
189
|
+
const preHydrateGesture = extractSpreadGestureCallback(firstSpread, 'onUpdate');
|
|
190
|
+
|
|
191
|
+
let delayedTap: Promise<unknown>;
|
|
192
|
+
let delayedGesture: Promise<unknown>;
|
|
193
|
+
env.switchToMainThread(() => {
|
|
194
|
+
// Object MainThreadRefs on both root and nested spread targets are attached on first screen.
|
|
195
|
+
const firstScreenRefMap = globalThis.lynxWorkletImpl._refImpl._firstScreenWorkletRefMap;
|
|
196
|
+
expect(
|
|
197
|
+
Object.values(firstScreenRefMap).filter(entry => (entry as { current?: unknown })?.current != null),
|
|
198
|
+
).toHaveLength(2);
|
|
199
|
+
|
|
200
|
+
delayedTap = globalThis.runWorklet(preHydrateTap, []) as Promise<unknown>;
|
|
201
|
+
delayedGesture = globalThis.runWorklet(preHydrateGesture, []) as Promise<unknown>;
|
|
202
|
+
expect(onTapReport).not.toHaveBeenCalled();
|
|
203
|
+
expect(onGestureReport).not.toHaveBeenCalled();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
expect(updates.at(-1)?.isHydration).toBe(true);
|
|
207
|
+
// Both root and nested spread object MainThreadRefs (including nestedElRef not captured in any worklet closure)
|
|
208
|
+
// are attached in _workletRefMap after hydration.
|
|
209
|
+
expect(
|
|
210
|
+
Object.values(globalThis.lynxWorkletImpl._refImpl._workletRefMap).filter(
|
|
211
|
+
entry =>
|
|
212
|
+
typeof (entry as { current?: unknown })?.current === 'object'
|
|
213
|
+
&& (entry as { current?: unknown }).current !== null,
|
|
214
|
+
),
|
|
215
|
+
).toHaveLength(2);
|
|
216
|
+
const hydratedSpread = findLatestSetAttributeForSlot(0)!;
|
|
217
|
+
expect(hydratedSpread).not.toHaveProperty('main-thread:ref');
|
|
218
|
+
expect(hydratedSpread).toMatchObject({ 'has-react-ref': true, flatten: false });
|
|
219
|
+
|
|
220
|
+
const hydratedTap = extractSpreadWorklet(hydratedSpread, 'main-thread:bindtap');
|
|
221
|
+
const hydratedMove = extractSpreadWorklet(hydratedSpread, 'main-thread:catchtouchmove');
|
|
222
|
+
const hydratedGesture = extractSpreadGestureCallback(hydratedSpread, 'onUpdate');
|
|
223
|
+
expect(hydratedTap).not.toBe(preHydrateTap);
|
|
224
|
+
expect(hydratedMove).not.toBe(preHydrateMove);
|
|
225
|
+
expect(hydratedGesture).not.toBe(preHydrateGesture);
|
|
226
|
+
|
|
227
|
+
env.switchToBackground();
|
|
228
|
+
expect(onTapReport).toHaveBeenCalledWith('primary:with-ref', 'first', 1);
|
|
229
|
+
expect(onGestureReport).toHaveBeenCalledWith('primary-gesture', 'first', 2);
|
|
230
|
+
|
|
231
|
+
env.switchToMainThread();
|
|
232
|
+
await expect(delayedTap!).resolves.toBe('primary:with-ref:first:1');
|
|
233
|
+
await expect(delayedGesture!).resolves.toBe('primary-gesture:first:2');
|
|
234
|
+
|
|
235
|
+
// Post-hydrate execution preserves MainThreadRef count (2 -> 3 -> 4) across all spread callbacks.
|
|
236
|
+
const moveRes = globalThis.runWorklet(hydratedMove, []) as Promise<unknown>;
|
|
237
|
+
env.switchToBackground();
|
|
238
|
+
expect(onMoveReport).toHaveBeenCalledWith('first', 3);
|
|
239
|
+
env.switchToMainThread();
|
|
240
|
+
await expect(moveRes).resolves.toBe('move:first:3');
|
|
241
|
+
|
|
242
|
+
// Closure update & clearing in spread slot.
|
|
243
|
+
env.switchToBackground();
|
|
244
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
245
|
+
label: 'second',
|
|
246
|
+
onTapReport,
|
|
247
|
+
onMoveReport,
|
|
248
|
+
onGestureReport,
|
|
249
|
+
});
|
|
250
|
+
env.switchToMainThread();
|
|
251
|
+
const updatedSpread = findLatestSetAttributeForSlot(0)!;
|
|
252
|
+
const updatedTap = extractSpreadWorklet(updatedSpread, 'main-thread:bindtap');
|
|
253
|
+
const tapRes = globalThis.runWorklet(updatedTap, []) as Promise<unknown>;
|
|
254
|
+
env.switchToBackground();
|
|
255
|
+
expect(onTapReport).toHaveBeenLastCalledWith('primary:with-ref', 'second', 4);
|
|
256
|
+
env.switchToMainThread();
|
|
257
|
+
await expect(tapRes).resolves.toBe('primary:with-ref:second:4');
|
|
258
|
+
|
|
259
|
+
// Clearing spread events & gesture emits explicit nulls to Native.
|
|
260
|
+
env.switchToBackground();
|
|
261
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
262
|
+
label: 'second',
|
|
263
|
+
enableEvents: false,
|
|
264
|
+
enableGesture: false,
|
|
265
|
+
onTapReport,
|
|
266
|
+
onMoveReport,
|
|
267
|
+
onGestureReport,
|
|
268
|
+
});
|
|
269
|
+
env.switchToMainThread();
|
|
270
|
+
const clearedSpread = findLatestSetAttributeForSlot(0)!;
|
|
271
|
+
expect(clearedSpread).toMatchObject({
|
|
272
|
+
'main-thread:bindtap': null,
|
|
273
|
+
'main-thread:catchtouchmove': null,
|
|
274
|
+
'main-thread:gesture': null,
|
|
275
|
+
});
|
|
276
|
+
});
|
|
277
|
+
|
|
278
|
+
it('handles multi-spread MTRef/MTEvent/Gesture override, masked ctx hydration, key-deletion fallback, latest closure update, and unmount cleanup', async () => {
|
|
279
|
+
const { backgroundModule, mainModule } = await loadCompiledFixturePair<FixtureModule>(fixturePath, {
|
|
280
|
+
enableWorkletTransform: true,
|
|
281
|
+
envManager: env,
|
|
282
|
+
});
|
|
283
|
+
const onRefReport = vi.fn();
|
|
284
|
+
const onTapReport = vi.fn((source: string, label: string, count: number) => `${source}:${label}:${count}`);
|
|
285
|
+
const onGestureReport = vi.fn((source: string, label: string, count: number) => `${source}:${label}:${count}`);
|
|
286
|
+
|
|
287
|
+
// Initial render with secondSpread overriding firstSpread's callback ref, bindtap, and gesture.
|
|
288
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
289
|
+
label: 'v1',
|
|
290
|
+
refKind: 'callback',
|
|
291
|
+
secondSpreadMode: 'override-events-gesture-ref',
|
|
292
|
+
onRefReport,
|
|
293
|
+
onTapReport,
|
|
294
|
+
onGestureReport,
|
|
295
|
+
});
|
|
296
|
+
renderCompiledFixtureOnMainThread(mainModule, env, {
|
|
297
|
+
label: 'v1',
|
|
298
|
+
refKind: 'callback',
|
|
299
|
+
secondSpreadMode: 'override-events-gesture-ref',
|
|
300
|
+
onRefReport,
|
|
301
|
+
onTapReport,
|
|
302
|
+
onGestureReport,
|
|
303
|
+
});
|
|
304
|
+
|
|
305
|
+
// Hydrate completes: BOTH slot 0 (masked firstSpread) and slot 1 (overriding secondSpread) hydrate!
|
|
306
|
+
env.switchToMainThread(() => {
|
|
307
|
+
const firstScreenRefs = Object.values(globalThis.lynxWorkletImpl._refImpl._firstScreenWorkletRefMap);
|
|
308
|
+
expect(firstScreenRefs.some(entry => (entry as { current?: unknown })?.current != null)).toBe(true);
|
|
309
|
+
});
|
|
310
|
+
env.switchToBackground();
|
|
311
|
+
expect(onRefReport.mock.calls).toEqual([
|
|
312
|
+
['mount-override:v1', 'present'],
|
|
313
|
+
['mount-override:v1', 'present'],
|
|
314
|
+
['cleanup-override:v1', 'cleaned'],
|
|
315
|
+
]);
|
|
316
|
+
|
|
317
|
+
env.switchToMainThread();
|
|
318
|
+
expect(
|
|
319
|
+
Object.values(globalThis.lynxWorkletImpl._refImpl._workletRefMap).some(
|
|
320
|
+
entry =>
|
|
321
|
+
typeof (entry as { current?: unknown })?.current === 'object'
|
|
322
|
+
&& (entry as { current?: unknown })?.current !== null,
|
|
323
|
+
),
|
|
324
|
+
).toBe(true);
|
|
325
|
+
|
|
326
|
+
// Capture the hydrated slot 0 (masked) and slot 1 (overriding) spread payloads.
|
|
327
|
+
const hydratedMaskedSlot0 = findLatestSetAttributeForSlot(0)!;
|
|
328
|
+
const hydratedOverrideSlot1 = findLatestSetAttributeForSlot(1)!;
|
|
329
|
+
const overrideTap = extractSpreadWorklet(hydratedOverrideSlot1, 'main-thread:bindtap');
|
|
330
|
+
const overrideGesture = extractSpreadGestureCallback(hydratedOverrideSlot1, 'onUpdate');
|
|
331
|
+
|
|
332
|
+
// Execute overriding slot 1 MTEvent (+10) and Gesture (+100).
|
|
333
|
+
const overrideTapRes = globalThis.runWorklet(overrideTap, []) as Promise<unknown>;
|
|
334
|
+
const overrideGestureRes = globalThis.runWorklet(overrideGesture, []) as Promise<unknown>;
|
|
335
|
+
env.switchToBackground();
|
|
336
|
+
expect(onTapReport).toHaveBeenLastCalledWith('secondary', 'v1', 10);
|
|
337
|
+
expect(onGestureReport).toHaveBeenLastCalledWith('secondary-gesture', 'v1', 110);
|
|
338
|
+
env.switchToMainThread();
|
|
339
|
+
await expect(overrideTapRes).resolves.toBe('secondary:v1:10');
|
|
340
|
+
await expect(overrideGestureRes).resolves.toBe('secondary-gesture:v1:110');
|
|
341
|
+
|
|
342
|
+
env.switchToBackground();
|
|
343
|
+
onRefReport.mockClear();
|
|
344
|
+
|
|
345
|
+
// Explicit { 'main-thread:ref': undefined } in secondSpread overrides firstSpread with null (detaches ref).
|
|
346
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
347
|
+
label: 'v1',
|
|
348
|
+
refKind: 'callback',
|
|
349
|
+
secondSpreadMode: 'null-ref',
|
|
350
|
+
onRefReport,
|
|
351
|
+
onTapReport,
|
|
352
|
+
onGestureReport,
|
|
353
|
+
});
|
|
354
|
+
env.switchToMainThread();
|
|
355
|
+
env.switchToBackground();
|
|
356
|
+
expect(onRefReport.mock.calls).toEqual([['cleanup-override:v1', 'cleaned']]);
|
|
357
|
+
onRefReport.mockClear();
|
|
358
|
+
|
|
359
|
+
// Omitting MTS keys from secondSpread while keeping label='v1' emits NO new slot 0 patch!
|
|
360
|
+
// Native falls back to slot 0's already-hydrated MTEvent & Gesture ctxs, while slot 0's callback ref mounts.
|
|
361
|
+
env.switchToMainThread();
|
|
362
|
+
lastMock!.nativeLog.length = 0;
|
|
363
|
+
env.switchToBackground();
|
|
364
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
365
|
+
label: 'v1',
|
|
366
|
+
refKind: 'callback',
|
|
367
|
+
secondSpreadMode: 'omit',
|
|
368
|
+
onRefReport,
|
|
369
|
+
onTapReport,
|
|
370
|
+
onGestureReport,
|
|
371
|
+
});
|
|
372
|
+
env.switchToMainThread();
|
|
373
|
+
// Confirm slot 0 was NOT re-patched (memoized on label='v1'), only slot 1 was updated to omit the override keys.
|
|
374
|
+
expect(
|
|
375
|
+
lastMock!.nativeLog.some(entry =>
|
|
376
|
+
Array.isArray(entry) && entry[0] === '__SetAttributeOfElementTemplate' && entry[2] === 0
|
|
377
|
+
),
|
|
378
|
+
).toBe(false);
|
|
379
|
+
expect(findLatestSetAttributeForSlot(1)).toEqual({ 'data-extra': 'ok' });
|
|
380
|
+
|
|
381
|
+
env.switchToBackground();
|
|
382
|
+
expect(onRefReport.mock.calls).toEqual([['mount:v1', 'present']]);
|
|
383
|
+
onRefReport.mockClear();
|
|
384
|
+
|
|
385
|
+
// Execute the masked-at-hydration slot 0 MTEvent & Gesture ctxs that Native now falls back to!
|
|
386
|
+
env.switchToMainThread();
|
|
387
|
+
const fallbackMaskedTap = extractSpreadWorklet(hydratedMaskedSlot0, 'main-thread:bindtap');
|
|
388
|
+
const fallbackMaskedGesture = extractSpreadGestureCallback(hydratedMaskedSlot0, 'onUpdate');
|
|
389
|
+
const fallbackTapRes = globalThis.runWorklet(fallbackMaskedTap, []) as Promise<unknown>;
|
|
390
|
+
const fallbackGestureRes = globalThis.runWorklet(fallbackMaskedGesture, []) as Promise<unknown>;
|
|
391
|
+
env.switchToBackground();
|
|
392
|
+
expect(onTapReport).toHaveBeenLastCalledWith('primary:with-ref', 'v1', 111);
|
|
393
|
+
expect(onGestureReport).toHaveBeenLastCalledWith('primary-gesture', 'v1', 112);
|
|
394
|
+
env.switchToMainThread();
|
|
395
|
+
await expect(fallbackTapRes).resolves.toBe('primary:with-ref:v1:111');
|
|
396
|
+
await expect(fallbackGestureRes).resolves.toBe('primary-gesture:v1:112');
|
|
397
|
+
|
|
398
|
+
// Updating a sibling prop in firstSpread without changing memoized primaryCallbackRef does NOT remount it.
|
|
399
|
+
env.switchToBackground();
|
|
400
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
401
|
+
label: 'v1',
|
|
402
|
+
cardId: 'card-sibling-update',
|
|
403
|
+
refKind: 'callback',
|
|
404
|
+
secondSpreadMode: 'omit',
|
|
405
|
+
onRefReport,
|
|
406
|
+
onTapReport,
|
|
407
|
+
onGestureReport,
|
|
408
|
+
});
|
|
409
|
+
env.switchToMainThread();
|
|
410
|
+
env.switchToBackground();
|
|
411
|
+
expect(onRefReport).not.toHaveBeenCalled();
|
|
412
|
+
|
|
413
|
+
// Updating label='v2' refreshes slot 0's closure for MTRef, MTEvent, and Gesture!
|
|
414
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
415
|
+
label: 'v2',
|
|
416
|
+
cardId: 'card-sibling-update',
|
|
417
|
+
refKind: 'callback',
|
|
418
|
+
secondSpreadMode: 'omit',
|
|
419
|
+
onRefReport,
|
|
420
|
+
onTapReport,
|
|
421
|
+
onGestureReport,
|
|
422
|
+
});
|
|
423
|
+
env.switchToMainThread();
|
|
424
|
+
const updatedSlot0 = findLatestSetAttributeForSlot(0)!;
|
|
425
|
+
const latestTap = extractSpreadWorklet(updatedSlot0, 'main-thread:bindtap');
|
|
426
|
+
const latestGesture = extractSpreadGestureCallback(updatedSlot0, 'onUpdate');
|
|
427
|
+
const latestTapRes = globalThis.runWorklet(latestTap, []) as Promise<unknown>;
|
|
428
|
+
const latestGestureRes = globalThis.runWorklet(latestGesture, []) as Promise<unknown>;
|
|
429
|
+
env.switchToBackground();
|
|
430
|
+
expect(onRefReport.mock.calls).toEqual([
|
|
431
|
+
['cleanup:v1', 'cleaned'],
|
|
432
|
+
['mount:v2', 'present'],
|
|
433
|
+
]);
|
|
434
|
+
expect(onTapReport).toHaveBeenLastCalledWith('primary:with-ref', 'v2', 113);
|
|
435
|
+
expect(onGestureReport).toHaveBeenLastCalledWith('primary-gesture', 'v2', 114);
|
|
436
|
+
env.switchToMainThread();
|
|
437
|
+
await expect(latestTapRes).resolves.toBe('primary:with-ref:v2:113');
|
|
438
|
+
await expect(latestGestureRes).resolves.toBe('primary-gesture:v2:114');
|
|
439
|
+
env.switchToBackground();
|
|
440
|
+
onRefReport.mockClear();
|
|
441
|
+
|
|
442
|
+
// Unmounting cleans up the active spread callback ref once.
|
|
443
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
444
|
+
show: false,
|
|
445
|
+
label: 'v2',
|
|
446
|
+
refKind: 'callback',
|
|
447
|
+
onRefReport,
|
|
448
|
+
onTapReport,
|
|
449
|
+
onGestureReport,
|
|
450
|
+
});
|
|
451
|
+
env.switchToMainThread();
|
|
452
|
+
env.switchToBackground();
|
|
453
|
+
expect(onRefReport.mock.calls).toEqual([['cleanup:v2', 'cleaned']]);
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
it('unmasks earlier main-thread:ref when MTS spread has explicit null/undefined ref and BTS hydration omits the key with matching attributes', async () => {
|
|
457
|
+
const { backgroundModule, mainModule } = await loadCompiledFixturePair<FixtureModule>(fixturePath, {
|
|
458
|
+
enableWorkletTransform: true,
|
|
459
|
+
envManager: env,
|
|
460
|
+
});
|
|
461
|
+
const onRefReport = vi.fn();
|
|
462
|
+
|
|
463
|
+
// BTS renders with secondSpreadMode='omit' ({ 'data-extra': 'ok' }, no 'main-thread:ref' key),
|
|
464
|
+
// while MTS renders with secondSpreadMode='null-ref' ({ 'data-extra': 'ok', 'main-thread:ref': undefined }).
|
|
465
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
466
|
+
label: 'hydrate-unmask',
|
|
467
|
+
refKind: 'callback',
|
|
468
|
+
secondSpreadMode: 'omit',
|
|
469
|
+
onRefReport,
|
|
470
|
+
});
|
|
471
|
+
renderCompiledFixtureOnMainThread(mainModule, env, {
|
|
472
|
+
label: 'hydrate-unmask',
|
|
473
|
+
refKind: 'callback',
|
|
474
|
+
secondSpreadMode: 'null-ref',
|
|
475
|
+
onRefReport,
|
|
476
|
+
});
|
|
477
|
+
|
|
478
|
+
env.switchToMainThread();
|
|
479
|
+
env.switchToBackground();
|
|
480
|
+
expect(onRefReport.mock.calls).toEqual([['mount:hydrate-unmask', 'present']]);
|
|
481
|
+
});
|
|
482
|
+
|
|
483
|
+
it('keeps typed <page {...props} /> and <list {...props} /> spreads isolated from main-thread capabilities', async () => {
|
|
484
|
+
const { backgroundModule, mainModule } = await loadCompiledFixturePair<FixtureModule>(fixturePath, {
|
|
485
|
+
enableWorkletTransform: true,
|
|
486
|
+
envManager: env,
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
490
|
+
mode: 'typed-page',
|
|
491
|
+
cardId: 'page-id',
|
|
492
|
+
refKind: 'callback',
|
|
493
|
+
});
|
|
494
|
+
renderCompiledFixtureOnMainThread(mainModule, env, {
|
|
495
|
+
mode: 'typed-page',
|
|
496
|
+
cardId: 'page-id',
|
|
497
|
+
refKind: 'callback',
|
|
498
|
+
});
|
|
499
|
+
env.switchToMainThread();
|
|
500
|
+
|
|
501
|
+
const pageAttrEntry = lastMock!.nativeLog.find(entry =>
|
|
502
|
+
Array.isArray(entry)
|
|
503
|
+
&& entry[0] === '__SetAttributeOfElementTemplate'
|
|
504
|
+
&& typeof entry[1] === 'string'
|
|
505
|
+
&& entry[1].startsWith('<page')
|
|
506
|
+
&& entry[2] === TYPED_ELEMENT_ATTRIBUTES_SLOT_INDEX
|
|
507
|
+
) as unknown[] | undefined;
|
|
508
|
+
expect(pageAttrEntry?.[3]).toEqual({ id: 'page-id' });
|
|
509
|
+
|
|
510
|
+
lastMock!.nativeLog.length = 0;
|
|
511
|
+
renderCompiledFixtureOnBackground(backgroundModule, env, {
|
|
512
|
+
mode: 'typed-list',
|
|
513
|
+
cardId: 'list-id',
|
|
514
|
+
refKind: 'callback',
|
|
515
|
+
});
|
|
516
|
+
env.switchToMainThread();
|
|
517
|
+
const listCreateEntry = lastMock!.nativeLog.find(entry =>
|
|
518
|
+
Array.isArray(entry) && entry[0] === '__CreateTypedElementTemplate' && entry[1] === 'list'
|
|
519
|
+
) as unknown[] | undefined;
|
|
520
|
+
const listAttrs = (listCreateEntry?.[2] ?? {}) as Record<string, unknown>;
|
|
521
|
+
expect(listAttrs['id']).toBe('list-id');
|
|
522
|
+
expect(listAttrs).not.toHaveProperty('main-thread:bindtap');
|
|
523
|
+
expect(listAttrs).not.toHaveProperty('main-thread:ref');
|
|
524
|
+
expect(listAttrs).not.toHaveProperty('main-thread:gesture');
|
|
525
|
+
expect(listAttrs).not.toHaveProperty('has-react-ref');
|
|
526
|
+
});
|
|
527
|
+
});
|