@lynx-js/react 0.129.4 → 0.129.5
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 +22 -0
- package/components/tsconfig.tsbuildinfo +1 -1
- package/package.json +9 -1
- package/refresh/.turbo/turbo-build.log +1 -1
- package/runtime/__test__/core/lynx/env.test.ts +146 -0
- package/runtime/__test__/element-template/imports/suspense-lazy.test.ts +47 -4
- package/runtime/__test__/element-template/native/index.test.ts +9 -9
- package/runtime/__test__/element-template/native/mts-destroy.test.ts +55 -2
- package/runtime/__test__/element-template/native/reload.test.ts +15 -5
- package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +1 -1
- package/runtime/__test__/element-template/runtime/hydration/hydration-listener.test.ts +1 -1
- package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +33 -0
- package/runtime/__test__/element-template/vitest.config.ts +1 -1
- package/runtime/__test__/snapshot/debug/profile.test.jsx +147 -26
- package/runtime/__test__/snapshot/lifecycle/reload-delayed-background.test.jsx +54 -0
- package/runtime/__test__/snapshot/lifecycle/transfer.mts.test.jsx +98 -6
- package/runtime/__test__/snapshot/lifecycle/transfer.test.jsx +214 -9
- package/runtime/__test__/snapshot/lynx/suspense.test.jsx +48 -0
- package/runtime/__test__/snapshot/utils/envManager.ts +2 -0
- package/runtime/__test__/worklet-runtime/delayWorkletEvent.test.js +21 -0
- package/runtime/__test__/worklet-runtime/lifecycle.test.js +297 -0
- package/runtime/__test__/worklet-runtime/profile.test.js +12 -6
- package/runtime/lazy/element-template-internal.js +1 -0
- package/runtime/lib/core/compat/componentIs.d.ts +1 -0
- package/runtime/lib/core/compat/componentIs.js +20 -0
- package/runtime/lib/core/compat/componentIs.js.map +1 -0
- package/runtime/lib/core/lynx/env.d.ts +1 -0
- package/runtime/lib/{element-template → core}/lynx/env.js +3 -2
- package/runtime/lib/core/lynx/env.js.map +1 -0
- package/runtime/lib/core/thread-function-call/main-thread.d.ts +1 -0
- package/runtime/lib/core/thread-function-call/main-thread.js +4 -1
- package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -1
- package/runtime/lib/element-template/background/commit-event.d.ts +6 -0
- package/runtime/lib/element-template/background/commit-event.js +36 -0
- package/runtime/lib/element-template/background/commit-event.js.map +1 -0
- package/runtime/lib/element-template/background/commit-hook.js +3 -23
- package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
- package/runtime/lib/element-template/background/destroy.js +1 -1
- package/runtime/lib/element-template/background/destroy.js.map +1 -1
- package/runtime/lib/element-template/background/hydrate.js +1 -2
- package/runtime/lib/element-template/background/hydrate.js.map +1 -1
- package/runtime/lib/element-template/background/hydration-listener.js +5 -25
- package/runtime/lib/element-template/background/hydration-listener.js.map +1 -1
- package/runtime/lib/element-template/{runtime/template → background}/main-thread-function.js +4 -4
- package/runtime/lib/element-template/background/main-thread-function.js.map +1 -0
- package/runtime/lib/element-template/index.d.ts +1 -1
- package/runtime/lib/element-template/index.js +1 -1
- package/runtime/lib/element-template/index.js.map +1 -1
- package/runtime/lib/element-template/internal.js +3 -0
- package/runtime/lib/element-template/internal.js.map +1 -1
- package/runtime/lib/element-template/native/index.js +2 -2
- package/runtime/lib/element-template/native/index.js.map +1 -1
- package/runtime/lib/element-template/native/mts-destroy.d.ts +2 -1
- package/runtime/lib/element-template/native/mts-destroy.js +15 -4
- package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
- package/runtime/lib/element-template/native/reload-main-thread.js +2 -2
- package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
- package/runtime/lib/element-template/runtime/patch.js +4 -11
- package/runtime/lib/element-template/runtime/patch.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/handle.d.ts +1 -1
- package/runtime/lib/element-template/runtime/template/handle.js +2 -2
- package/runtime/lib/element-template/runtime/template/handle.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.d.ts +1 -0
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +7 -2
- package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
- package/runtime/lib/internal.js +4 -10
- package/runtime/lib/internal.js.map +1 -1
- package/runtime/lib/snapshot/debug/profileHooks.js +27 -62
- package/runtime/lib/snapshot/debug/profileHooks.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/reload.js +2 -2
- package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
- package/runtime/lib/snapshot/lynx/env.js +2 -32
- package/runtime/lib/snapshot/lynx/env.js.map +1 -1
- package/runtime/lib/snapshot/transfer.d.ts +6 -0
- package/runtime/lib/snapshot/transfer.js +154 -11
- package/runtime/lib/snapshot/transfer.js.map +1 -1
- package/runtime/lib/tsconfig.tsbuildinfo +1 -1
- package/runtime/lib/worklet-runtime/bindings/lifecycle.d.ts +19 -0
- package/runtime/lib/worklet-runtime/bindings/lifecycle.js +49 -0
- package/runtime/lib/worklet-runtime/bindings/lifecycle.js.map +1 -0
- package/runtime/lib/worklet-runtime/delayWorkletEvent.d.ts +1 -1
- package/runtime/lib/worklet-runtime/delayWorkletEvent.js +9 -1
- package/runtime/lib/worklet-runtime/delayWorkletEvent.js.map +1 -1
- package/runtime/lib/worklet-runtime/utils/profile.js +1 -3
- package/runtime/lib/worklet-runtime/utils/profile.js.map +1 -1
- package/runtime/src/core/compat/componentIs.ts +32 -0
- package/runtime/src/{element-template → core}/lynx/env.ts +6 -4
- package/runtime/src/core/thread-function-call/main-thread.ts +5 -1
- package/runtime/src/element-template/background/commit-event.ts +52 -0
- package/runtime/src/element-template/background/commit-hook.ts +4 -31
- package/runtime/src/element-template/background/destroy.ts +1 -1
- package/runtime/src/element-template/background/hydrate.ts +5 -3
- package/runtime/src/element-template/background/hydration-listener.ts +7 -32
- package/runtime/src/element-template/{runtime/template → background}/main-thread-function.ts +4 -7
- package/runtime/src/element-template/index.ts +1 -1
- package/runtime/src/element-template/internal.ts +4 -0
- package/runtime/src/element-template/native/index.ts +2 -2
- package/runtime/src/element-template/native/mts-destroy.ts +21 -4
- package/runtime/src/element-template/native/reload-main-thread.ts +2 -2
- package/runtime/src/element-template/runtime/patch.ts +20 -35
- package/runtime/src/element-template/runtime/template/handle.ts +2 -0
- package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -2
- package/runtime/src/internal.ts +4 -13
- package/runtime/src/snapshot/debug/profileHooks.ts +30 -91
- package/runtime/src/snapshot/lifecycle/reload.ts +2 -3
- package/runtime/src/snapshot/lynx/env.ts +3 -43
- package/runtime/src/snapshot/transfer.ts +169 -13
- package/runtime/src/worklet-runtime/bindings/lifecycle.ts +51 -0
- package/runtime/src/worklet-runtime/delayWorkletEvent.ts +10 -2
- package/runtime/src/worklet-runtime/utils/profile.ts +1 -3
- package/runtime/types/internal-preact.d.ts +2 -0
- package/runtime/vitest.config.ts +0 -1
- package/testing-library/.turbo/turbo-build.log +7 -7
- package/testing-library/src/__tests__/events.test.jsx +1 -1
- package/testing-library/src/fire-event.ts +1 -1
- package/transform/.turbo/turbo-build$colon$wasm.log +67 -66
- package/transform/__test__/fixture.spec.js +1 -1
- package/transform/cjs/browser.cjs +11 -0
- package/transform/crates/swc_plugin_compat/lib.rs +20 -2
- package/transform/crates/swc_plugin_compat/napi.rs +7 -0
- package/transform/crates/swc_plugin_dynamic_import/lib.rs +15 -4
- package/transform/crates/swc_plugin_element_template/attr_name.rs +0 -3
- package/transform/crates/swc_plugin_element_template/extractor.rs +0 -8
- package/transform/crates/swc_plugin_element_template/lib.rs +12 -1
- package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +35 -0
- package/transform/crates/swc_plugin_worklet/lib.rs +11 -7
- package/transform/dist/wasm.cjs +1 -1
- package/transform/rspack-napi/lib/index.js +1 -4
- package/transform/rspack-napi/lib/index.js.map +1 -1
- package/transform/rspack-napi/src/index.ts +1 -4
- package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
- package/transform/src/lib.rs +394 -27
- package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +413 -105
- package/transform/swc-plugin-reactlynx/src/lib.rs +99 -22
- package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
- package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +28 -359
- package/transform/swc-plugin-reactlynx-compat/swc_plugin_reactlynx_compat.wasm +0 -0
- package/runtime/__test__/element-template/lynx/env.test.ts +0 -242
- package/runtime/lib/element-template/lynx/env.d.ts +0 -1
- package/runtime/lib/element-template/lynx/env.js.map +0 -1
- package/runtime/lib/element-template/runtime/template/main-thread-function.js.map +0 -1
- package/runtime/lib/snapshot/compat/componentIs.d.ts +0 -4
- package/runtime/lib/snapshot/compat/componentIs.js +0 -20
- package/runtime/lib/snapshot/compat/componentIs.js.map +0 -1
- package/runtime/src/snapshot/compat/componentIs.ts +0 -30
- /package/runtime/__test__/element-template/runtime/{template → background}/main-thread-function.test.ts +0 -0
- /package/runtime/lib/element-template/{runtime/template → background}/main-thread-function.d.ts +0 -0
|
@@ -0,0 +1,297 @@
|
|
|
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
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
5
|
+
|
|
6
|
+
import { WorkletEvents } from '../../src/worklet-runtime/bindings/events.js';
|
|
7
|
+
import { disposeWorkletRuntime, resetWorkletRuntimeForReload } from '../../src/worklet-runtime/bindings/lifecycle.js';
|
|
8
|
+
import { onHydrationFinished } from '../../src/worklet-runtime/bindings/observers.js';
|
|
9
|
+
import {
|
|
10
|
+
getFromWorkletRefMap,
|
|
11
|
+
isHydratedWorkletValue,
|
|
12
|
+
updateWorkletRefInitValueChanges,
|
|
13
|
+
} from '../../src/worklet-runtime/workletRef.js';
|
|
14
|
+
import { initWorklet } from '../../src/worklet-runtime/workletRuntime.js';
|
|
15
|
+
|
|
16
|
+
describe('worklet-runtime lifecycle contract', () => {
|
|
17
|
+
let originalLynx;
|
|
18
|
+
let originalSystemInfo;
|
|
19
|
+
let originalSetTimeout;
|
|
20
|
+
let originalSetInterval;
|
|
21
|
+
let originalClearTimeout;
|
|
22
|
+
let originalClearInterval;
|
|
23
|
+
let originalRequestAnimationFrame;
|
|
24
|
+
let originalCancelAnimationFrame;
|
|
25
|
+
|
|
26
|
+
beforeEach(() => {
|
|
27
|
+
originalLynx = globalThis.lynx;
|
|
28
|
+
originalSystemInfo = globalThis.SystemInfo;
|
|
29
|
+
originalSetTimeout = globalThis.setTimeout;
|
|
30
|
+
originalSetInterval = globalThis.setInterval;
|
|
31
|
+
originalClearTimeout = globalThis.clearTimeout;
|
|
32
|
+
originalClearInterval = globalThis.clearInterval;
|
|
33
|
+
originalRequestAnimationFrame = globalThis.requestAnimationFrame;
|
|
34
|
+
originalCancelAnimationFrame = globalThis.cancelAnimationFrame;
|
|
35
|
+
|
|
36
|
+
globalThis.SystemInfo = { lynxSdkVersion: '2.16' };
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
vi.restoreAllMocks();
|
|
41
|
+
globalThis.lynxWorkletImpl?._jsFunctionLifecycleManager?.dispose?.();
|
|
42
|
+
delete globalThis.lynxWorkletImpl;
|
|
43
|
+
delete globalThis.registerWorklet;
|
|
44
|
+
delete globalThis.registerWorkletInternal;
|
|
45
|
+
delete globalThis.runWorklet;
|
|
46
|
+
globalThis.lynx = originalLynx;
|
|
47
|
+
globalThis.SystemInfo = originalSystemInfo;
|
|
48
|
+
globalThis.setTimeout = originalSetTimeout;
|
|
49
|
+
globalThis.setInterval = originalSetInterval;
|
|
50
|
+
globalThis.clearTimeout = originalClearTimeout;
|
|
51
|
+
globalThis.clearInterval = originalClearInterval;
|
|
52
|
+
globalThis.requestAnimationFrame = originalRequestAnimationFrame;
|
|
53
|
+
globalThis.cancelAnimationFrame = originalCancelAnimationFrame;
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it('no-ops cleanly when lynxWorkletImpl is not initialized', () => {
|
|
57
|
+
delete globalThis.lynxWorkletImpl;
|
|
58
|
+
|
|
59
|
+
expect(() => resetWorkletRuntimeForReload()).not.toThrow();
|
|
60
|
+
expect(() => disposeWorkletRuntime()).not.toThrow();
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it('handles partial runtime mocks safely for reload and dispose', () => {
|
|
64
|
+
globalThis.lynxWorkletImpl = {};
|
|
65
|
+
expect(() => resetWorkletRuntimeForReload()).not.toThrow();
|
|
66
|
+
expect(() => disposeWorkletRuntime()).not.toThrow();
|
|
67
|
+
expect('_jsFunctionLifecycleManager' in globalThis.lynxWorkletImpl).toBe(false);
|
|
68
|
+
|
|
69
|
+
globalThis.lynxWorkletImpl = {
|
|
70
|
+
_jsFunctionLifecycleManager: { addRef: vi.fn() },
|
|
71
|
+
};
|
|
72
|
+
expect(() => resetWorkletRuntimeForReload()).not.toThrow();
|
|
73
|
+
expect(globalThis.lynxWorkletImpl._jsFunctionLifecycleManager).toBeDefined();
|
|
74
|
+
|
|
75
|
+
expect(() => disposeWorkletRuntime()).not.toThrow();
|
|
76
|
+
expect(globalThis.lynxWorkletImpl._jsFunctionLifecycleManager).toBeUndefined();
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('resets aborted first-screen state on reload while preserving VM registrations and positive refs', () => {
|
|
80
|
+
const dispatchEvent = vi.fn();
|
|
81
|
+
globalThis.lynx = {
|
|
82
|
+
...originalLynx,
|
|
83
|
+
getJSContext: () => ({ dispatchEvent }),
|
|
84
|
+
};
|
|
85
|
+
initWorklet();
|
|
86
|
+
|
|
87
|
+
const lazyWorklet = vi.fn(() => 'from-lazy');
|
|
88
|
+
globalThis.registerWorklet('main-thread', 'lazy-worklet', lazyWorklet);
|
|
89
|
+
globalThis.lynxWorkletImpl._refImpl.registerMainThreadObjectType(
|
|
90
|
+
'@test/lazy-object',
|
|
91
|
+
value => ({ value }),
|
|
92
|
+
);
|
|
93
|
+
updateWorkletRefInitValueChanges([[1, 'live-positive', '@test/lazy-object']]);
|
|
94
|
+
const positiveObj = getFromWorkletRefMap({ _wvid: 1 });
|
|
95
|
+
const firstScreenRef = getFromWorkletRefMap({ _wvid: -1, _initValue: 'aborted-first-screen' });
|
|
96
|
+
const delayedTask = vi.fn();
|
|
97
|
+
const firstScreenHandle = { _isFirstScreen: true };
|
|
98
|
+
globalThis.lynxWorkletImpl._runOnBackgroundDelayImpl.delayRunOnBackground(firstScreenHandle, delayedTask);
|
|
99
|
+
globalThis.lynxWorkletImpl._eventDelayImpl.delayExecUntilJsReady('legacy-event', [{ currentTarget: {} }]);
|
|
100
|
+
|
|
101
|
+
const previousManager = globalThis.lynxWorkletImpl._jsFunctionLifecycleManager;
|
|
102
|
+
previousManager.addRef(5, {});
|
|
103
|
+
|
|
104
|
+
resetWorkletRuntimeForReload();
|
|
105
|
+
|
|
106
|
+
const nextManager = globalThis.lynxWorkletImpl._jsFunctionLifecycleManager;
|
|
107
|
+
expect(nextManager).toBeDefined();
|
|
108
|
+
expect(nextManager).not.toBe(previousManager);
|
|
109
|
+
expect(globalThis.lynxWorkletImpl._runOnBackgroundDelayImpl.epoch).toBe(1);
|
|
110
|
+
expect(globalThis.lynxWorkletImpl._runOnBackgroundDelayImpl.delayedBackgroundFunctionArray).toHaveLength(0);
|
|
111
|
+
expect(firstScreenHandle).not.toHaveProperty('_delayIndices');
|
|
112
|
+
expect(globalThis.lynxWorkletImpl._refImpl._firstScreenWorkletRefMap).toEqual({});
|
|
113
|
+
expect(globalThis.lynxWorkletImpl._eventDelayImpl._delayedWorkletParamsMap.size).toBe(0);
|
|
114
|
+
|
|
115
|
+
// VM-scoped registrations and positive-ID refs survive reload.
|
|
116
|
+
expect(globalThis.runWorklet({ _wkltId: 'lazy-worklet' }, [])).toBe('from-lazy');
|
|
117
|
+
expect(getFromWorkletRefMap({ _wvid: 1 })).toBe(positiveObj);
|
|
118
|
+
expect(isHydratedWorkletValue(positiveObj)).toBe(true);
|
|
119
|
+
// Re-allocating negative ID -1 produces a fresh ref instead of returning the aborted one.
|
|
120
|
+
const nextFirstScreenRef = getFromWorkletRefMap({ _wvid: -1, _initValue: 'reloaded-first-screen' });
|
|
121
|
+
expect(nextFirstScreenRef).not.toBe(firstScreenRef);
|
|
122
|
+
expect(nextFirstScreenRef.current).toBe('reloaded-first-screen');
|
|
123
|
+
|
|
124
|
+
// Retired manager finalizers cannot release reused execId 5 on the new page.
|
|
125
|
+
nextManager.addRef(5, {});
|
|
126
|
+
previousManager.removeRef(5);
|
|
127
|
+
previousManager.fire();
|
|
128
|
+
expect(dispatchEvent).not.toHaveBeenCalled();
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
it('disposes lifecycle manager without allocating a replacement on terminal teardown', () => {
|
|
132
|
+
const dispatchEvent = vi.fn();
|
|
133
|
+
globalThis.lynx = {
|
|
134
|
+
...originalLynx,
|
|
135
|
+
getJSContext: () => ({ dispatchEvent }),
|
|
136
|
+
};
|
|
137
|
+
initWorklet();
|
|
138
|
+
|
|
139
|
+
const previousManager = globalThis.lynxWorkletImpl._jsFunctionLifecycleManager;
|
|
140
|
+
previousManager.addRef(7, {});
|
|
141
|
+
getFromWorkletRefMap({ _wvid: -1, _initValue: 'first-screen' });
|
|
142
|
+
globalThis.lynxWorkletImpl._runOnBackgroundDelayImpl.delayRunOnBackground({ _isFirstScreen: true }, vi.fn());
|
|
143
|
+
globalThis.lynxWorkletImpl._eventDelayImpl.delayExecUntilJsReady('legacy-event', [{}]);
|
|
144
|
+
|
|
145
|
+
disposeWorkletRuntime();
|
|
146
|
+
|
|
147
|
+
expect(globalThis.lynxWorkletImpl._jsFunctionLifecycleManager).toBeUndefined();
|
|
148
|
+
expect(globalThis.lynxWorkletImpl._runOnBackgroundDelayImpl.delayedBackgroundFunctionArray).toHaveLength(0);
|
|
149
|
+
expect(globalThis.lynxWorkletImpl._refImpl._firstScreenWorkletRefMap).toEqual({});
|
|
150
|
+
expect(globalThis.lynxWorkletImpl._eventDelayImpl._delayedWorkletParamsMap.size).toBe(0);
|
|
151
|
+
|
|
152
|
+
previousManager.removeRef(7);
|
|
153
|
+
previousManager.fire();
|
|
154
|
+
expect(dispatchEvent).not.toHaveBeenCalled();
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
it('invalidates scheduled legacy worklet replays on reload and dispose without breaking hydration replay', () => {
|
|
158
|
+
vi.useFakeTimers();
|
|
159
|
+
try {
|
|
160
|
+
initWorklet();
|
|
161
|
+
const handler = vi.fn();
|
|
162
|
+
globalThis.registerWorklet('main-thread', 'legacy-tap', handler);
|
|
163
|
+
const element = {};
|
|
164
|
+
const worklet = { _wkltId: 'legacy-tap' };
|
|
165
|
+
const makeEvent = () => ({ currentTarget: { elementRefptr: element } });
|
|
166
|
+
|
|
167
|
+
// Normal hydration finish clears unmatched entries but preserves scheduled replay.
|
|
168
|
+
globalThis.runWorklet({ _lepusWorkletHash: 'legacy-tap' }, [makeEvent(), 'hydrated']);
|
|
169
|
+
globalThis.lynxWorkletImpl._eventDelayImpl.runDelayedWorklet(worklet, element);
|
|
170
|
+
onHydrationFinished();
|
|
171
|
+
vi.runAllTimers();
|
|
172
|
+
expect(handler).toHaveBeenCalledExactlyOnceWith(expect.anything(), 'hydrated');
|
|
173
|
+
|
|
174
|
+
// Reload invalidates already-scheduled replay.
|
|
175
|
+
handler.mockClear();
|
|
176
|
+
globalThis.runWorklet({ _lepusWorkletHash: 'legacy-tap' }, [makeEvent(), 'stale-reload']);
|
|
177
|
+
globalThis.lynxWorkletImpl._eventDelayImpl.runDelayedWorklet(worklet, element);
|
|
178
|
+
expect(vi.getTimerCount()).toBe(1);
|
|
179
|
+
resetWorkletRuntimeForReload();
|
|
180
|
+
vi.runAllTimers();
|
|
181
|
+
expect(handler).not.toHaveBeenCalled();
|
|
182
|
+
|
|
183
|
+
// Terminal dispose invalidates already-scheduled replay.
|
|
184
|
+
globalThis.runWorklet({ _lepusWorkletHash: 'legacy-tap' }, [makeEvent(), 'stale-dispose']);
|
|
185
|
+
globalThis.lynxWorkletImpl._eventDelayImpl.runDelayedWorklet(worklet, element);
|
|
186
|
+
expect(vi.getTimerCount()).toBe(1);
|
|
187
|
+
disposeWorkletRuntime();
|
|
188
|
+
vi.runAllTimers();
|
|
189
|
+
expect(handler).not.toHaveBeenCalled();
|
|
190
|
+
} finally {
|
|
191
|
+
vi.useRealTimers();
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
it('preserves lazy registrations, allows factory refresh, and avoids duplicate listeners across entry re-evaluations', async () => {
|
|
196
|
+
const listeners = new Map();
|
|
197
|
+
const jsContext = {
|
|
198
|
+
addEventListener: vi.fn((type, fn) => {
|
|
199
|
+
if (!listeners.has(type)) listeners.set(type, []);
|
|
200
|
+
listeners.get(type).push(fn);
|
|
201
|
+
}),
|
|
202
|
+
dispatchEvent: vi.fn(),
|
|
203
|
+
};
|
|
204
|
+
globalThis.lynx = {
|
|
205
|
+
...originalLynx,
|
|
206
|
+
getJSContext: () => jsContext,
|
|
207
|
+
setTimeout: originalSetTimeout,
|
|
208
|
+
setInterval: originalSetInterval,
|
|
209
|
+
clearTimeout: originalClearTimeout,
|
|
210
|
+
clearInterval: originalClearInterval,
|
|
211
|
+
requestAnimationFrame: originalRequestAnimationFrame,
|
|
212
|
+
cancelAnimationFrame: originalCancelAnimationFrame,
|
|
213
|
+
};
|
|
214
|
+
|
|
215
|
+
vi.resetModules();
|
|
216
|
+
const initialEntry = await import('@lynx-js/react/worklet-runtime/init');
|
|
217
|
+
expect(listeners.get(WorkletEvents.runWorkletCtx)).toHaveLength(1);
|
|
218
|
+
expect(listeners.get(WorkletEvents.releaseWorkletRef)).toHaveLength(1);
|
|
219
|
+
|
|
220
|
+
// Simulate a lazy bundle registering a worklet and MainThreadObject type once.
|
|
221
|
+
const lazyWorklet = vi.fn(() => 'lazy-ok');
|
|
222
|
+
initialEntry.registerWorklet('main-thread', 'lazy-bundle-fn', lazyWorklet);
|
|
223
|
+
globalThis.lynxWorkletImpl._refImpl.registerMainThreadObjectType(
|
|
224
|
+
'@test/lazy-type',
|
|
225
|
+
value => ({ source: 'lazy', value }),
|
|
226
|
+
);
|
|
227
|
+
// Simulate a main-entry module registering a MainThreadObject type bound to module-local state.
|
|
228
|
+
const makeEntryFactory = (epoch) => value => ({ epoch, value });
|
|
229
|
+
globalThis.lynxWorkletImpl._refImpl.registerMainThreadObjectType(
|
|
230
|
+
'@test/entry-type',
|
|
231
|
+
makeEntryFactory(1),
|
|
232
|
+
);
|
|
233
|
+
updateWorkletRefInitValueChanges([[1, 'before', '@test/entry-type']]);
|
|
234
|
+
const realizedBefore = getFromWorkletRefMap({ _wvid: 1 });
|
|
235
|
+
expect(realizedBefore).toEqual({ epoch: 1, value: 'before' });
|
|
236
|
+
|
|
237
|
+
// Perform two consecutive reload + entry re-evaluation cycles.
|
|
238
|
+
for (const nextEpoch of [2, 3]) {
|
|
239
|
+
const retiredManager = globalThis.lynxWorkletImpl._jsFunctionLifecycleManager;
|
|
240
|
+
retiredManager.addRef(1, {});
|
|
241
|
+
resetWorkletRuntimeForReload();
|
|
242
|
+
|
|
243
|
+
vi.resetModules();
|
|
244
|
+
const reEvaluatedEntry = await import('@lynx-js/react/worklet-runtime/init');
|
|
245
|
+
expect(reEvaluatedEntry.registerWorklet).toBe(globalThis.registerWorkletInternal);
|
|
246
|
+
|
|
247
|
+
// Re-evaluated main-entry module refreshes its factory with identical source identity.
|
|
248
|
+
globalThis.lynxWorkletImpl._refImpl.registerMainThreadObjectType(
|
|
249
|
+
'@test/entry-type',
|
|
250
|
+
makeEntryFactory(nextEpoch),
|
|
251
|
+
);
|
|
252
|
+
|
|
253
|
+
// Listeners must not be duplicated on repeated entry re-evaluations, and delivery still works.
|
|
254
|
+
expect(listeners.get(WorkletEvents.runWorkletCtx)).toHaveLength(1);
|
|
255
|
+
expect(listeners.get(WorkletEvents.releaseWorkletRef)).toHaveLength(1);
|
|
256
|
+
lazyWorklet.mockClear();
|
|
257
|
+
listeners.get(WorkletEvents.runWorkletCtx)[0]({
|
|
258
|
+
data: JSON.stringify({
|
|
259
|
+
worklet: { _wkltId: 'lazy-bundle-fn' },
|
|
260
|
+
params: [],
|
|
261
|
+
resolveId: nextEpoch,
|
|
262
|
+
}),
|
|
263
|
+
});
|
|
264
|
+
expect(lazyWorklet).toHaveBeenCalledOnce();
|
|
265
|
+
|
|
266
|
+
// Lifecycle manager was recreated cleanly and retired finalizers are inert.
|
|
267
|
+
jsContext.dispatchEvent.mockClear();
|
|
268
|
+
const activeManager = globalThis.lynxWorkletImpl._jsFunctionLifecycleManager;
|
|
269
|
+
expect(activeManager).toBeDefined();
|
|
270
|
+
expect(activeManager).not.toBe(retiredManager);
|
|
271
|
+
retiredManager.removeRef(1);
|
|
272
|
+
retiredManager.fire();
|
|
273
|
+
expect(jsContext.dispatchEvent).not.toHaveBeenCalled();
|
|
274
|
+
|
|
275
|
+
// Cached lazy bundle registrations & existing object brands still work.
|
|
276
|
+
expect(globalThis.runWorklet({ _wkltId: 'lazy-bundle-fn' }, [])).toBe('lazy-ok');
|
|
277
|
+
updateWorkletRefInitValueChanges([
|
|
278
|
+
[nextEpoch * 10, `lazy-${nextEpoch}`, '@test/lazy-type'],
|
|
279
|
+
[nextEpoch * 10 + 1, `entry-${nextEpoch}`, '@test/entry-type'],
|
|
280
|
+
]);
|
|
281
|
+
expect(getFromWorkletRefMap({ _wvid: nextEpoch * 10 })).toEqual({
|
|
282
|
+
source: 'lazy',
|
|
283
|
+
value: `lazy-${nextEpoch}`,
|
|
284
|
+
});
|
|
285
|
+
expect(getFromWorkletRefMap({ _wvid: nextEpoch * 10 + 1 })).toEqual({
|
|
286
|
+
epoch: nextEpoch,
|
|
287
|
+
value: `entry-${nextEpoch}`,
|
|
288
|
+
});
|
|
289
|
+
// Existing realized object keeps its brand across entry re-evaluations.
|
|
290
|
+
expect(() => updateWorkletRefInitValueChanges([[1, 'mismatch', 'main-thread']])).toThrow(
|
|
291
|
+
'Worklet value kind mismatch',
|
|
292
|
+
);
|
|
293
|
+
updateWorkletRefInitValueChanges([[1, 'same', '@test/entry-type']]);
|
|
294
|
+
expect(getFromWorkletRefMap({ _wvid: 1 })).toBe(realizedBefore);
|
|
295
|
+
}
|
|
296
|
+
});
|
|
297
|
+
});
|
|
@@ -10,18 +10,24 @@ describe('profile', () => {
|
|
|
10
10
|
const originalProfileEnd = console.profileEnd;
|
|
11
11
|
|
|
12
12
|
afterEach(() => {
|
|
13
|
+
vi.unstubAllGlobals();
|
|
13
14
|
console.profile = originalProfile;
|
|
14
15
|
console.profileEnd = originalProfileEnd;
|
|
15
16
|
});
|
|
16
17
|
|
|
17
|
-
it('
|
|
18
|
-
|
|
18
|
+
it.each([true, false])('respects __PROFILE__=%s independently of __DEV__', (enabled) => {
|
|
19
|
+
vi.stubGlobal('__PROFILE__', enabled);
|
|
20
|
+
vi.stubGlobal('__DEV__', !enabled);
|
|
19
21
|
console.profile = vi.fn();
|
|
20
22
|
console.profileEnd = vi.fn();
|
|
21
23
|
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
24
|
+
expect(profile('x', () => 123)).toBe(123);
|
|
25
|
+
if (enabled) {
|
|
26
|
+
expect(console.profile).toHaveBeenCalledWith('x');
|
|
27
|
+
expect(console.profileEnd).toHaveBeenCalledTimes(1);
|
|
28
|
+
} else {
|
|
29
|
+
expect(console.profile).not.toHaveBeenCalled();
|
|
30
|
+
expect(console.profileEnd).not.toHaveBeenCalled();
|
|
31
|
+
}
|
|
26
32
|
});
|
|
27
33
|
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
// Copyright 2024 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
|
+
import { Suspense, createElement, lazy } from 'preact/compat';
|
|
5
|
+
import { useMemo } from '../hooks/react.js';
|
|
6
|
+
import { loadLazyBundle } from '../lynx/lazy-bundle.js';
|
|
7
|
+
/**
|
|
8
|
+
* @internal Compatibility for the compiler lowering of <component is=? />.
|
|
9
|
+
*/
|
|
10
|
+
export function __ComponentIsPolyfill({ is, ...props }) {
|
|
11
|
+
const DynamicComponent = useMemo(
|
|
12
|
+
// Lazy bundles expose React types but run on the host's Preact renderer.
|
|
13
|
+
() => lazy(() => loadLazyBundle(is)), [is]);
|
|
14
|
+
if (typeof is !== 'string') {
|
|
15
|
+
lynx.reportError(new Error('You must provide a string to props `is` when using syntax `<component is=? />`.'));
|
|
16
|
+
return null;
|
|
17
|
+
}
|
|
18
|
+
return createElement(Suspense, { key: is, fallback: null }, createElement(DynamicComponent, props));
|
|
19
|
+
}
|
|
20
|
+
//# sourceMappingURL=componentIs.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"componentIs.js","sourceRoot":"","sources":["../../../src/core/compat/componentIs.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAG1D,OAAO,EAAE,QAAQ,EAAE,aAAa,EAAE,IAAI,EAAE,MAAM,eAAe,CAAC;AAE9D,OAAO,EAAE,OAAO,EAAE,MAAM,mBAAmB,CAAC;AAC5C,OAAO,EAAE,cAAc,EAAE,MAAM,wBAAwB,CAAC;AAExD;;GAEG;AACH,MAAM,UAAU,qBAAqB,CAAC,EAAE,EAAE,EAAE,GAAG,KAAK,EAAkB;IACpE,MAAM,gBAAgB,GAAG,OAAO;IAC9B,yEAAyE;IACzE,GAAG,EAAE,CACH,IAAI,CAAyC,GAAG,EAAE,CAChD,cAAc,CAAC,EAAE,CAAiE,CACnF,EACH,CAAC,EAAE,CAAC,CACL,CAAC;IACF,IAAI,OAAO,EAAE,KAAK,QAAQ,EAAE,CAAC;QAC3B,IAAI,CAAC,WAAW,CACd,IAAI,KAAK,CACP,iFAAiF,CAClF,CACF,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;IACD,OAAO,aAAa,CAAC,QAAQ,EAAE,EAAE,GAAG,EAAE,EAAE,EAAE,QAAQ,EAAE,IAAI,EAAE,EAAE,aAAa,CAAC,gBAAgB,EAAE,KAAK,CAAC,CAAC,CAAC;AACtG,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport type { ComponentType } from 'preact';\nimport { Suspense, createElement, lazy } from 'preact/compat';\n\nimport { useMemo } from '../hooks/react.js';\nimport { loadLazyBundle } from '../lynx/lazy-bundle.js';\n\n/**\n * @internal Compatibility for the compiler lowering of <component is=? />.\n */\nexport function __ComponentIsPolyfill({ is, ...props }: { is: string }) {\n const DynamicComponent = useMemo(\n // Lazy bundles expose React types but run on the host's Preact renderer.\n () =>\n lazy<ComponentType<Record<string, unknown>>>(() =>\n loadLazyBundle(is) as Promise<{ default: ComponentType<Record<string, unknown>> }>\n ),\n [is],\n );\n if (typeof is !== 'string') {\n lynx.reportError(\n new Error(\n 'You must provide a string to props `is` when using syntax `<component is=? />`.',\n ),\n );\n return null;\n }\n return createElement(Suspense, { key: is, fallback: null }, createElement(DynamicComponent, props));\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function setupSharedLynxEnv(): void;
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
// Copyright 2024 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 { createProcessData } from '
|
|
5
|
-
export function
|
|
4
|
+
import { createProcessData } from '../lynx-data-processors.js';
|
|
5
|
+
export function setupSharedLynxEnv() {
|
|
6
6
|
if (!__LEPUS__) {
|
|
7
7
|
let initData = {};
|
|
8
8
|
let updateData = {};
|
|
@@ -41,6 +41,7 @@ export function setupLynxEnv() {
|
|
|
41
41
|
lynx.registerDataProcessors = function (dataProcessorDefinition) {
|
|
42
42
|
globalThis.processData = createProcessData(dataProcessorDefinition);
|
|
43
43
|
};
|
|
44
|
+
// register empty DataProcessors to make sure `globalThis.processData` is set
|
|
44
45
|
lynx.registerDataProcessors();
|
|
45
46
|
}
|
|
46
47
|
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"env.js","sourceRoot":"","sources":["../../../src/core/lynx/env.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,iBAAiB,EAAE,MAAM,4BAA4B,CAAC;AAG/D,MAAM,UAAU,kBAAkB;IAChC,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,IAAI,QAAQ,GAA4B,EAAE,CAAC;QAC3C,IAAI,UAAU,GAA4B,EAAE,CAAC;QAE7C,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,EAAE,CAAC,OAAO,CAAC;YACrC,IAAI,MAAM,EAAE,CAAC;gBACX,QAAQ,GAAG,MAAM,CAAC,QAAQ,IAAI,EAAE,CAAC;gBACjC,UAAU,GAAG,MAAM,CAAC,UAAU,IAAI,EAAE,CAAC;YACvC,CAAC;QACH,CAAC;QAAC,MAAM,CAAC,CAAA,CAAC;QAEV,IAAI,CAAC,UAAU,GAAG,EAAE,GAAG,QAAQ,EAAE,GAAG,UAAU,EAAE,CAAC;QACjD,IAAI,CAAC,sBAAsB,GAAG,cAAY,CAAC,CAAC;IAC9C,CAAC;IAED,IAAI,SAAS,EAAE,CAAC;QACd,IAAI,CAAC,UAAU,GAAG;QAChB,kCAAkC;SACnC,CAAC;QACD,UAA2D,CAAC,UAAU;YACpE,IAA+C,CAAC,UAAU,IAAI,EAAE,CAAC;QAEpE,IAAI,CAAC,2BAA2B,GAAG,UACjC,SAAiB,EACjB,MAAe;YAEf,kBAAkB,CAAC,CAAC,sBAAsB,EAAE,CAAC,SAAS,EAAE,MAAM,CAAC,CAAC,CAAC,CAAC;QACpE,CAAC,CAAC;QAEF,CAAC;YACC,+DAA+D;YAC/D,SAAS,MAAM,CAAC,KAAa;gBAC3B,OAAO,SAAS,KAAK,SAAS,CAAC;YACjC,CAAC;YACD,8GAA8G;YAC9G,wBAAwB;YACxB,IAAI,OAAO,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,KAAK,WAAW,EAAE,CAAC;gBAClD,wBAAwB;gBACxB,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,GAAG,SAAS,CAAC;YACrC,CAAC;QACH,CAAC;QAED,IAAI,CAAC,sBAAsB,GAAG,UAC5B,uBAAiD;YAEjD,UAAU,CAAC,WAAW,GAAG,iBAAiB,CAAC,uBAAuB,CAAC,CAAC;QACtE,CAAC,CAAC;QAEF,6EAA6E;QAC7E,IAAI,CAAC,sBAAsB,EAAE,CAAC;IAChC,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { createProcessData } from '../lynx-data-processors.js';\nimport type { DataProcessorDefinition } from '../lynx-data-processors.js';\n\nexport function setupSharedLynxEnv(): void {\n if (!__LEPUS__) {\n let initData: Record<string, unknown> = {};\n let updateData: Record<string, unknown> = {};\n\n try {\n const params = lynx.getApp()._params;\n if (params) {\n initData = params.initData ?? {};\n updateData = params.updateData ?? {};\n }\n } catch {}\n\n lynx.__initData = { ...initData, ...updateData };\n lynx.registerDataProcessors = function() {};\n }\n\n if (__LEPUS__) {\n lynx.__initData = {\n /* available only in renderPage */\n };\n (globalThis as typeof globalThis & { SystemInfo?: unknown }).SystemInfo =\n (lynx as typeof lynx & { SystemInfo?: unknown }).SystemInfo ?? {};\n\n lynx.triggerGlobalEventFromLepus = function(\n eventName: string,\n params: unknown,\n ) {\n __OnLifecycleEvent(['globalEventFromLepus', [eventName, params]]);\n };\n\n {\n // eslint-disable-next-line unicorn/consistent-function-scoping\n function __name(empty: string) {\n return `Native${empty}Modules`;\n }\n // TODO(hongzhiyuan.hzy): make sure this is run before any other code (especially code access `NativeModules`)\n // @ts-expect-error hack\n if (typeof globalThis[__name('')] === 'undefined') {\n // @ts-expect-error hack\n globalThis[__name('')] = undefined;\n }\n }\n\n lynx.registerDataProcessors = function(\n dataProcessorDefinition?: DataProcessorDefinition,\n ) {\n globalThis.processData = createProcessData(dataProcessorDefinition);\n };\n\n // register empty DataProcessors to make sure `globalThis.processData` is set\n lynx.registerDataProcessors();\n }\n}\n"]}
|
|
@@ -3,3 +3,4 @@ export type RunOnMainThread = <R, Fn extends (...args: any[]) => R>(fn: Fn) => (
|
|
|
3
3
|
export declare let delayedRunOnMainThreadData: RunWorkletCtxData[];
|
|
4
4
|
export declare function enqueueDelayedRunOnMainThreadData(data: RunWorkletCtxData): void;
|
|
5
5
|
export declare function takeDelayedRunOnMainThreadData(): typeof delayedRunOnMainThreadData;
|
|
6
|
+
export declare function prepareMainThreadFunctionCtx(worklet: unknown): void;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
// Copyright 2026 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 { options as preactOptions } from 'preact';
|
|
4
5
|
import { onFunctionCall } from './return-value.js';
|
|
5
6
|
import { isSdkVersionGt } from '../../shared/version.js';
|
|
6
7
|
import { WorkletEvents } from '../../worklet-runtime/bindings/events.js';
|
|
@@ -34,6 +35,8 @@ export function createRunOnMainThread(options) {
|
|
|
34
35
|
if (isRuntimeDestroyed())
|
|
35
36
|
return;
|
|
36
37
|
const worklet = fn;
|
|
38
|
+
if (preactOptions.scheduleMainThreadWorklet?.({ worklet, params, resolve }))
|
|
39
|
+
return;
|
|
37
40
|
prepareMainThreadFunctionCtx(worklet);
|
|
38
41
|
const data = {
|
|
39
42
|
worklet,
|
|
@@ -52,7 +55,7 @@ export function createRunOnMainThread(options) {
|
|
|
52
55
|
function isRunOnBackgroundSupported() {
|
|
53
56
|
return isSdkVersionGt(2, 15);
|
|
54
57
|
}
|
|
55
|
-
function prepareMainThreadFunctionCtx(worklet) {
|
|
58
|
+
export function prepareMainThreadFunctionCtx(worklet) {
|
|
56
59
|
if (isMainThreadFunction(worklet) && isRunOnBackgroundSupported()) {
|
|
57
60
|
registerBackgroundFunctionCtx(worklet);
|
|
58
61
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main-thread.js","sourceRoot":"","sources":["../../../src/core/thread-function-call/main-thread.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,0CAA0C,CAAC;AAGzE,OAAO,EAAE,6BAA6B,EAAE,MAAM,6CAA6C,CAAC;AAC5F,OAAO,EAAE,oBAAoB,EAAE,MAAM,4BAA4B,CAAC;AAClE,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAC;AACpD,OAAO,EAAE,yBAAyB,EAAE,MAAM,2BAA2B,CAAC;AACtE,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAQ3D,MAAM,CAAC,IAAI,0BAA0B,GAAwB,EAAE,CAAC;AAEhE,MAAM,UAAU,iCAAiC,CAAC,IAAuB;IACvE,0BAA0B,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC;AACxC,CAAC;AAED,MAAM,UAAU,8BAA8B;IAC5C,MAAM,IAAI,GAAG,0BAA0B,CAAC;IACxC,0BAA0B,GAAG,EAAE,CAAC;IAChC,OAAO,IAAI,CAAC;AACd,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,qBAAqB,CACnC,OAA+B;IAE/B,OAAO,CAAsC,EAAM,EAA2C,EAAE;QAC9F,IAAI,SAAS,EAAE,CAAC;YACd,MAAM,IAAI,KAAK,CAAC,4DAA4D,CAAC,CAAC;QAChF,CAAC;QACD,IAAI,CAAC,YAAY,EAAE,EAAE,CAAC;YACpB,MAAM,IAAI,KAAK,CAAC,iDAAiD,CAAC,CAAC;QACrE,CAAC;QACD,OAAO,KAAK,EAAE,GAAG,MAAsB,EAAc,EAAE;YACrD,OAAO,IAAI,OAAO,CAAC,CAAC,OAAO,EAAE,EAAE;gBAC7B,IAAI,kBAAkB,EAAE;oBAAE,OAAO;gBACjC,MAAM,OAAO,GAAG,EAAwB,CAAC;gBACzC,4BAA4B,CAAC,OAAO,CAAC,CAAC;gBACtC,MAAM,IAAI,GAAG;oBACX,OAAO;oBACP,MAAM;oBACN,SAAS,EAAE,cAAc,CAAC,OAAO,CAAC;iBACd,CAAC;gBAEvB,IAAI,OAAO,CAAC,qCAAqC,EAAE,EAAE,CAAC;oBACpD,4BAA4B,CAAC,IAAI,CAAC,CAAC;oBACnC,OAAO;gBACT,CAAC;gBAED,iCAAiC,CAAC,IAAI,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,0BAA0B;IACjC,OAAO,cAAc,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC/B,CAAC;AAED,
|
|
1
|
+
{"version":3,"file":"main-thread.js","sourceRoot":"","sources":["../../../src/core/thread-function-call/main-thread.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,OAAO,IAAI,aAAa,EAAE,MAAM,QAAQ,CAAC;AAElD,OAAO,EAAE,cAAc,EAAE,MAAM,mBAAmB,CAAC;AACnD,OAAO,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AACzD,OAAO,EAAE,aAAa,EAAE,MAAM,0CAA0C,CAAC;AAGzE,OAAO,EAAE,6BAA6B,EAAE,MAAM,6CAA6C,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vB,IAAI,OAAO,CAAC,qCAAqC,EAAE,EAAE,CAAC;oBACpD,4BAA4B,CAAC,IAAI,CAAC,CAAC;oBACnC,OAAO;gBACT,CAAC;gBAED,iCAAiC,CAAC,IAAI,CAAC,CAAC;YAC1C,CAAC,CAAC,CAAC;QACL,CAAC,CAAC;IACJ,CAAC,CAAC;AACJ,CAAC;AAED,SAAS,0BAA0B;IACjC,OAAO,cAAc,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC;AAC/B,CAAC;AAED,MAAM,UAAU,4BAA4B,CAAC,OAAgB;IAC3D,IAAI,oBAAoB,CAAC,OAAO,CAAC,IAAI,0BAA0B,EAAE,EAAE,CAAC;QAClE,6BAA6B,CAAC,OAAO,CAAC,CAAC;IACzC,CAAC;AACH,CAAC;AAED,SAAS,4BAA4B,CAAC,IAAuB;IAC3D,yBAAyB,CAAC,IAAI,CAAC,cAAc,EAAE,EAAE,EAAE,IAAI,EAAE,aAAa,CAAC,aAAa,EAAE,IAAI,EAAE,CAAC,CAAC;AAChG,CAAC","sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { options as preactOptions } from 'preact';\n\nimport { onFunctionCall } from './return-value.js';\nimport { isSdkVersionGt } from '../../shared/version.js';\nimport { WorkletEvents } from '../../worklet-runtime/bindings/events.js';\nimport type { RunWorkletCtxData } from '../../worklet-runtime/bindings/events.js';\nimport type { Worklet } from '../../worklet-runtime/bindings/types.js';\nimport { registerBackgroundFunctionCtx } from '../background-function/run-on-background.js';\nimport { isMainThreadFunction } from '../main-thread-function.js';\nimport { isMtsEnabled } from '../mts-capability.js';\nimport { dispatchReloadScopedEvent } from '../reload-scoped-event.js';\nimport { isRuntimeDestroyed } from '../runtime-destroy.js';\n\ninterface RunOnMainThreadOptions {\n shouldDispatchRunOnMainThreadDirectly: () => boolean;\n}\n\nexport type RunOnMainThread = <R, Fn extends (...args: any[]) => R>(fn: Fn) => (...args: Parameters<Fn>) => Promise<R>;\n\nexport let delayedRunOnMainThreadData: RunWorkletCtxData[] = [];\n\nexport function enqueueDelayedRunOnMainThreadData(data: RunWorkletCtxData): void {\n delayedRunOnMainThreadData.push(data);\n}\n\nexport function takeDelayedRunOnMainThreadData(): typeof delayedRunOnMainThreadData {\n const data = delayedRunOnMainThreadData;\n delayedRunOnMainThreadData = [];\n return data;\n}\n\n/**\n * @internal\n */\nexport function createRunOnMainThread(\n options: RunOnMainThreadOptions,\n): RunOnMainThread {\n return <R, Fn extends (...args: any[]) => R>(fn: Fn): (...args: Parameters<Fn>) => Promise<R> => {\n if (__LEPUS__) {\n throw new Error('runOnMainThread can only be used on the background thread.');\n }\n if (!isMtsEnabled()) {\n throw new Error('runOnMainThread requires Lynx sdk version 2.14.');\n }\n return async (...params: Parameters<Fn>): Promise<R> => {\n return new Promise((resolve) => {\n if (isRuntimeDestroyed()) return;\n const worklet = fn as unknown as Worklet;\n if (preactOptions.scheduleMainThreadWorklet?.({ worklet, params, resolve })) return;\n\n prepareMainThreadFunctionCtx(worklet);\n const data = {\n worklet,\n params,\n resolveId: onFunctionCall(resolve),\n } as RunWorkletCtxData;\n\n if (options.shouldDispatchRunOnMainThreadDirectly()) {\n dispatchRunOnMainThreadEvent(data);\n return;\n }\n\n enqueueDelayedRunOnMainThreadData(data);\n });\n };\n };\n}\n\nfunction isRunOnBackgroundSupported(): boolean {\n return isSdkVersionGt(2, 15);\n}\n\nexport function prepareMainThreadFunctionCtx(worklet: unknown): void {\n if (isMainThreadFunction(worklet) && isRunOnBackgroundSupported()) {\n registerBackgroundFunctionCtx(worklet);\n }\n}\n\nfunction dispatchRunOnMainThreadEvent(data: RunWorkletCtxData): void {\n dispatchReloadScopedEvent(lynx.getCoreContext(), { type: WorkletEvents.runWorkletCtx, data });\n}\n"]}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { RunWorkletCtxData } from '../../worklet-runtime/bindings/events.js';
|
|
2
|
+
import type { ElementTemplateUpdateEvent } from '../protocol/update-event.js';
|
|
3
|
+
export declare function dropDelayedRunOnMainThreadReturnIds(delayedRunOnMainThreadPayload: readonly {
|
|
4
|
+
resolveId: number;
|
|
5
|
+
}[] | undefined): void;
|
|
6
|
+
export declare function packElementTemplateCommitUpdateEvent(delayedRunOnMainThreadData: RunWorkletCtxData[] | undefined, isHydration?: boolean): ElementTemplateUpdateEvent;
|
|
@@ -0,0 +1,36 @@
|
|
|
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
|
+
import { globalCommitContext } from './commit-context.js';
|
|
5
|
+
import { getReloadVersion } from '../../core/reload-version.js';
|
|
6
|
+
import { dropFunctionCallReturnIds } from '../../core/thread-function-call/return-value.js';
|
|
7
|
+
import { formatElementTemplateUpdateCommands } from '../debug/alog.js';
|
|
8
|
+
import { createElementTemplateUpdateEvent } from '../protocol/update-event.js';
|
|
9
|
+
export function dropDelayedRunOnMainThreadReturnIds(delayedRunOnMainThreadPayload) {
|
|
10
|
+
if (delayedRunOnMainThreadPayload) {
|
|
11
|
+
dropFunctionCallReturnIds(delayedRunOnMainThreadPayload.map(data => data.resolveId));
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export function packElementTemplateCommitUpdateEvent(delayedRunOnMainThreadData, isHydration = false) {
|
|
15
|
+
if (typeof __ALOG__ !== 'undefined' && __ALOG__) {
|
|
16
|
+
console.alog?.((isHydration
|
|
17
|
+
? '[ReactLynxDebug] ElementTemplate hydrate update commands:\n'
|
|
18
|
+
: '[ReactLynxDebug] ElementTemplate BTS -> MTS update:\n')
|
|
19
|
+
+ JSON.stringify({
|
|
20
|
+
ops: formatElementTemplateUpdateCommands(globalCommitContext.ops),
|
|
21
|
+
flushOptions: globalCommitContext.flushOptions,
|
|
22
|
+
flowIds: globalCommitContext.flowIds,
|
|
23
|
+
isHydration: isHydration || undefined,
|
|
24
|
+
delayedRunOnMainThreadDataCount: delayedRunOnMainThreadData?.length,
|
|
25
|
+
}, null, 2));
|
|
26
|
+
}
|
|
27
|
+
return createElementTemplateUpdateEvent({
|
|
28
|
+
ops: globalCommitContext.ops,
|
|
29
|
+
flushOptions: globalCommitContext.flushOptions,
|
|
30
|
+
isHydration: isHydration || undefined,
|
|
31
|
+
reloadVersion: getReloadVersion(),
|
|
32
|
+
flowIds: globalCommitContext.flowIds,
|
|
33
|
+
delayedRunOnMainThreadData,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
//# sourceMappingURL=commit-event.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"commit-event.js","sourceRoot":"","sources":["../../../src/element-template/background/commit-event.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAC1D,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EAAE,yBAAyB,EAAE,MAAM,iDAAiD,CAAC;AAE5F,OAAO,EAAE,mCAAmC,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAE,gCAAgC,EAAE,MAAM,6BAA6B,CAAC;AAG/E,MAAM,UAAU,mCAAmC,CACjD,6BAA2E;IAE3E,IAAI,6BAA6B,EAAE,CAAC;QAClC,yBAAyB,CAAC,6BAA6B,CAAC,GAAG,CAAC,IAAI,CAAC,EAAE,CAAC,IAAI,CAAC,SAAS,CAAC,CAAC,CAAC;IACvF,CAAC;AACH,CAAC;AAED,MAAM,UAAU,oCAAoC,CAClD,0BAA2D,EAC3D,WAAW,GAAG,KAAK;IAEnB,IAAI,OAAO,QAAQ,KAAK,WAAW,IAAI,QAAQ,EAAE,CAAC;QAChD,OAAO,CAAC,IAAI,EAAE,CACZ,CAAC,WAAW;YACV,CAAC,CAAC,6DAA6D;YAC/D,CAAC,CAAC,uDAAuD,CAAC;cACxD,IAAI,CAAC,SAAS,CACd;gBACE,GAAG,EAAE,mCAAmC,CAAC,mBAAmB,CAAC,GAAG,CAAC;gBACjE,YAAY,EAAE,mBAAmB,CAAC,YAAY;gBAC9C,OAAO,EAAE,mBAAmB,CAAC,OAAO;gBACpC,WAAW,EAAE,WAAW,IAAI,SAAS;gBACrC,+BAA+B,EAAE,0BAA0B,EAAE,MAAM;aACpE,EACD,IAAI,EACJ,CAAC,CACF,CACJ,CAAC;IACJ,CAAC;IAED,OAAO,gCAAgC,CAAC;QACtC,GAAG,EAAE,mBAAmB,CAAC,GAAG;QAC5B,YAAY,EAAE,mBAAmB,CAAC,YAAY;QAC9C,WAAW,EAAE,WAAW,IAAI,SAAS;QACrC,aAAa,EAAE,gBAAgB,EAAE;QACjC,OAAO,EAAE,mBAAmB,CAAC,OAAO;QACpC,0BAA0B;KAC3B,CAAC,CAAC;AACL,CAAC","sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { globalCommitContext } from './commit-context.js';\nimport { getReloadVersion } from '../../core/reload-version.js';\nimport { dropFunctionCallReturnIds } from '../../core/thread-function-call/return-value.js';\nimport type { RunWorkletCtxData } from '../../worklet-runtime/bindings/events.js';\nimport { formatElementTemplateUpdateCommands } from '../debug/alog.js';\nimport { createElementTemplateUpdateEvent } from '../protocol/update-event.js';\nimport type { ElementTemplateUpdateEvent } from '../protocol/update-event.js';\n\nexport function dropDelayedRunOnMainThreadReturnIds(\n delayedRunOnMainThreadPayload: readonly { resolveId: number }[] | undefined,\n): void {\n if (delayedRunOnMainThreadPayload) {\n dropFunctionCallReturnIds(delayedRunOnMainThreadPayload.map(data => data.resolveId));\n }\n}\n\nexport function packElementTemplateCommitUpdateEvent(\n delayedRunOnMainThreadData: RunWorkletCtxData[] | undefined,\n isHydration = false,\n): ElementTemplateUpdateEvent {\n if (typeof __ALOG__ !== 'undefined' && __ALOG__) {\n console.alog?.(\n (isHydration\n ? '[ReactLynxDebug] ElementTemplate hydrate update commands:\\n'\n : '[ReactLynxDebug] ElementTemplate BTS -> MTS update:\\n')\n + JSON.stringify(\n {\n ops: formatElementTemplateUpdateCommands(globalCommitContext.ops),\n flushOptions: globalCommitContext.flushOptions,\n flowIds: globalCommitContext.flowIds,\n isHydration: isHydration || undefined,\n delayedRunOnMainThreadDataCount: delayedRunOnMainThreadData?.length,\n },\n null,\n 2,\n ),\n );\n }\n\n return createElementTemplateUpdateEvent({\n ops: globalCommitContext.ops,\n flushOptions: globalCommitContext.flushOptions,\n isHydration: isHydration || undefined,\n reloadVersion: getReloadVersion(),\n flowIds: globalCommitContext.flowIds,\n delayedRunOnMainThreadData,\n });\n}\n"]}
|
|
@@ -3,18 +3,15 @@
|
|
|
3
3
|
// LICENSE file in the root directory of this source tree.
|
|
4
4
|
import { options } from 'preact';
|
|
5
5
|
import { globalCommitContext, resetGlobalCommitContext, takeRemovedSubtreesForPostDispatchTeardown, } from './commit-context.js';
|
|
6
|
+
import { dropDelayedRunOnMainThreadReturnIds, packElementTemplateCommitUpdateEvent } from './commit-event.js';
|
|
6
7
|
import { clearElementTemplateRenderScope, resetElementTemplateRenderScope } from './render-scope.js';
|
|
7
8
|
import { globalPipelineOptions, markTiming, markTimingLegacy, setPipeline } from '../../core/performance.js';
|
|
8
|
-
import { getReloadVersion } from '../../core/reload-version.js';
|
|
9
9
|
import { delayedRunOnMainThreadData, takeDelayedRunOnMainThreadData, } from '../../core/thread-function-call/main-thread.js';
|
|
10
|
-
import { dropFunctionCallReturnIds } from '../../core/thread-function-call/return-value.js';
|
|
11
10
|
import { COMMIT } from '../../shared/render-constants.js';
|
|
12
11
|
import { hook, isEmptyObject } from '../../shared/utils.js';
|
|
13
|
-
import { formatElementTemplateUpdateCommands } from '../debug/alog.js';
|
|
14
12
|
import { profileEnd, profileStart } from '../debug/profile.js';
|
|
15
13
|
import { sendMTRefInitValueToMainThread } from '../main-thread-ref-init-value.js';
|
|
16
14
|
import { clearPendingRefs, flushPendingRefs, hasPendingRefs } from '../prop-adapters/ref.js';
|
|
17
|
-
import { createElementTemplateUpdateEvent } from '../protocol/update-event.js';
|
|
18
15
|
let installed = false;
|
|
19
16
|
let previousCommit;
|
|
20
17
|
let hasHydrated = false;
|
|
@@ -49,9 +46,7 @@ export function cancelElementTemplateRemovedSubtreeCleanup() {
|
|
|
49
46
|
scheduledRemovedSubtreeCleanupTimers.clear();
|
|
50
47
|
}
|
|
51
48
|
function cleanupFailedElementTemplateCommit(removedSubtreesAwaitingTeardown, delayedRunOnMainThreadPayload) {
|
|
52
|
-
|
|
53
|
-
dropFunctionCallReturnIds(delayedRunOnMainThreadPayload.map(data => data.resolveId));
|
|
54
|
-
}
|
|
49
|
+
dropDelayedRunOnMainThreadReturnIds(delayedRunOnMainThreadPayload);
|
|
55
50
|
clearPendingRefs();
|
|
56
51
|
resetGlobalCommitContext();
|
|
57
52
|
setPipeline(undefined);
|
|
@@ -89,22 +84,7 @@ function flushElementTemplateCommitChanges() {
|
|
|
89
84
|
: undefined;
|
|
90
85
|
let updateEvent;
|
|
91
86
|
try {
|
|
92
|
-
|
|
93
|
-
console.alog?.('[ReactLynxDebug] ElementTemplate BTS -> MTS update:\n'
|
|
94
|
-
+ JSON.stringify({
|
|
95
|
-
ops: formatElementTemplateUpdateCommands(globalCommitContext.ops),
|
|
96
|
-
flushOptions: globalCommitContext.flushOptions,
|
|
97
|
-
flowIds: globalCommitContext.flowIds,
|
|
98
|
-
delayedRunOnMainThreadDataCount: delayedRunOnMainThreadPayload?.length,
|
|
99
|
-
}, null, 2));
|
|
100
|
-
}
|
|
101
|
-
updateEvent = createElementTemplateUpdateEvent({
|
|
102
|
-
ops: globalCommitContext.ops,
|
|
103
|
-
flushOptions: globalCommitContext.flushOptions,
|
|
104
|
-
reloadVersion: getReloadVersion(),
|
|
105
|
-
flowIds: globalCommitContext.flowIds,
|
|
106
|
-
delayedRunOnMainThreadData: delayedRunOnMainThreadPayload,
|
|
107
|
-
});
|
|
87
|
+
updateEvent = packElementTemplateCommitUpdateEvent(delayedRunOnMainThreadPayload);
|
|
108
88
|
}
|
|
109
89
|
catch (error) {
|
|
110
90
|
cleanupFailedElementTemplateCommit(removedSubtreesAwaitingTeardown, delayedRunOnMainThreadPayload);
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"commit-hook.js","sourceRoot":"","sources":["../../../src/element-template/background/commit-hook.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAEjC,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,0CAA0C,GAC3C,MAAM,qBAAqB,CAAC;AAE7B,OAAO,EAAE,+BAA+B,EAAE,+BAA+B,EAAE,MAAM,mBAAmB,CAAC;AACrG,OAAO,EAAE,qBAAqB,EAAE,UAAU,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7G,OAAO,EAAE,gBAAgB,EAAE,MAAM,8BAA8B,CAAC;AAChE,OAAO,EACL,0BAA0B,EAC1B,8BAA8B,GAC/B,MAAM,gDAAgD,CAAC;AACxD,OAAO,EAAE,yBAAyB,EAAE,MAAM,iDAAiD,CAAC;AAC5F,OAAO,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,mCAAmC,EAAE,MAAM,kBAAkB,CAAC;AACvE,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,OAAO,EAAE,gBAAgB,EAAE,gBAAgB,EAAE,cAAc,EAAE,MAAM,yBAAyB,CAAC;AAC7F,OAAO,EAAE,gCAA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AC;IAEjC,IAAI,CAAC,OAAO,EAAE,MAAM,EAAE,CAAC,cAAc,EAAE,KAAK,EAAE,WAAW,EAAE,EAAE;QAC3D,IAAI,cAAc,EAAE,CAAC;YACnB,+BAA+B,EAAE,CAAC;YAClC,IAAI,CAAC;gBACH,8BAA8B,EAAE,CAAC;YACnC,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,kCAAkC,CAChC,0CAA0C,EAAE,EAC5C,8BAA8B,EAAE,CACjC,CAAC;gBACF,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC;QAED,IAAI,cAAc,IAAI,CAAC,WAAW,IAAI,cAAc,EAAE,EAAE,CAAC;YACvD,2EAA2E;YAC3E,2EAA2E;YAC3E,gBAAgB,EAAE,CAAC;QACrB,CAAC;aAAM,IACL,cAAc,IAAI,WAAW;eAC1B,CACD,mBAAmB,CAAC,GAAG,CAAC,MAAM,GAAG,CAAC;mBAC/B,CAAC,aAAa,CAAC,mBAAmB,CAAC,YAAY,CAAC;mBAChD,cAAc,EAAE;mBAChB,0BAA0B,CAAC,MAAM,GAAG,CAAC,CACzC,EACD,CAAC;YACD,iCAAiC,EAAE,CAAC;QACtC,CAAC;QAED,cAAc,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,4CAA4C;IAC1D,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO;IACT,CAAC;IACD,SAAS,GAAG,KAAK,CAAC;IAClB,IAAI,cAAc,EAAE,CAAC;QACnB,OAAO,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC;QACjC,cAAc,GAAG,SAAS,CAAC;IAC7B,CAAC;SAAM,CAAC;QACN,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { options } from 'preact';\n\nimport {\n globalCommitContext,\n resetGlobalCommitContext,\n takeRemovedSubtreesForPostDispatchTeardown,\n} from './commit-context.js';\nimport type { BackgroundElementTemplateInstance } from './instance.js';\nimport { clearElementTemplateRenderScope, resetElementTemplateRenderScope } from './render-scope.js';\nimport { globalPipelineOptions, markTiming, markTimingLegacy, setPipeline } from '../../core/performance.js';\nimport { getReloadVersion } from '../../core/reload-version.js';\nimport {\n delayedRunOnMainThreadData,\n takeDelayedRunOnMainThreadData,\n} from '../../core/thread-function-call/main-thread.js';\nimport { dropFunctionCallReturnIds } from '../../core/thread-function-call/return-value.js';\nimport { COMMIT } from '../../shared/render-constants.js';\nimport { hook, isEmptyObject } from '../../shared/utils.js';\nimport { formatElementTemplateUpdateCommands } from '../debug/alog.js';\nimport { profileEnd, profileStart } from '../debug/profile.js';\nimport { sendMTRefInitValueToMainThread } from '../main-thread-ref-init-value.js';\nimport { clearPendingRefs, flushPendingRefs, hasPendingRefs } from '../prop-adapters/ref.js';\nimport { createElementTemplateUpdateEvent } from '../protocol/update-event.js';\n\nlet installed = false;\nlet previousCommit: typeof options[typeof COMMIT];\nlet hasHydrated = false;\nconst scheduledRemovedSubtreeCleanupTimers = /*#__PURE__*/ new Set<ReturnType<typeof setTimeout>>();\n\nexport function markElementTemplateHydrated(): void {\n hasHydrated = true;\n}\n\nexport function isElementTemplateHydrated(): boolean {\n return hasHydrated;\n}\n\nexport function resetElementTemplateCommitState(): void {\n hasHydrated = false;\n resetElementTemplateRenderScope();\n resetGlobalCommitContext();\n}\n\nexport function scheduleElementTemplateRemovedSubtreeCleanup(\n removedSubtreesAwaitingTeardown: BackgroundElementTemplateInstance[],\n): void {\n if (removedSubtreesAwaitingTeardown.length === 0) {\n return;\n }\n const timer = setTimeout(() => {\n scheduledRemovedSubtreeCleanupTimers.delete(timer);\n for (const root of removedSubtreesAwaitingTeardown) {\n root.releaseDetachedSubtreeFromManager();\n }\n }, 10000);\n scheduledRemovedSubtreeCleanupTimers.add(timer);\n}\n\nexport function cancelElementTemplateRemovedSubtreeCleanup(): void {\n for (const timer of scheduledRemovedSubtreeCleanupTimers) {\n clearTimeout(timer);\n }\n scheduledRemovedSubtreeCleanupTimers.clear();\n}\n\nfunction cleanupFailedElementTemplateCommit(\n removedSubtreesAwaitingTeardown: BackgroundElementTemplateInstance[],\n delayedRunOnMainThreadPayload: readonly { resolveId: number }[] | undefined,\n): void {\n if (delayedRunOnMainThreadPayload) {\n dropFunctionCallReturnIds(delayedRunOnMainThreadPayload.map(data => data.resolveId));\n }\n clearPendingRefs();\n resetGlobalCommitContext();\n setPipeline(undefined);\n scheduleElementTemplateRemovedSubtreeCleanup(removedSubtreesAwaitingTeardown);\n}\n\nfunction flushElementTemplateCommitChanges(): void {\n const hasNativeOps = globalCommitContext.ops.length > 0;\n const hasDelayedRunOnMainThread = delayedRunOnMainThreadData.length > 0;\n const hasUpdatePayload = hasNativeOps\n || !isEmptyObject(globalCommitContext.flushOptions)\n || hasDelayedRunOnMainThread;\n const removedSubtreesAwaitingTeardown = hasNativeOps ? takeRemovedSubtreesForPostDispatchTeardown() : [];\n if (hasUpdatePayload) {\n markTimingLegacy('updateDiffVdomEnd');\n markTiming('diffVdomEnd');\n\n if (__PROFILE__) {\n profileStart('ReactLynx::commitChanges');\n }\n markTiming('packChangesStart');\n if (globalPipelineOptions) {\n globalCommitContext.flushOptions.pipelineOptions = globalPipelineOptions;\n }\n markTiming('packChangesEnd');\n if (globalPipelineOptions) {\n setPipeline(undefined);\n }\n if (__PROFILE__) {\n profileEnd();\n }\n\n if (!hasNativeOps && !hasDelayedRunOnMainThread) {\n globalCommitContext.flushOptions.emptyPatch = true;\n }\n\n const delayedRunOnMainThreadPayload = hasDelayedRunOnMainThread\n ? takeDelayedRunOnMainThreadData()\n : undefined;\n\n let updateEvent: ReturnType<typeof createElementTemplateUpdateEvent>;\n try {\n if (typeof __ALOG__ !== 'undefined' && __ALOG__) {\n console.alog?.(\n '[ReactLynxDebug] ElementTemplate BTS -> MTS update:\\n'\n + JSON.stringify(\n {\n ops: formatElementTemplateUpdateCommands(globalCommitContext.ops),\n flushOptions: globalCommitContext.flushOptions,\n flowIds: globalCommitContext.flowIds,\n delayedRunOnMainThreadDataCount: delayedRunOnMainThreadPayload?.length,\n },\n null,\n 2,\n ),\n );\n }\n\n updateEvent = createElementTemplateUpdateEvent({\n ops: globalCommitContext.ops,\n flushOptions: globalCommitContext.flushOptions,\n reloadVersion: getReloadVersion(),\n flowIds: globalCommitContext.flowIds,\n delayedRunOnMainThreadData: delayedRunOnMainThreadPayload,\n });\n } catch (error) {\n cleanupFailedElementTemplateCommit(removedSubtreesAwaitingTeardown, delayedRunOnMainThreadPayload);\n throw error;\n }\n\n lynx.getCoreContext().dispatchEvent(updateEvent);\n }\n // When native ops exist, patch first so a newly attached ref observes the\n // committed native state. Ref-only commits still flush through this path.\n flushPendingRefs();\n resetGlobalCommitContext();\n // Match Snapshot's cleanup boundary: start the delayed teardown only\n // after the bridge dispatch, so background JS objects are not torn down\n // before main-thread detach observes the same commit.\n scheduleElementTemplateRemovedSubtreeCleanup(removedSubtreesAwaitingTeardown);\n}\n\nexport function installElementTemplateCommitHook(): void {\n if (installed) {\n return;\n }\n installed = true;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n previousCommit = options[COMMIT];\n\n hook(options, COMMIT, (originalCommit, vnode, commitQueue) => {\n if (__BACKGROUND__) {\n clearElementTemplateRenderScope();\n try {\n sendMTRefInitValueToMainThread();\n } catch (error) {\n cleanupFailedElementTemplateCommit(\n takeRemovedSubtreesForPostDispatchTeardown(),\n takeDelayedRunOnMainThreadData(),\n );\n throw error;\n }\n }\n\n if (__BACKGROUND__ && !hasHydrated && hasPendingRefs()) {\n // User effects can run before ET hydrate arrives, so ordinary refs must be\n // attached on the background commit even though native UI ops are delayed.\n flushPendingRefs();\n } else if (\n __BACKGROUND__ && hasHydrated\n && (\n globalCommitContext.ops.length > 0\n || !isEmptyObject(globalCommitContext.flushOptions)\n || hasPendingRefs()\n || delayedRunOnMainThreadData.length > 0\n )\n ) {\n flushElementTemplateCommitChanges();\n }\n\n originalCommit?.(vnode, commitQueue);\n });\n}\n\n/**\n * Restores the commit option replaced by\n * {@link installElementTemplateCommitHook} so tests can clean up the global\n * Preact `options` object. Assumes the matching install was the most recent\n * commit-option replacement; the snapshot and ElementTemplate runtimes are\n * separate entrypoints and never install their commit hooks in the same\n * context.\n */\nexport function uninstallElementTemplateCommitHookForTesting(): void {\n if (!installed) {\n return;\n }\n installed = false;\n if (previousCommit) {\n options[COMMIT] = previousCommit;\n previousCommit = undefined;\n } else {\n delete options[COMMIT];\n }\n}\n"]}
|
|
1
|
+
{"version":3,"file":"commit-hook.js","sourceRoot":"","sources":["../../../src/element-template/background/commit-hook.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAE1D,OAAO,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAEjC,OAAO,EACL,mBAAmB,EACnB,wBAAwB,EACxB,0CAA0C,GAC3C,MAAM,qBAAqB,CAAC;AAC7B,OAAO,EAAE,mCAAmC,EAAE,oCAAoC,EAAE,MAAM,mBAAmB,CAAC;AAE9G,OAAO,EAAE,+BAA+B,EAAE,+BAA+B,EAAE,MAAM,mBAAmB,CAAC;AACrG,OAAO,EAAE,qBAAqB,EAAE,UAAU,EAAE,gBAAgB,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAC;AAC7G,OAAO,EACL,0BAA0B,EAC1B,8BAA8B,GAC/B,MAAM,gDAAgD,CAAC;AACxD,OAAO,EAAE,MAAM,EAAE,MAAM,kCAAkC,CAAC;AAC1D,OAAO,EAAE,IAAI,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAC;AAC5D,OAAO,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,qBAAqB,CAAC;AAC/D,OAAO,EAAE,8BAA8B,EAAE,MAAM,kCAAkC,CAAC;AAClF,O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AChD,cAAc,EAAE;mBAChB,0BAA0B,CAAC,MAAM,GAAG,CAAC,CACzC,EACD,CAAC;YACD,iCAAiC,EAAE,CAAC;QACtC,CAAC;QAED,cAAc,EAAE,CAAC,KAAK,EAAE,WAAW,CAAC,CAAC;IACvC,CAAC,CAAC,CAAC;AACL,CAAC;AAED;;;;;;;GAOG;AACH,MAAM,UAAU,4CAA4C;IAC1D,IAAI,CAAC,SAAS,EAAE,CAAC;QACf,OAAO;IACT,CAAC;IACD,SAAS,GAAG,KAAK,CAAC;IAClB,IAAI,cAAc,EAAE,CAAC;QACnB,OAAO,CAAC,MAAM,CAAC,GAAG,cAAc,CAAC;QACjC,cAAc,GAAG,SAAS,CAAC;IAC7B,CAAC;SAAM,CAAC;QACN,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACzB,CAAC;AACH,CAAC","sourcesContent":["// Copyright 2026 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\n\nimport { options } from 'preact';\n\nimport {\n globalCommitContext,\n resetGlobalCommitContext,\n takeRemovedSubtreesForPostDispatchTeardown,\n} from './commit-context.js';\nimport { dropDelayedRunOnMainThreadReturnIds, packElementTemplateCommitUpdateEvent } from './commit-event.js';\nimport type { BackgroundElementTemplateInstance } from './instance.js';\nimport { clearElementTemplateRenderScope, resetElementTemplateRenderScope } from './render-scope.js';\nimport { globalPipelineOptions, markTiming, markTimingLegacy, setPipeline } from '../../core/performance.js';\nimport {\n delayedRunOnMainThreadData,\n takeDelayedRunOnMainThreadData,\n} from '../../core/thread-function-call/main-thread.js';\nimport { COMMIT } from '../../shared/render-constants.js';\nimport { hook, isEmptyObject } from '../../shared/utils.js';\nimport { profileEnd, profileStart } from '../debug/profile.js';\nimport { sendMTRefInitValueToMainThread } from '../main-thread-ref-init-value.js';\nimport { clearPendingRefs, flushPendingRefs, hasPendingRefs } from '../prop-adapters/ref.js';\n\nlet installed = false;\nlet previousCommit: typeof options[typeof COMMIT];\nlet hasHydrated = false;\nconst scheduledRemovedSubtreeCleanupTimers = /*#__PURE__*/ new Set<ReturnType<typeof setTimeout>>();\n\nexport function markElementTemplateHydrated(): void {\n hasHydrated = true;\n}\n\nexport function isElementTemplateHydrated(): boolean {\n return hasHydrated;\n}\n\nexport function resetElementTemplateCommitState(): void {\n hasHydrated = false;\n resetElementTemplateRenderScope();\n resetGlobalCommitContext();\n}\n\nexport function scheduleElementTemplateRemovedSubtreeCleanup(\n removedSubtreesAwaitingTeardown: BackgroundElementTemplateInstance[],\n): void {\n if (removedSubtreesAwaitingTeardown.length === 0) {\n return;\n }\n const timer = setTimeout(() => {\n scheduledRemovedSubtreeCleanupTimers.delete(timer);\n for (const root of removedSubtreesAwaitingTeardown) {\n root.releaseDetachedSubtreeFromManager();\n }\n }, 10000);\n scheduledRemovedSubtreeCleanupTimers.add(timer);\n}\n\nexport function cancelElementTemplateRemovedSubtreeCleanup(): void {\n for (const timer of scheduledRemovedSubtreeCleanupTimers) {\n clearTimeout(timer);\n }\n scheduledRemovedSubtreeCleanupTimers.clear();\n}\n\nfunction cleanupFailedElementTemplateCommit(\n removedSubtreesAwaitingTeardown: BackgroundElementTemplateInstance[],\n delayedRunOnMainThreadPayload: readonly { resolveId: number }[] | undefined,\n): void {\n dropDelayedRunOnMainThreadReturnIds(delayedRunOnMainThreadPayload);\n clearPendingRefs();\n resetGlobalCommitContext();\n setPipeline(undefined);\n scheduleElementTemplateRemovedSubtreeCleanup(removedSubtreesAwaitingTeardown);\n}\n\nfunction flushElementTemplateCommitChanges(): void {\n const hasNativeOps = globalCommitContext.ops.length > 0;\n const hasDelayedRunOnMainThread = delayedRunOnMainThreadData.length > 0;\n const hasUpdatePayload = hasNativeOps\n || !isEmptyObject(globalCommitContext.flushOptions)\n || hasDelayedRunOnMainThread;\n const removedSubtreesAwaitingTeardown = hasNativeOps ? takeRemovedSubtreesForPostDispatchTeardown() : [];\n if (hasUpdatePayload) {\n markTimingLegacy('updateDiffVdomEnd');\n markTiming('diffVdomEnd');\n\n if (__PROFILE__) {\n profileStart('ReactLynx::commitChanges');\n }\n markTiming('packChangesStart');\n if (globalPipelineOptions) {\n globalCommitContext.flushOptions.pipelineOptions = globalPipelineOptions;\n }\n markTiming('packChangesEnd');\n if (globalPipelineOptions) {\n setPipeline(undefined);\n }\n if (__PROFILE__) {\n profileEnd();\n }\n\n if (!hasNativeOps && !hasDelayedRunOnMainThread) {\n globalCommitContext.flushOptions.emptyPatch = true;\n }\n\n const delayedRunOnMainThreadPayload = hasDelayedRunOnMainThread\n ? takeDelayedRunOnMainThreadData()\n : undefined;\n\n let updateEvent: ReturnType<typeof packElementTemplateCommitUpdateEvent>;\n try {\n updateEvent = packElementTemplateCommitUpdateEvent(delayedRunOnMainThreadPayload);\n } catch (error) {\n cleanupFailedElementTemplateCommit(removedSubtreesAwaitingTeardown, delayedRunOnMainThreadPayload);\n throw error;\n }\n\n lynx.getCoreContext().dispatchEvent(updateEvent);\n }\n // When native ops exist, patch first so a newly attached ref observes the\n // committed native state. Ref-only commits still flush through this path.\n flushPendingRefs();\n resetGlobalCommitContext();\n // Match Snapshot's cleanup boundary: start the delayed teardown only\n // after the bridge dispatch, so background JS objects are not torn down\n // before main-thread detach observes the same commit.\n scheduleElementTemplateRemovedSubtreeCleanup(removedSubtreesAwaitingTeardown);\n}\n\nexport function installElementTemplateCommitHook(): void {\n if (installed) {\n return;\n }\n installed = true;\n // eslint-disable-next-line @typescript-eslint/unbound-method\n previousCommit = options[COMMIT];\n\n hook(options, COMMIT, (originalCommit, vnode, commitQueue) => {\n if (__BACKGROUND__) {\n clearElementTemplateRenderScope();\n try {\n sendMTRefInitValueToMainThread();\n } catch (error) {\n cleanupFailedElementTemplateCommit(\n takeRemovedSubtreesForPostDispatchTeardown(),\n takeDelayedRunOnMainThreadData(),\n );\n throw error;\n }\n }\n\n if (__BACKGROUND__ && !hasHydrated && hasPendingRefs()) {\n // User effects can run before ET hydrate arrives, so ordinary refs must be\n // attached on the background commit even though native UI ops are delayed.\n flushPendingRefs();\n } else if (\n __BACKGROUND__ && hasHydrated\n && (\n globalCommitContext.ops.length > 0\n || !isEmptyObject(globalCommitContext.flushOptions)\n || hasPendingRefs()\n || delayedRunOnMainThreadData.length > 0\n )\n ) {\n flushElementTemplateCommitChanges();\n }\n\n originalCommit?.(vnode, commitQueue);\n });\n}\n\n/**\n * Restores the commit option replaced by\n * {@link installElementTemplateCommitHook} so tests can clean up the global\n * Preact `options` object. Assumes the matching install was the most recent\n * commit-option replacement; the snapshot and ElementTemplate runtimes are\n * separate entrypoints and never install their commit hooks in the same\n * context.\n */\nexport function uninstallElementTemplateCommitHookForTesting(): void {\n if (!installed) {\n return;\n }\n installed = false;\n if (previousCommit) {\n options[COMMIT] = previousCommit;\n previousCommit = undefined;\n } else {\n delete options[COMMIT];\n }\n}\n"]}
|