@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.
Files changed (147) hide show
  1. package/CHANGELOG.md +22 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +9 -1
  4. package/refresh/.turbo/turbo-build.log +1 -1
  5. package/runtime/__test__/core/lynx/env.test.ts +146 -0
  6. package/runtime/__test__/element-template/imports/suspense-lazy.test.ts +47 -4
  7. package/runtime/__test__/element-template/native/index.test.ts +9 -9
  8. package/runtime/__test__/element-template/native/mts-destroy.test.ts +55 -2
  9. package/runtime/__test__/element-template/native/reload.test.ts +15 -5
  10. package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +1 -1
  11. package/runtime/__test__/element-template/runtime/hydration/hydration-listener.test.ts +1 -1
  12. package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +33 -0
  13. package/runtime/__test__/element-template/vitest.config.ts +1 -1
  14. package/runtime/__test__/snapshot/debug/profile.test.jsx +147 -26
  15. package/runtime/__test__/snapshot/lifecycle/reload-delayed-background.test.jsx +54 -0
  16. package/runtime/__test__/snapshot/lifecycle/transfer.mts.test.jsx +98 -6
  17. package/runtime/__test__/snapshot/lifecycle/transfer.test.jsx +214 -9
  18. package/runtime/__test__/snapshot/lynx/suspense.test.jsx +48 -0
  19. package/runtime/__test__/snapshot/utils/envManager.ts +2 -0
  20. package/runtime/__test__/worklet-runtime/delayWorkletEvent.test.js +21 -0
  21. package/runtime/__test__/worklet-runtime/lifecycle.test.js +297 -0
  22. package/runtime/__test__/worklet-runtime/profile.test.js +12 -6
  23. package/runtime/lazy/element-template-internal.js +1 -0
  24. package/runtime/lib/core/compat/componentIs.d.ts +1 -0
  25. package/runtime/lib/core/compat/componentIs.js +20 -0
  26. package/runtime/lib/core/compat/componentIs.js.map +1 -0
  27. package/runtime/lib/core/lynx/env.d.ts +1 -0
  28. package/runtime/lib/{element-template → core}/lynx/env.js +3 -2
  29. package/runtime/lib/core/lynx/env.js.map +1 -0
  30. package/runtime/lib/core/thread-function-call/main-thread.d.ts +1 -0
  31. package/runtime/lib/core/thread-function-call/main-thread.js +4 -1
  32. package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -1
  33. package/runtime/lib/element-template/background/commit-event.d.ts +6 -0
  34. package/runtime/lib/element-template/background/commit-event.js +36 -0
  35. package/runtime/lib/element-template/background/commit-event.js.map +1 -0
  36. package/runtime/lib/element-template/background/commit-hook.js +3 -23
  37. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  38. package/runtime/lib/element-template/background/destroy.js +1 -1
  39. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  40. package/runtime/lib/element-template/background/hydrate.js +1 -2
  41. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  42. package/runtime/lib/element-template/background/hydration-listener.js +5 -25
  43. package/runtime/lib/element-template/background/hydration-listener.js.map +1 -1
  44. package/runtime/lib/element-template/{runtime/template → background}/main-thread-function.js +4 -4
  45. package/runtime/lib/element-template/background/main-thread-function.js.map +1 -0
  46. package/runtime/lib/element-template/index.d.ts +1 -1
  47. package/runtime/lib/element-template/index.js +1 -1
  48. package/runtime/lib/element-template/index.js.map +1 -1
  49. package/runtime/lib/element-template/internal.js +3 -0
  50. package/runtime/lib/element-template/internal.js.map +1 -1
  51. package/runtime/lib/element-template/native/index.js +2 -2
  52. package/runtime/lib/element-template/native/index.js.map +1 -1
  53. package/runtime/lib/element-template/native/mts-destroy.d.ts +2 -1
  54. package/runtime/lib/element-template/native/mts-destroy.js +15 -4
  55. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  56. package/runtime/lib/element-template/native/reload-main-thread.js +2 -2
  57. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  58. package/runtime/lib/element-template/runtime/patch.js +4 -11
  59. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  60. package/runtime/lib/element-template/runtime/template/handle.d.ts +1 -1
  61. package/runtime/lib/element-template/runtime/template/handle.js +2 -2
  62. package/runtime/lib/element-template/runtime/template/handle.js.map +1 -1
  63. package/runtime/lib/element-template/runtime/template/main-thread-background-function.d.ts +1 -0
  64. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +7 -2
  65. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  66. package/runtime/lib/internal.js +4 -10
  67. package/runtime/lib/internal.js.map +1 -1
  68. package/runtime/lib/snapshot/debug/profileHooks.js +27 -62
  69. package/runtime/lib/snapshot/debug/profileHooks.js.map +1 -1
  70. package/runtime/lib/snapshot/lifecycle/reload.js +2 -2
  71. package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
  72. package/runtime/lib/snapshot/lynx/env.js +2 -32
  73. package/runtime/lib/snapshot/lynx/env.js.map +1 -1
  74. package/runtime/lib/snapshot/transfer.d.ts +6 -0
  75. package/runtime/lib/snapshot/transfer.js +154 -11
  76. package/runtime/lib/snapshot/transfer.js.map +1 -1
  77. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  78. package/runtime/lib/worklet-runtime/bindings/lifecycle.d.ts +19 -0
  79. package/runtime/lib/worklet-runtime/bindings/lifecycle.js +49 -0
  80. package/runtime/lib/worklet-runtime/bindings/lifecycle.js.map +1 -0
  81. package/runtime/lib/worklet-runtime/delayWorkletEvent.d.ts +1 -1
  82. package/runtime/lib/worklet-runtime/delayWorkletEvent.js +9 -1
  83. package/runtime/lib/worklet-runtime/delayWorkletEvent.js.map +1 -1
  84. package/runtime/lib/worklet-runtime/utils/profile.js +1 -3
  85. package/runtime/lib/worklet-runtime/utils/profile.js.map +1 -1
  86. package/runtime/src/core/compat/componentIs.ts +32 -0
  87. package/runtime/src/{element-template → core}/lynx/env.ts +6 -4
  88. package/runtime/src/core/thread-function-call/main-thread.ts +5 -1
  89. package/runtime/src/element-template/background/commit-event.ts +52 -0
  90. package/runtime/src/element-template/background/commit-hook.ts +4 -31
  91. package/runtime/src/element-template/background/destroy.ts +1 -1
  92. package/runtime/src/element-template/background/hydrate.ts +5 -3
  93. package/runtime/src/element-template/background/hydration-listener.ts +7 -32
  94. package/runtime/src/element-template/{runtime/template → background}/main-thread-function.ts +4 -7
  95. package/runtime/src/element-template/index.ts +1 -1
  96. package/runtime/src/element-template/internal.ts +4 -0
  97. package/runtime/src/element-template/native/index.ts +2 -2
  98. package/runtime/src/element-template/native/mts-destroy.ts +21 -4
  99. package/runtime/src/element-template/native/reload-main-thread.ts +2 -2
  100. package/runtime/src/element-template/runtime/patch.ts +20 -35
  101. package/runtime/src/element-template/runtime/template/handle.ts +2 -0
  102. package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -2
  103. package/runtime/src/internal.ts +4 -13
  104. package/runtime/src/snapshot/debug/profileHooks.ts +30 -91
  105. package/runtime/src/snapshot/lifecycle/reload.ts +2 -3
  106. package/runtime/src/snapshot/lynx/env.ts +3 -43
  107. package/runtime/src/snapshot/transfer.ts +169 -13
  108. package/runtime/src/worklet-runtime/bindings/lifecycle.ts +51 -0
  109. package/runtime/src/worklet-runtime/delayWorkletEvent.ts +10 -2
  110. package/runtime/src/worklet-runtime/utils/profile.ts +1 -3
  111. package/runtime/types/internal-preact.d.ts +2 -0
  112. package/runtime/vitest.config.ts +0 -1
  113. package/testing-library/.turbo/turbo-build.log +7 -7
  114. package/testing-library/src/__tests__/events.test.jsx +1 -1
  115. package/testing-library/src/fire-event.ts +1 -1
  116. package/transform/.turbo/turbo-build$colon$wasm.log +67 -66
  117. package/transform/__test__/fixture.spec.js +1 -1
  118. package/transform/cjs/browser.cjs +11 -0
  119. package/transform/crates/swc_plugin_compat/lib.rs +20 -2
  120. package/transform/crates/swc_plugin_compat/napi.rs +7 -0
  121. package/transform/crates/swc_plugin_dynamic_import/lib.rs +15 -4
  122. package/transform/crates/swc_plugin_element_template/attr_name.rs +0 -3
  123. package/transform/crates/swc_plugin_element_template/extractor.rs +0 -8
  124. package/transform/crates/swc_plugin_element_template/lib.rs +12 -1
  125. package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +35 -0
  126. package/transform/crates/swc_plugin_worklet/lib.rs +11 -7
  127. package/transform/dist/wasm.cjs +1 -1
  128. package/transform/rspack-napi/lib/index.js +1 -4
  129. package/transform/rspack-napi/lib/index.js.map +1 -1
  130. package/transform/rspack-napi/src/index.ts +1 -4
  131. package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
  132. package/transform/src/lib.rs +394 -27
  133. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +413 -105
  134. package/transform/swc-plugin-reactlynx/src/lib.rs +99 -22
  135. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  136. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +28 -359
  137. package/transform/swc-plugin-reactlynx-compat/swc_plugin_reactlynx_compat.wasm +0 -0
  138. package/runtime/__test__/element-template/lynx/env.test.ts +0 -242
  139. package/runtime/lib/element-template/lynx/env.d.ts +0 -1
  140. package/runtime/lib/element-template/lynx/env.js.map +0 -1
  141. package/runtime/lib/element-template/runtime/template/main-thread-function.js.map +0 -1
  142. package/runtime/lib/snapshot/compat/componentIs.d.ts +0 -4
  143. package/runtime/lib/snapshot/compat/componentIs.js +0 -20
  144. package/runtime/lib/snapshot/compat/componentIs.js.map +0 -1
  145. package/runtime/src/snapshot/compat/componentIs.ts +0 -30
  146. /package/runtime/__test__/element-template/runtime/{template → background}/main-thread-function.test.ts +0 -0
  147. /package/runtime/lib/element-template/{runtime/template → background}/main-thread-function.d.ts +0 -0
@@ -1,12 +1,28 @@
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 { createElement, render } from 'preact';
4
+ import { createElement, options, render } from 'preact';
5
5
  import type { ComponentType, ContainerNode, VNode } from 'preact';
6
6
 
7
+ import { addReloadScopedEventListener } from '../core/reload-scoped-event.js';
7
8
  import { getReloadVersion } from '../core/reload-version.js';
9
+ import { prepareMainThreadFunctionCtx } from '../core/thread-function-call/main-thread.js';
8
10
  import { __root, enableTransferMode, warnRootRenderOnTransferPage } from '../root.js';
9
- import { CHILDREN, PARENT } from '../shared/render-constants.js';
11
+ import {
12
+ CHILDREN,
13
+ COMMIT,
14
+ COMPONENT,
15
+ DIFFED,
16
+ HOOKS,
17
+ PARENT,
18
+ PENDING_EFFECTS,
19
+ RENDER_COMPONENT,
20
+ VALUE,
21
+ } from '../shared/render-constants.js';
22
+ import { isRendering } from './lifecycle/isRendering.js';
23
+ import { WorkletEvents } from '../worklet-runtime/bindings/events.js';
24
+ import type { RunWorkletCtxRetData } from '../worklet-runtime/bindings/events.js';
25
+ import type { Worklet } from '../worklet-runtime/bindings/types.js';
10
26
  import { genCommitTaskId, replaceCommitHook } from './lifecycle/patch/commit.js';
11
27
  import type { PatchList, PatchOptions } from './lifecycle/patch/commit.js';
12
28
  import { updateMainThread } from './lifecycle/patch/updateMainThread.js';
@@ -25,7 +41,7 @@ interface Scope {
25
41
  transferId: string;
26
42
  }
27
43
  type CreateTransferEvent = Scope & { componentName: string; initialProps: TransferProps };
28
- type SetTransferPropsEvent = Scope & { props: TransferProps };
44
+ type TransferPropsEvent = Scope & { props: TransferProps };
29
45
  type TransferFirstFrame = Scope & { snapshot: string };
30
46
  type TransferCommit = Scope & {
31
47
  version: number;
@@ -41,17 +57,23 @@ interface Pending {
41
57
  packet: TransferCommit;
42
58
  finish: () => void;
43
59
  }
60
+ export interface PendingMainThreadCall {
61
+ worklet: Worklet;
62
+ params: unknown[];
63
+ resolve: (value: any) => void;
64
+ }
44
65
  interface Instance {
45
66
  init: CreateTransferEvent;
46
67
  Component: ComponentType<TransferProps>;
47
68
  props: TransferProps;
48
69
  phase: 'waiting' | 'hydrating' | 'live' | 'disposed';
49
- /** Counts props updates; each one re-syncs the first frame and restarts `seq`. */
70
+ /** Counts reloads; each one re-syncs the first frame and restarts `seq`. */
50
71
  version: number;
51
72
  seq: number;
52
73
  main?: SnapshotInstance;
53
74
  element?: FiberElement;
54
75
  background?: BackgroundSnapshotInstance;
76
+ firstFrameWorklets?: PendingMainThreadCall[] | undefined;
55
77
  queue: Pending[];
56
78
  inFlight?: Pending | undefined;
57
79
  }
@@ -74,10 +96,14 @@ function initialize(): void {
74
96
  const frames = new Map<string, TransferFirstFrame>();
75
97
  const disposed = new Set<string>();
76
98
  const owners = new WeakMap<object, Instance>();
99
+ const workletReturns = new Map<number, { instance: Instance; resolve: (value: unknown) => void }>();
100
+ let nextWorkletReturnId = 0;
77
101
  const removers: (() => void)[] = [];
102
+ const patchedDelays = new WeakSet<object>();
78
103
  const pendingCreates: CreateTransferEvent[] = [];
79
104
  let pageReady = !mts;
80
105
  let active: Instance | undefined;
106
+ let activeVersion: number | undefined;
81
107
 
82
108
  function send(type: string, data: unknown) {
83
109
  const target = mts ? lynx.getJSContext() : lynx.getCoreContext();
@@ -94,13 +120,16 @@ function initialize(): void {
94
120
  target.addEventListener(type, callback);
95
121
  removers.push(() => target.removeEventListener(type, callback));
96
122
  }
97
- function withInstance<T>(instance: Instance, fn: () => T): T {
123
+ function withInstance<T>(instance: Instance | undefined, fn: () => T, version = instance?.version): T {
98
124
  const previous = active;
125
+ const previousVersion = activeVersion;
99
126
  active = instance;
127
+ activeVersion = version;
100
128
  try {
101
129
  return fn();
102
130
  } finally {
103
131
  active = previous;
132
+ activeVersion = previousVersion;
104
133
  }
105
134
  }
106
135
  function findOwner(vnode: VNode): Instance | undefined {
@@ -115,6 +144,7 @@ function initialize(): void {
115
144
  function renderBackground(instance: Instance) {
116
145
  const container = instance.background as unknown as ContainerNode;
117
146
  withInstance(instance, () => render(createElement(instance.Component, instance.props), container));
147
+ // Each `render` call creates a new root vnode.
118
148
  const vnode = (container as unknown as Record<string, object | undefined>)[CHILDREN];
119
149
  if (vnode) owners.set(vnode, instance);
120
150
  }
@@ -147,6 +177,10 @@ function initialize(): void {
147
177
  }
148
178
  /** Releases the commits of an instance that will never be applied. */
149
179
  function release(instance: Instance) {
180
+ instance.firstFrameWorklets = undefined;
181
+ for (const [id, call] of workletReturns) {
182
+ if (call.instance === instance) workletReturns.delete(id);
183
+ }
150
184
  instance.inFlight?.finish();
151
185
  instance.inFlight = undefined;
152
186
  instance.queue.forEach(item => item.finish());
@@ -186,10 +220,21 @@ function initialize(): void {
186
220
  instance.phase = 'hydrating';
187
221
  const serialized = JSON.parse(frame.snapshot) as SerializedSnapshotInstance;
188
222
  const snapshotPatch = withInstance(instance, () => hydrate(serialized, instance.background!));
223
+ const patchList: PatchList = { patchList: [{ id: genCommitTaskId(), snapshotPatch }] };
224
+ if (instance.firstFrameWorklets?.length) {
225
+ patchList.delayedRunOnMainThreadData = instance.firstFrameWorklets.map(({ worklet, params, resolve }) => {
226
+ const callWorklet = { ...worklet };
227
+ prepareMainThreadFunctionCtx(callWorklet);
228
+ const resolveId = --nextWorkletReturnId;
229
+ workletReturns.set(resolveId, { instance, resolve });
230
+ return { worklet: callWorklet, params, resolveId };
231
+ });
232
+ }
233
+ instance.firstFrameWorklets = undefined;
189
234
  enqueue(
190
235
  instance,
191
236
  'hydrate',
192
- { patchList: [{ id: genCommitTaskId(), snapshotPatch }] },
237
+ patchList,
193
238
  { reloadVersion: getReloadVersion(), isHydration: true },
194
239
  () => {
195
240
  if (instance.phase === 'hydrating') instance.phase = 'live';
@@ -223,6 +268,7 @@ function initialize(): void {
223
268
  renderMain(instance);
224
269
  } else {
225
270
  instance.background = new BackgroundSnapshotInstance('root');
271
+ instance.firstFrameWorklets = [];
226
272
  renderBackground(instance);
227
273
  const frame = frames.get(init.transferId);
228
274
  if (frame) {
@@ -231,20 +277,36 @@ function initialize(): void {
231
277
  }
232
278
  }
233
279
  }
234
- /** Re-renders both threads with the new props and re-syncs the first frame, like a template reload. */
235
- function setProps(update: SetTransferPropsEvent) {
280
+ /** Replaces the props of an instance; returns the instance to re-render, if it has been created. */
281
+ function replaceProps(update: TransferPropsEvent): Instance | undefined {
236
282
  const pending = pendingCreates.find(init => init.transferId === update.transferId);
237
283
  if (pending) {
238
284
  pending.initialProps = update.props;
239
- return;
285
+ return undefined;
240
286
  }
241
287
  const instance = instances.get(update.transferId);
288
+ if (instance) instance.props = update.props;
289
+ return instance;
290
+ }
291
+ /** Re-renders the background with the new props and keeps component state, like `updateGlobalProps`. */
292
+ function updateProps(update: TransferPropsEvent) {
293
+ const instance = replaceProps(update);
294
+ if (!instance) return;
295
+ // The main thread keeps its elements; background commits bring them up to date.
296
+ if (mts) {
297
+ send('__UpdateTransfer', update);
298
+ return;
299
+ }
300
+ renderBackground(instance);
301
+ }
302
+ /** Re-renders both threads with the new props and re-syncs the first frame, like a template reload. */
303
+ function reloadProps(update: TransferPropsEvent) {
304
+ const instance = replaceProps(update);
242
305
  if (!instance) return;
243
- instance.props = update.props;
244
306
  instance.version++;
245
307
  instance.seq = 0;
246
308
  if (mts) {
247
- send('__SetTransferProps', update);
309
+ send('__ReloadTransfer', update);
248
310
  renderMain(instance);
249
311
  return;
250
312
  }
@@ -252,6 +314,7 @@ function initialize(): void {
252
314
  instance.phase = 'waiting';
253
315
  release(instance);
254
316
  withInstance(instance, () => render(null, instance.background as unknown as ContainerNode));
317
+ instance.firstFrameWorklets = [];
255
318
  renderBackground(instance);
256
319
  }
257
320
  function dispose(scope: Scope, notify = true) {
@@ -284,6 +347,38 @@ function initialize(): void {
284
347
  }
285
348
  enableTransferMode();
286
349
 
350
+ /**
351
+ * The worklet runtime flushes its pre-hydration `runOnBackground` queue once per page, dropping calls whose
352
+ * first-screen handles are not bound yet. Each transfer instance hydrates separately, so a hydration only runs
353
+ * the calls already bound to the background and keeps the rest in place for the instances still waiting.
354
+ */
355
+ function keepDelayedBackgroundCalls() {
356
+ const delay = globalThis.lynxWorkletImpl?._runOnBackgroundDelayImpl;
357
+ if (!delay || patchedDelays.has(delay)) return;
358
+ patchedDelays.add(delay);
359
+ /* eslint-disable-next-line @typescript-eslint/unbound-method */
360
+ const run = delay.runDelayedBackgroundFunctions;
361
+ delay.runDelayedBackgroundFunctions = () => {
362
+ const queue = delay.delayedBackgroundFunctionArray;
363
+ if (queue.every(details => !details || details.jsFnHandle)) {
364
+ run();
365
+ // Later instances still queue their first-screen calls.
366
+ delay.hasHydrated = false;
367
+ return;
368
+ }
369
+ // Pending handles keep indices into the queue, so bound calls are cleared in place.
370
+ queue.forEach((details, index) => {
371
+ if (!details?.jsFnHandle) return;
372
+ queue[index] = undefined;
373
+ details.task(details.jsFnHandle._jsFnId!, details.jsFnHandle._execId!);
374
+ });
375
+ };
376
+ removers.push(() => {
377
+ delay.runDelayedBackgroundFunctions = run;
378
+ patchedDelays.delete(delay);
379
+ });
380
+ }
381
+
287
382
  if (mts) {
288
383
  listen<[Record<string, unknown>?]>(lynx.getEngine(), '__RenderPage', ([initData]) => {
289
384
  lynx.__initData = initData ?? {};
@@ -301,7 +396,8 @@ function initialize(): void {
301
396
  });
302
397
  const native = lynx.getNative();
303
398
  listen(native, '__CreateTransfer', create);
304
- listen(native, '__SetTransferProps', setProps);
399
+ listen(native, '__UpdateTransfer', updateProps);
400
+ listen(native, '__ReloadTransfer', reloadProps);
305
401
  listen<Scope>(native, '__DestroyTransfer', scope => dispose(scope));
306
402
  listen<TransferCommit>(lynx.getJSContext(), '__TransferCommit', packet => {
307
403
  const instance = instances.get(packet.transferId);
@@ -311,6 +407,7 @@ function initialize(): void {
311
407
  if (instance && instance.phase !== 'disposed') {
312
408
  try {
313
409
  if (packet.seq !== instance.seq + 1) throw new Error('Transfer commit sequence mismatch');
410
+ if (packet.patchOptions.isHydration) keepDelayedBackgroundCalls();
314
411
  updateMainThread({ data: JSON.stringify(packet.patchList), patchOptions: packet.patchOptions });
315
412
  instance.seq = packet.seq;
316
413
  status = 'applied';
@@ -338,12 +435,71 @@ function initialize(): void {
338
435
  }
339
436
  enqueue(instance, 'update', patchList, { ...patchOptions, reloadVersion: getReloadVersion() }, finish);
340
437
  });
438
+ let rendering: Instance | undefined;
439
+ /* eslint-disable @typescript-eslint/unbound-method */
440
+ const renderComponent = options[RENDER_COMPONENT];
441
+ const commit = options[COMMIT];
442
+ const diffed = options[DIFFED];
443
+ /* eslint-enable @typescript-eslint/unbound-method */
444
+ options[RENDER_COMPONENT] = (vnode, component) => {
445
+ rendering = findOwner(vnode);
446
+ renderComponent?.(vnode, component);
447
+ };
448
+ options[DIFFED] = vnode => {
449
+ const effects = vnode[COMPONENT]?.[HOOKS]?.[PENDING_EFFECTS] as
450
+ | { [VALUE]: () => void | (() => void) }[]
451
+ | undefined;
452
+ if (effects?.length) {
453
+ const instance = findOwner(vnode);
454
+ if (instance?.phase === 'waiting') {
455
+ const version = instance.version;
456
+ for (const effect of effects) {
457
+ const callback = effect[VALUE];
458
+ effect[VALUE] = () => {
459
+ const cleanup = withInstance(instance, () => callback.call(effect), version);
460
+ return typeof cleanup === 'function' ? () => withInstance(instance, cleanup, version) : cleanup;
461
+ };
462
+ }
463
+ }
464
+ }
465
+ diffed?.(vnode);
466
+ };
467
+ options[COMMIT] = (vnode, queue) => {
468
+ rendering = undefined;
469
+ withInstance(findOwner(vnode), () => commit?.(vnode, queue));
470
+ };
471
+ const scheduleWorklet = options.scheduleMainThreadWorklet;
472
+ function scheduleTransferWorklet(call: PendingMainThreadCall): boolean {
473
+ if (active && active.version !== activeVersion) return true;
474
+ const instance = active ?? (isRendering.value ? rendering : undefined);
475
+ if (!instance || (instance.phase !== 'waiting' && instance.phase !== 'disposed')) {
476
+ return false;
477
+ }
478
+ instance.firstFrameWorklets?.push(call);
479
+ return true;
480
+ }
481
+ options.scheduleMainThreadWorklet = scheduleTransferWorklet;
482
+ removers.push(() => {
483
+ Object.assign(options, {
484
+ [RENDER_COMPONENT]: renderComponent,
485
+ [COMMIT]: commit,
486
+ [DIFFED]: diffed,
487
+ scheduleMainThreadWorklet: scheduleWorklet,
488
+ });
489
+ });
341
490
  // Events of hydrated instances dispatch directly; earlier events have no handler and are dropped.
342
491
  enablePublishEvent();
343
492
  const core = lynx.getCoreContext();
493
+ removers.push(addReloadScopedEventListener<RunWorkletCtxRetData>(core, WorkletEvents.FunctionCallRet, data => {
494
+ const call = workletReturns.get(data.resolveId);
495
+ if (!call) return;
496
+ workletReturns.delete(data.resolveId);
497
+ call.resolve(data.returnValue);
498
+ }));
344
499
  listen(core, '__CreateTransfer', create);
345
500
  listen(core, '__TransferFirstFrame', acceptFrame);
346
- listen(core, '__SetTransferProps', setProps);
501
+ listen(core, '__UpdateTransfer', updateProps);
502
+ listen(core, '__ReloadTransfer', reloadProps);
347
503
  listen<Scope>(core, '__DestroyTransfer', scope => dispose(scope, false));
348
504
  listen<TransferCommitResult>(core, '__TransferCommitResult', result => {
349
505
  const instance = instances.get(result.transferId);
@@ -0,0 +1,51 @@
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
+ function clearPendingWorkletRuntimeState(impl: typeof globalThis.lynxWorkletImpl): void {
6
+ impl._runOnBackgroundDelayImpl?.clearDelayedBackgroundFunctions?.();
7
+ impl._refImpl?.clearFirstScreenWorkletRefMap?.();
8
+ impl._eventDelayImpl?.clearDelayedWorklets?.(true);
9
+ }
10
+
11
+ /**
12
+ * Retires the current page's worklet-runtime state for an in-place reload or
13
+ * entry re-evaluation on the same main-thread VM.
14
+ *
15
+ * Preserves VM-scoped registrations (`_workletMap`, MainThreadObject
16
+ * definitions, realized object brands, and positive-ID ref entries), while
17
+ * advancing the `runOnBackground` epoch, recreating the lifecycle manager for
18
+ * the next page, and dropping any aborted first-screen refs or queued legacy
19
+ * worklet events.
20
+ */
21
+ export function resetWorkletRuntimeForReload(): void {
22
+ const impl = globalThis.lynxWorkletImpl;
23
+ if (!impl) {
24
+ return;
25
+ }
26
+ const manager = impl._jsFunctionLifecycleManager;
27
+ if (manager?.recreate) {
28
+ impl._jsFunctionLifecycleManager = manager.recreate();
29
+ }
30
+ clearPendingWorkletRuntimeState(impl);
31
+ }
32
+
33
+ /**
34
+ * Disposes worklet-runtime state for terminal main-thread destruction
35
+ * (`__DestroyLifetime`).
36
+ *
37
+ * Unlike {@link resetWorkletRuntimeForReload}, this retires the lifecycle
38
+ * manager without allocating a replacement instance.
39
+ */
40
+ export function disposeWorkletRuntime(): void {
41
+ const impl = globalThis.lynxWorkletImpl;
42
+ if (!impl) {
43
+ return;
44
+ }
45
+ const manager = impl._jsFunctionLifecycleManager;
46
+ if (manager) {
47
+ manager.dispose?.();
48
+ delete impl._jsFunctionLifecycleManager;
49
+ }
50
+ clearPendingWorkletRuntimeState(impl);
51
+ }
@@ -8,10 +8,11 @@ interface EventDelayImpl {
8
8
  _delayedWorkletParamsMap: Map<string, ClosureValueType[][]>;
9
9
  delayExecUntilJsReady(hash: string, params: ClosureValueType[]): void;
10
10
  runDelayedWorklet(worklet: Worklet, element: ElementNode): void;
11
- clearDelayedWorklets(): void;
11
+ clearDelayedWorklets(cancelScheduled?: boolean): void;
12
12
  }
13
13
 
14
14
  function initEventDelay(): EventDelayImpl {
15
+ let generation = 0;
15
16
  const instance: EventDelayImpl = {
16
17
  _delayedWorkletParamsMap: new Map(),
17
18
  delayExecUntilJsReady(
@@ -36,11 +37,15 @@ function initEventDelay(): EventDelayImpl {
36
37
  if (paramsVec === undefined) {
37
38
  return;
38
39
  }
40
+ const scheduledGeneration = generation;
39
41
  const leftParamsVec: ClosureValueType[][] = [];
40
42
  paramsVec.forEach((params) => {
41
43
  const firstParam = params[0] as { currentTarget?: { elementRefptr?: ElementNode } } | undefined;
42
44
  if (firstParam?.currentTarget?.elementRefptr === element) {
43
45
  setTimeout(() => {
46
+ if (scheduledGeneration !== generation) {
47
+ return;
48
+ }
44
49
  profile('runDelayedWorklet', () => {
45
50
  runWorklet(worklet, params);
46
51
  });
@@ -55,7 +60,10 @@ function initEventDelay(): EventDelayImpl {
55
60
  );
56
61
  });
57
62
  },
58
- clearDelayedWorklets(): void {
63
+ clearDelayedWorklets(cancelScheduled?: boolean): void {
64
+ if (cancelScheduled) {
65
+ generation++;
66
+ }
59
67
  instance._delayedWorkletParamsMap.clear();
60
68
  },
61
69
  };
@@ -5,9 +5,7 @@ export function profile<Ret, Fn extends (...args: unknown[]) => Ret>(
5
5
  sliceName: string,
6
6
  f: Fn,
7
7
  ): Ret {
8
- /* v8 ignore next 9 */
9
- // TODO: change it to __PROFILE__
10
- if (__DEV__) {
8
+ if (__PROFILE__) {
11
9
  console.profile?.(sliceName);
12
10
  try {
13
11
  return f();
@@ -2,6 +2,7 @@
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
4
  import 'preact';
5
+ import type { PendingMainThreadCall } from '../src/snapshot/transfer.js';
5
6
 
6
7
  declare module 'preact' {
7
8
  interface Options {
@@ -22,6 +23,7 @@ declare module 'preact' {
22
23
  __?(vnode: VNode, parentDom: any): void;
23
24
  /** _hook */
24
25
  __h?: (component: Component, index: number, type: number) => void;
26
+ scheduleMainThreadWorklet?: (call: PendingMainThreadCall) => boolean;
25
27
  }
26
28
 
27
29
  interface PreactContext<T> {
@@ -160,7 +160,6 @@ export default defineConfig({
160
160
  'src/snapshot/lynx/calledByNative.ts',
161
161
  'src/snapshot/lynx/env.ts',
162
162
  'src/snapshot/lynx/tt.ts',
163
- 'src/snapshot/compat/componentIs.ts',
164
163
  'src/snapshot/snapshot/types.ts',
165
164
 
166
165
  '__test__/snapshot/page.test.jsx',
@@ -2,14 +2,14 @@ $ rslib
2
2
  Rslib v1.0.7
3
3
 
4
4
  info build started...
5
- ready built in 0.33s (esm2)
6
- start generating declaration files... (esm0)
7
5
  start generating declaration files... (esm2)
8
- ready built in 0.40s (esm1)
9
- ready built in 0.59s (esm0)
10
- info declaration files prepared with tsgo in 0.74 s (esm2)
11
- ready declaration files bundled successfully: dist/index.d.ts in 1.87 s (esm2)
12
- ready declaration files generated in 3.96 s (esm0)
6
+ start generating declaration files... (esm0)
7
+ ready built in 0.40s (esm2)
8
+ ready built in 0.49s (esm1)
9
+ ready built in 0.85s (esm0)
10
+ info declaration files prepared with tsgo in 0.88 s (esm2)
11
+ ready declaration files bundled successfully: dist/index.d.ts in 2.15 s (esm2)
12
+ ready declaration files generated in 4.14 s (esm0)
13
13
 
14
14
  File (esm0) Size
15
15
  dist/rslib-runtime-0.js 1.6 kB
@@ -308,7 +308,7 @@ describe('Event handler property semantics', () => {
308
308
  expect(parent).toHaveBeenCalledTimes(1);
309
309
  });
310
310
 
311
- // https://lynx.bytedance.net/next/zh/api/lynx-api/event/touch-event.html
311
+ // https://lynxjs.org/api/lynx-api/event/touch-event.html
312
312
  // Every TouchEvent-family event (BaseTouchEvent in @lynx-js/types)
313
313
  // bubbles in Lynx: touch{start,move,end,cancel}, longpress.
314
314
  it.each(['touchstart', 'touchmove', 'touchend', 'touchcancel', 'longpress'])(
@@ -42,7 +42,7 @@ export const fireEvent: any = (elemOrNodesRef, ...args) => {
42
42
 
43
43
  export const eventMap = {
44
44
  // LynxBindCatchEvent — TouchEvent family, bubble/capture per
45
- // https://lynx.bytedance.net/next/zh/api/lynx-api/event/touch-event.html
45
+ // https://lynxjs.org/api/lynx-api/event/touch-event.html
46
46
  tap: {
47
47
  defaultInit: { bubbles: true },
48
48
  },