@lynx-js/react 0.128.0 → 0.129.0

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 (174) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +1 -1
  4. package/refresh/.turbo/turbo-build.log +1 -1
  5. package/runtime/__test__/core/entry-re-eval.test.ts +161 -0
  6. package/runtime/__test__/core/reload-scoped-dual-thread-communication.test.ts +129 -0
  7. package/runtime/__test__/core/thread-function-call.test.ts +1 -0
  8. package/runtime/__test__/element-template/fixtures/background/render/_shared.tsx +2 -0
  9. package/runtime/__test__/element-template/native/index.test.ts +8 -0
  10. package/runtime/__test__/element-template/native/main-thread-ref-init-value.test.ts +100 -0
  11. package/runtime/__test__/element-template/native/reload-dual-thread-communication.test.ts +101 -0
  12. package/runtime/__test__/element-template/native/reload.test.ts +50 -1
  13. package/runtime/__test__/element-template/runtime/background/commit-hook.test.ts +104 -47
  14. package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +7 -1
  15. package/runtime/__test__/element-template/runtime/background/re-eval-reload.test.ts +69 -0
  16. package/runtime/__test__/element-template/runtime/hydration/hydration-listener.test.ts +31 -5
  17. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +17 -3
  18. package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +2 -0
  19. package/runtime/__test__/element-template/runtime/template/main-thread-function.test.ts +89 -2
  20. package/runtime/__test__/element-template/test-utils/debug/compiledFixtureModule.ts +5 -0
  21. package/runtime/__test__/snapshot/lifecycle/re-eval-reload.test.jsx +114 -0
  22. package/runtime/__test__/snapshot/lifecycle/reload-background-function.test.jsx +169 -0
  23. package/runtime/__test__/snapshot/lifecycle/reload-delayed-background.test.jsx +63 -0
  24. package/runtime/__test__/snapshot/lifecycle/reload-delayed-main-thread.test.jsx +103 -0
  25. package/runtime/__test__/snapshot/lifecycle.test.jsx +61 -1
  26. package/runtime/__test__/snapshot/lynx/lazy-effect-scheduling.test.jsx +105 -0
  27. package/runtime/__test__/snapshot/ssr.test.jsx +2 -2
  28. package/runtime/__test__/snapshot/worklet/runOnBackground.test.js +6 -11
  29. package/runtime/__test__/snapshot/worklet/runOnMainThread.test.jsx +1 -1
  30. package/runtime/__test__/snapshot/worklet/workletRef.test.jsx +24 -1
  31. package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +15 -1
  32. package/runtime/__test__/worklet-runtime/mainThreadObject.test.js +46 -0
  33. package/runtime/__test__/worklet-runtime/runOnMainThread.test.js +3 -3
  34. package/runtime/lib/core/background-function/exec-map.d.ts +2 -0
  35. package/runtime/lib/core/background-function/exec-map.js +7 -0
  36. package/runtime/lib/core/background-function/exec-map.js.map +1 -1
  37. package/runtime/lib/core/background-function/run-on-background.js +15 -16
  38. package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
  39. package/runtime/lib/core/card-lifetime.d.ts +5 -0
  40. package/runtime/lib/core/card-lifetime.js +17 -0
  41. package/runtime/lib/core/card-lifetime.js.map +1 -0
  42. package/runtime/lib/core/entry-reload.d.ts +7 -0
  43. package/runtime/lib/core/entry-reload.js +49 -0
  44. package/runtime/lib/core/entry-reload.js.map +1 -0
  45. package/runtime/lib/core/lynx/lazy-bundle.js +4 -1
  46. package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
  47. package/runtime/lib/core/main-thread-ref-id.js +4 -4
  48. package/runtime/lib/core/main-thread-ref-id.js.map +1 -1
  49. package/runtime/lib/core/reload-scoped-event.d.ts +5 -0
  50. package/runtime/lib/core/reload-scoped-event.js +26 -0
  51. package/runtime/lib/core/reload-scoped-event.js.map +1 -0
  52. package/runtime/lib/core/reload-version.js +4 -3
  53. package/runtime/lib/core/reload-version.js.map +1 -1
  54. package/runtime/lib/core/runtime-destroy.d.ts +2 -0
  55. package/runtime/lib/core/runtime-destroy.js +9 -0
  56. package/runtime/lib/core/runtime-destroy.js.map +1 -1
  57. package/runtime/lib/core/thread-function-call/main-thread.js +5 -4
  58. package/runtime/lib/core/thread-function-call/main-thread.js.map +1 -1
  59. package/runtime/lib/core/thread-function-call/return-value.js +6 -5
  60. package/runtime/lib/core/thread-function-call/return-value.js.map +1 -1
  61. package/runtime/lib/element-template/background/commit-hook.js +19 -16
  62. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  63. package/runtime/lib/element-template/background/hydration-listener.js +2 -6
  64. package/runtime/lib/element-template/background/hydration-listener.js.map +1 -1
  65. package/runtime/lib/element-template/main-thread-ref-init-value.d.ts +3 -0
  66. package/runtime/lib/element-template/main-thread-ref-init-value.js +38 -0
  67. package/runtime/lib/element-template/main-thread-ref-init-value.js.map +1 -0
  68. package/runtime/lib/element-template/native/index.js +2 -0
  69. package/runtime/lib/element-template/native/index.js.map +1 -1
  70. package/runtime/lib/element-template/native/mts-destroy.js +2 -0
  71. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  72. package/runtime/lib/element-template/native/patch-listener.js +1 -4
  73. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  74. package/runtime/lib/element-template/native/reload-background.d.ts +1 -1
  75. package/runtime/lib/element-template/native/reload-background.js +12 -1
  76. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  77. package/runtime/lib/element-template/native/reload-main-thread.d.ts +6 -0
  78. package/runtime/lib/element-template/native/reload-main-thread.js +18 -2
  79. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  80. package/runtime/lib/element-template/protocol/lifecycle-constant.d.ts +2 -1
  81. package/runtime/lib/element-template/protocol/lifecycle-constant.js +1 -0
  82. package/runtime/lib/element-template/protocol/lifecycle-constant.js.map +1 -1
  83. package/runtime/lib/element-template/protocol/types.d.ts +0 -2
  84. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  85. package/runtime/lib/shared/index-map.d.ts +4 -1
  86. package/runtime/lib/shared/index-map.js +5 -2
  87. package/runtime/lib/shared/index-map.js.map +1 -1
  88. package/runtime/lib/snapshot/lifecycle/destroy.js +6 -0
  89. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  90. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.d.ts +6 -2
  91. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.js +21 -18
  92. package/runtime/lib/snapshot/lifecycle/event/firstScreenSync.js.map +1 -1
  93. package/runtime/lib/snapshot/lifecycle/reload.d.ts +9 -2
  94. package/runtime/lib/snapshot/lifecycle/reload.js +29 -8
  95. package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
  96. package/runtime/lib/snapshot/list/list.js +3 -2
  97. package/runtime/lib/snapshot/list/list.js.map +1 -1
  98. package/runtime/lib/snapshot/lynx/calledByNative.js +3 -3
  99. package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
  100. package/runtime/lib/snapshot/snapshot/list.js +2 -1
  101. package/runtime/lib/snapshot/snapshot/list.js.map +1 -1
  102. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.d.ts +4 -2
  103. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.js +11 -11
  104. package/runtime/lib/snapshot/snapshot/snapshotInstanceManager.js.map +1 -1
  105. package/runtime/lib/snapshot/worklet/call/runOnMainThread.d.ts +1 -0
  106. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +6 -1
  107. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
  108. package/runtime/lib/snapshot/worklet/ctx.d.ts +1 -1
  109. package/runtime/lib/snapshot/worklet/ctx.js +6 -1
  110. package/runtime/lib/snapshot/worklet/ctx.js.map +1 -1
  111. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js +6 -0
  112. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js.map +1 -1
  113. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  114. package/runtime/lib/worklet-runtime/delayRunOnBackground.js +3 -1
  115. package/runtime/lib/worklet-runtime/delayRunOnBackground.js.map +1 -1
  116. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +2 -1
  117. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
  118. package/runtime/lib/worklet-runtime/listeners.js +2 -2
  119. package/runtime/lib/worklet-runtime/listeners.js.map +1 -1
  120. package/runtime/lib/worklet-runtime/mainThreadObject.js +3 -1
  121. package/runtime/lib/worklet-runtime/mainThreadObject.js.map +1 -1
  122. package/runtime/lib/worklet-runtime/runOnMainThread.js +4 -3
  123. package/runtime/lib/worklet-runtime/runOnMainThread.js.map +1 -1
  124. package/runtime/src/core/background-function/exec-map.ts +9 -0
  125. package/runtime/src/core/background-function/run-on-background.ts +14 -16
  126. package/runtime/src/core/card-lifetime.ts +18 -0
  127. package/runtime/src/core/entry-reload.ts +63 -0
  128. package/runtime/src/core/lynx/lazy-bundle.ts +4 -1
  129. package/runtime/src/core/main-thread-ref-id.ts +5 -4
  130. package/runtime/src/core/reload-scoped-event.ts +35 -0
  131. package/runtime/src/core/reload-version.ts +5 -3
  132. package/runtime/src/core/runtime-destroy.ts +11 -0
  133. package/runtime/src/core/thread-function-call/main-thread.ts +4 -4
  134. package/runtime/src/core/thread-function-call/return-value.ts +6 -5
  135. package/runtime/src/element-template/background/commit-hook.ts +20 -17
  136. package/runtime/src/element-template/background/hydration-listener.ts +2 -6
  137. package/runtime/src/element-template/main-thread-ref-init-value.ts +44 -0
  138. package/runtime/src/element-template/native/index.ts +2 -0
  139. package/runtime/src/element-template/native/mts-destroy.ts +3 -0
  140. package/runtime/src/element-template/native/patch-listener.ts +0 -5
  141. package/runtime/src/element-template/native/reload-background.ts +13 -1
  142. package/runtime/src/element-template/native/reload-main-thread.ts +30 -2
  143. package/runtime/src/element-template/protocol/lifecycle-constant.ts +1 -0
  144. package/runtime/src/element-template/protocol/types.ts +0 -2
  145. package/runtime/src/shared/index-map.ts +3 -2
  146. package/runtime/src/snapshot/lifecycle/destroy.ts +7 -0
  147. package/runtime/src/snapshot/lifecycle/event/firstScreenSync.ts +26 -19
  148. package/runtime/src/snapshot/lifecycle/reload.ts +47 -11
  149. package/runtime/src/snapshot/list/list.ts +9 -2
  150. package/runtime/src/snapshot/lynx/calledByNative.ts +3 -4
  151. package/runtime/src/snapshot/snapshot/list.ts +5 -1
  152. package/runtime/src/snapshot/snapshot/snapshotInstanceManager.ts +19 -12
  153. package/runtime/src/snapshot/worklet/call/runOnMainThread.ts +10 -1
  154. package/runtime/src/snapshot/worklet/ctx.ts +10 -1
  155. package/runtime/src/snapshot/worklet/ref/updateInitValue.ts +6 -0
  156. package/runtime/src/worklet-runtime/README.md +13 -0
  157. package/runtime/src/worklet-runtime/delayRunOnBackground.ts +3 -1
  158. package/runtime/src/worklet-runtime/jsFunctionLifecycle.ts +2 -1
  159. package/runtime/src/worklet-runtime/listeners.ts +4 -7
  160. package/runtime/src/worklet-runtime/mainThreadObject.ts +2 -1
  161. package/runtime/src/worklet-runtime/runOnMainThread.ts +4 -3
  162. package/runtime/types/types.d.ts +3 -1
  163. package/testing-library/.turbo/turbo-build.log +7 -7
  164. package/testing-library/src/__tests__/worklet.test.jsx +10 -5
  165. package/transform/.turbo/turbo-build$colon$wasm.log +339 -134
  166. package/transform/__test__/defines.spec.js +107 -0
  167. package/transform/crates/swc_plugin_worklet/definition_imports.rs +97 -0
  168. package/transform/crates/swc_plugin_worklet/lib.rs +7 -6
  169. package/transform/dist/wasm.cjs +1 -1
  170. package/transform/index.d.ts +3 -1
  171. package/transform/src/lib.rs +172 -11
  172. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +57 -55
  173. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  174. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +66 -274
@@ -30,6 +30,7 @@ import {
30
30
  import { dropFunctionCallReturnIds } from '../../core/thread-function-call/return-value.js';
31
31
  import { formatElementTemplateUpdateCommands, printElementTemplateTreeToString } from '../debug/alog.js';
32
32
  import { profileEnd, profileStart } from '../debug/profile.js';
33
+ import { sendMTRefInitValueToMainThread } from '../main-thread-ref-init-value.js';
33
34
  import { clearPendingEvents, flushPendingEvents } from '../prop-adapters/event.js';
34
35
  import { clearDelayedRefUiOps, clearPendingRefs, flushDelayedRefUiOps } from '../prop-adapters/ref.js';
35
36
  import { ElementTemplateLifecycleConstant } from '../protocol/lifecycle-constant.js';
@@ -107,8 +108,6 @@ export function installElementTemplateHydrationListener(): void {
107
108
  const delayedRunOnMainThreadPayload = hasDelayedRunOnMainThread
108
109
  ? takeDelayedRunOnMainThreadData()
109
110
  : undefined;
110
- const mainThreadRefInitValuePatch = takeMainThreadRefInitValuePatch();
111
- const hasMainThreadRefInitValuePatch = mainThreadRefInitValuePatch.length > 0;
112
111
  const pipelineOptions = globalPipelineOptions;
113
112
  if (pipelineOptions) {
114
113
  globalCommitContext.flushOptions.pipelineOptions = pipelineOptions;
@@ -122,6 +121,7 @@ export function installElementTemplateHydrationListener(): void {
122
121
  }
123
122
  markTiming('packChangesStart');
124
123
  try {
124
+ sendMTRefInitValueToMainThread();
125
125
  if (typeof __ALOG__ !== 'undefined' && __ALOG__) {
126
126
  console.alog?.(
127
127
  '[ReactLynxDebug] ElementTemplate hydrate update commands:\n'
@@ -132,7 +132,6 @@ export function installElementTemplateHydrationListener(): void {
132
132
  flowIds: globalCommitContext.flowIds,
133
133
  isHydration: true,
134
134
  delayedRunOnMainThreadDataCount: delayedRunOnMainThreadPayload?.length,
135
- mainThreadRefInitValuePatchCount: mainThreadRefInitValuePatch.length,
136
135
  },
137
136
  null,
138
137
  2,
@@ -146,9 +145,6 @@ export function installElementTemplateHydrationListener(): void {
146
145
  reloadVersion: getReloadVersion(),
147
146
  flowIds: globalCommitContext.flowIds,
148
147
  delayedRunOnMainThreadData: delayedRunOnMainThreadPayload,
149
- mainThreadRefInitValuePatch: hasMainThreadRefInitValuePatch
150
- ? mainThreadRefInitValuePatch
151
- : undefined,
152
148
  });
153
149
  } catch (error) {
154
150
  if (delayedRunOnMainThreadPayload) {
@@ -0,0 +1,44 @@
1
+ // Copyright 2026 The Lynx Authors. All rights reserved.
2
+ // Licensed under the Apache License Version 2.0 that can be found in the
3
+ // LICENSE file in the root directory of this source tree.
4
+
5
+ import { updateWorkletRefInitValueChanges } from '@lynx-js/react/worklet-runtime/bindings';
6
+ import type { MainThreadRefInitValuePatch } from '@lynx-js/react/worklet-runtime/bindings';
7
+
8
+ import { takeMainThreadRefInitValuePatch } from '../core/main-thread-ref-init-value.js';
9
+ import { ElementTemplateLifecycleConstant } from './protocol/lifecycle-constant.js';
10
+
11
+ let listener: ((event: RuntimeProxy.Event) => void) | undefined;
12
+
13
+ export function installElementTemplateRefInitValueListener(): void {
14
+ resetElementTemplateRefInitValueListener();
15
+ listener = (event) => {
16
+ const data = event.data as string;
17
+ const patch = JSON.parse(data) as MainThreadRefInitValuePatch;
18
+ if (typeof __ALOG__ !== 'undefined' && __ALOG__) {
19
+ console.alog?.('[ReactLynxDebug] ElementTemplate MTS updateMTRefInitValue:\n' + data);
20
+ }
21
+ // Ref initialization outlives UI patches and must not be filtered by reloadVersion.
22
+ updateWorkletRefInitValueChanges(patch);
23
+ };
24
+ lynx.getJSContext().addEventListener(ElementTemplateLifecycleConstant.updateMTRefInitValue, listener);
25
+ }
26
+
27
+ export function resetElementTemplateRefInitValueListener(): void {
28
+ if (listener) {
29
+ lynx.getJSContext().removeEventListener(ElementTemplateLifecycleConstant.updateMTRefInitValue, listener);
30
+ }
31
+ listener = undefined;
32
+ }
33
+
34
+ export function sendMTRefInitValueToMainThread(): void {
35
+ const patch = takeMainThreadRefInitValuePatch();
36
+ if (patch.length === 0) {
37
+ return;
38
+ }
39
+ const data = JSON.stringify(patch);
40
+ if (typeof __ALOG__ !== 'undefined' && __ALOG__) {
41
+ console.alog?.('[ReactLynxDebug] ElementTemplate BTS -> MTS sendMTRefInitValue:\n' + data);
42
+ }
43
+ lynx.getCoreContext().dispatchEvent({ type: ElementTemplateLifecycleConstant.updateMTRefInitValue, data });
44
+ }
@@ -25,6 +25,7 @@ import { initElementTemplatePAPICallAlog } from '../debug/elementPAPICall.js';
25
25
  import { initProfileHook } from '../debug/profile.js';
26
26
  import { setupLynxEnv } from '../lynx/env.js';
27
27
  import { initTimingAPI } from '../lynx/performance.js';
28
+ import { installElementTemplateRefInitValueListener } from '../main-thread-ref-init-value.js';
28
29
  import { publicComponentEvent, publishEvent, resetEventStateForRuntime } from '../prop-adapters/event.js';
29
30
  import { __root, setRoot } from '../runtime/page/root-instance.js';
30
31
 
@@ -57,6 +58,7 @@ function init(): void {
57
58
  if (__MAIN_THREAD__) {
58
59
  installMainThreadHooks();
59
60
  injectCalledByNative();
61
+ installElementTemplateRefInitValueListener();
60
62
  installElementTemplatePatchListener();
61
63
  installOnMtsDestruction();
62
64
  if (__PROFILE__) {
@@ -3,6 +3,7 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
5
  import { resetElementTemplatePatchListener } from './patch-listener.js';
6
+ import { resetElementTemplateRefInitValueListener } from '../main-thread-ref-init-value.js';
6
7
  import { destroyAllElementTemplateListStates } from '../runtime/list/list.js';
7
8
  import { resetElementTemplateMainThreadBackgroundFunctionRuntime } from '../runtime/template/main-thread-background-function.js';
8
9
  import { clearMainThreadDynamicAttrState } from '../runtime/template/main-thread-dynamic-attr-state.js';
@@ -33,6 +34,8 @@ export function destroyElementTemplateMainThreadRuntime(): void {
33
34
  didPatchListenerResetThrow = true;
34
35
  }
35
36
 
37
+ resetElementTemplateRefInitValueListener();
38
+
36
39
  // The registry is the main-thread strong-reference owner for ET refs. Clear it
37
40
  // even if listener reset fails so destroy does not leave removed pages retained.
38
41
  destroyAllElementTemplateListStates();
@@ -8,7 +8,6 @@ import {
8
8
  flushDelayedRunOnBackgroundFunctions,
9
9
  runRunOnMainThreadTask,
10
10
  setEomShouldFlushElementTree,
11
- updateWorkletRefInitValueChanges,
12
11
  } from '@lynx-js/react/worklet-runtime/bindings';
13
12
 
14
13
  import { markTiming, setPipeline } from '../../core/performance.js';
@@ -45,10 +44,6 @@ export function installElementTemplatePatchListener(): void {
45
44
  const payload = JSON.parse(event.data.payload) as ElementTemplateUpdateCommitContext;
46
45
  markTiming('parseChangesEnd');
47
46
 
48
- if (payload.mainThreadRefInitValuePatch?.length) {
49
- updateWorkletRefInitValueChanges(payload.mainThreadRefInitValuePatch);
50
- }
51
-
52
47
  if (
53
48
  typeof patchOptions.reloadVersion === 'number'
54
49
  && patchOptions.reloadVersion < getReloadVersion()
@@ -5,8 +5,10 @@
5
5
  import type { ComponentChild, ContainerNode } from 'preact';
6
6
  import { render } from 'preact';
7
7
 
8
+ import { canReEvalBackgroundEntry, reEvalBackgroundEntry } from '../../core/entry-reload.js';
8
9
  import { applyUpdatePageData } from '../../core/lynx-page-data.js';
9
10
  import { increaseReloadVersion } from '../../core/reload-version.js';
11
+ import { resetElementTemplateCommitState } from '../background/commit-hook.js';
10
12
  import { destroyElementTemplateBackgroundRuntime } from '../background/destroy.js';
11
13
  import { setupBackgroundElementTemplateDocument } from '../background/document.js';
12
14
  import { installElementTemplateHydrationListener } from '../background/hydration-listener.js';
@@ -15,7 +17,17 @@ import { profileEnd, profileStart } from '../debug/profile.js';
15
17
  import { resetEventStateForRuntime } from '../prop-adapters/event.js';
16
18
  import { __root, setRoot } from '../runtime/page/root-instance.js';
17
19
 
18
- export function reloadBackground(updateData: unknown): void {
20
+ export function reloadBackground(updateData: unknown, options?: Record<string, unknown>): void {
21
+ if (canReEvalBackgroundEntry()) {
22
+ reEvalBackgroundEntry(updateData as Record<string, unknown>, options, () => {
23
+ // The unmount of the old tree must not reach the new entry.
24
+ resetElementTemplateCommitState();
25
+ // A queued `updateGlobalProps` callback must not render it again.
26
+ __root.__jsx = undefined;
27
+ });
28
+ return;
29
+ }
30
+
19
31
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
20
32
  profileStart('ReactLynx::reloadBackground');
21
33
  }
@@ -2,13 +2,19 @@
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
 
5
+ import { onMtsDestruction } from './mts-destroy.js';
6
+ import {
7
+ canReEvalMainThreadEntry,
8
+ installMainThreadReloadCompleter,
9
+ reEvalMainThreadEntry,
10
+ } from '../../core/entry-reload.js';
5
11
  import { applyUpdatePageData } from '../../core/lynx-page-data.js';
6
12
  import { increaseReloadVersion } from '../../core/reload-version.js';
7
13
  import { profileEnd, profileStart } from '../debug/profile.js';
8
14
  import { ELEMENT_TEMPLATE_PAGE_ROOT_SLOT_INDEX } from '../protocol/page.js';
9
15
  import type { SerializedEtNode, SerializedPageRoot } from '../protocol/types.js';
10
16
  import { destroyAllElementTemplateListStates } from '../runtime/list/list.js';
11
- import { __page } from '../runtime/page/page.js';
17
+ import { __page, setupPage } from '../runtime/page/page.js';
12
18
  import { __root, setRoot } from '../runtime/page/root-instance.js';
13
19
  import { renderMainThread } from '../runtime/render/render-main-thread.js';
14
20
  import { resetTemplateId } from '../runtime/template/handle.js';
@@ -25,7 +31,6 @@ export function reloadMainThread(data: unknown, options: UpdatePageOption): void
25
31
  }
26
32
 
27
33
  try {
28
- increaseReloadVersion();
29
34
  applyUpdatePageData(data, options);
30
35
 
31
36
  destroyAllElementTemplateListStates();
@@ -42,11 +47,21 @@ export function reloadMainThread(data: unknown, options: UpdatePageOption): void
42
47
  }
43
48
  deleteMainThreadDynamicAttrStateForSubtree(removedHandleIds);
44
49
  }
50
+ if (canReEvalMainThreadEntry()) {
51
+ onMtsDestruction();
52
+ increaseReloadVersion();
53
+ reEvalMainThreadEntry<MainThreadReloadHandoff>({ page: __page, options });
54
+ return;
55
+ }
45
56
  elementTemplateRegistry.clear();
46
57
  clearMainThreadDynamicAttrState();
47
58
  resetElementTemplateMainThreadBackgroundFunctionRuntime();
48
59
  resetTemplateId();
49
60
 
61
+ // Ref cleanup still belongs to the old page. Advance only before rendering
62
+ // the new tree so calls queued by old ref/unmount callbacks remain stale.
63
+ increaseReloadVersion();
64
+
50
65
  const oldRoot = __root;
51
66
  setRoot({ __jsx: oldRoot.__jsx });
52
67
  renderMainThread();
@@ -59,6 +74,19 @@ export function reloadMainThread(data: unknown, options: UpdatePageOption): void
59
74
  }
60
75
  }
61
76
 
77
+ interface MainThreadReloadHandoff {
78
+ page: ElementTemplateHandle;
79
+ options: UpdatePageOption;
80
+ }
81
+
82
+ export function completeMainThreadReload({ page, options }: MainThreadReloadHandoff): void {
83
+ setupPage(page);
84
+ renderMainThread();
85
+ __FlushElementTree(undefined, options);
86
+ }
87
+
88
+ installMainThreadReloadCompleter(completeMainThreadReload);
89
+
62
90
  function collectSerializedSubtreeHandleIds(
63
91
  node: SerializedEtNode,
64
92
  handleIds: number[],
@@ -5,4 +5,5 @@
5
5
  export const enum ElementTemplateLifecycleConstant {
6
6
  hydrate = 'rLynxElementTemplateHydrate',
7
7
  update = 'rLynxElementTemplateUpdate',
8
+ updateMTRefInitValue = 'rLynxElementTemplateRefInitValue',
8
9
  }
@@ -6,7 +6,6 @@ import type { RunWorkletCtxData } from '@lynx-js/react/worklet-runtime/bindings'
6
6
 
7
7
  import { ElementTemplateUpdateOps } from './opcodes.js';
8
8
  import { ELEMENT_TEMPLATE_PAGE_HANDLE_ID, ELEMENT_TEMPLATE_PAGE_TYPE } from './page.js';
9
- import type { MainThreadRefInitValuePatch } from '../../worklet-runtime/bindings/types.js';
10
9
 
11
10
  export type SerializableValue =
12
11
  | string
@@ -225,5 +224,4 @@ export interface ElementTemplateUpdateCommitContext {
225
224
  isHydration?: boolean | undefined;
226
225
  reloadVersion?: number | undefined;
227
226
  delayedRunOnMainThreadData?: RunWorkletCtxData[] | undefined;
228
- mainThreadRefInitValuePatch?: MainThreadRefInitValuePatch | undefined;
229
227
  }
@@ -3,11 +3,12 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
5
  export class IndexMap<T> {
6
- protected lastIndex = 0;
7
6
  protected indexMap: Map<number, T> = new Map<number, T>();
8
7
 
8
+ constructor(private counter: { lastIndex: number } = { lastIndex: 0 }) {}
9
+
9
10
  public add(value: T): number {
10
- const index = ++this.lastIndex;
11
+ const index = ++this.counter.lastIndex;
11
12
  this.indexMap.set(index, value);
12
13
  return index;
13
14
  }
@@ -7,14 +7,19 @@ import { __root } from '../../root.js';
7
7
  import { delayedEvents } from './event/delayEvents.js';
8
8
  import { delayedLifecycleEvents } from './event/delayLifecycleEvents.js';
9
9
  import { globalCommitTaskMap } from './patch/commit.js';
10
+ import { deinitGlobalSnapshotPatch } from './patch/snapshotPatch.js';
10
11
  import { profileEnd, profileStart } from '../../shared/profile.js';
11
12
  import { withSyncEffectFlush } from '../../utils.js';
13
+ import { resetSnapshotMainThreadFunctionRuntime } from '../worklet/call/runOnMainThread.js';
14
+ import { resetSnapshotBackgroundFunctionRuntime } from '../worklet/ctx.js';
12
15
 
13
16
  function destroyBackground(): void {
14
17
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
15
18
  profileStart('ReactLynx::destroyBackground');
16
19
  }
17
20
 
21
+ deinitGlobalSnapshotPatch();
22
+
18
23
  withSyncEffectFlush(() => {
19
24
  // eslint-disable-next-line @typescript-eslint/no-unsafe-argument
20
25
  render(null, __root as any);
@@ -30,6 +35,8 @@ function destroyBackground(): void {
30
35
  if (delayedEvents) {
31
36
  delayedEvents.length = 0;
32
37
  }
38
+ resetSnapshotBackgroundFunctionRuntime();
39
+ resetSnapshotMainThreadFunctionRuntime();
33
40
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
34
41
  profileEnd();
35
42
  }
@@ -1,6 +1,7 @@
1
1
  // Copyright 2025 The Lynx Authors. All rights reserved.
2
2
  // Licensed under the Apache License Version 2.0 that can be found in the
3
3
  // LICENSE file in the root directory of this source tree.
4
+ import { cardLifetime } from '../../../core/card-lifetime.js';
4
5
  import { isProcessingDefaultData } from '../../../core/lynx-data-processors.js';
5
6
  import { __root } from '../../../root.js';
6
7
  import { profileEnd, profileStart } from '../../../shared/profile.js';
@@ -9,13 +10,20 @@ import { LifecycleConstant } from '../../lifecycle/constant.js';
9
10
  // Initialized once at module load (`renderPage` runs once per runtime and does
10
11
  // not reset), so a ready mark set before `renderPage` (e.g. in `defaultDataProcessor`)
11
12
  // survives. In `'manual'` mode the first screen syncs only after both are true.
12
- let isFirstScreenSynced = false;
13
- let firstScreenEventIdSwap: Record<string | number, number> = {};
14
- let isMarkedFirstScreenSyncReady = false;
15
- let isFirstScreenTreeReady = false;
13
+ const firstScreenSync: {
14
+ isSynced: boolean;
15
+ eventIdSwap: Record<string | number, number>;
16
+ isMarkedReady: boolean;
17
+ isTreeReady: boolean;
18
+ } = /*#__PURE__*/ cardLifetime('firstScreenSync', () => ({
19
+ isSynced: false,
20
+ eventIdSwap: {},
21
+ isMarkedReady: false,
22
+ isTreeReady: false,
23
+ }));
16
24
 
17
25
  function syncFirstScreen(): void {
18
- isFirstScreenSynced = true;
26
+ firstScreenSync.isSynced = true;
19
27
 
20
28
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
21
29
  profileStart('ReactLynx::serializeRoot');
@@ -31,13 +39,13 @@ function syncFirstScreen(): void {
31
39
  LifecycleConstant.firstScreen, /* FIRST_SCREEN */
32
40
  {
33
41
  root,
34
- firstScreenEventIdSwap,
42
+ firstScreenEventIdSwap: firstScreenSync.eventIdSwap,
35
43
  },
36
44
  ]);
37
45
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
38
46
  profileEnd();
39
47
  }
40
- firstScreenEventIdSwap = {};
48
+ firstScreenSync.eventIdSwap = {};
41
49
  }
42
50
 
43
51
  // ready signal: business/framework allows the handover. Syncs if the tree is ready.
@@ -45,10 +53,10 @@ function onFirstScreenSyncReady(): void {
45
53
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
46
54
  profileStart('ReactLynx::onFirstScreenSyncReady');
47
55
  }
48
- isMarkedFirstScreenSyncReady = true;
56
+ firstScreenSync.isMarkedReady = true;
49
57
  // A mark made inside `defaultDataProcessor` (a native call before the upcoming render)
50
58
  // is recorded but not synced now; `onFirstScreenTreeReady` honors it once render ends.
51
- if (isFirstScreenTreeReady && !isProcessingDefaultData() && !isFirstScreenSynced) {
59
+ if (firstScreenSync.isTreeReady && !isProcessingDefaultData() && !firstScreenSync.isSynced) {
52
60
  syncFirstScreen();
53
61
  }
54
62
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
@@ -58,8 +66,8 @@ function onFirstScreenSyncReady(): void {
58
66
 
59
67
  // tree built (`renderPage` / `updatePage` done). Syncs if the ready signal already came.
60
68
  function onFirstScreenTreeReady(): void {
61
- isFirstScreenTreeReady = true;
62
- if (isMarkedFirstScreenSyncReady && !isFirstScreenSynced) {
69
+ firstScreenSync.isTreeReady = true;
70
+ if (firstScreenSync.isMarkedReady && !firstScreenSync.isSynced) {
63
71
  syncFirstScreen();
64
72
  }
65
73
  }
@@ -67,19 +75,19 @@ function onFirstScreenTreeReady(): void {
67
75
  // the first-screen tree is being (re-)rendered, a mark during this period
68
76
  // must not sync until the next `onFirstScreenTreeReady`
69
77
  function resetFirstScreenTreeReady(): void {
70
- isFirstScreenTreeReady = false;
78
+ firstScreenSync.isTreeReady = false;
71
79
  }
72
80
 
73
81
  function clearFirstScreenEventIdSwap(): void {
74
- firstScreenEventIdSwap = {};
82
+ firstScreenSync.eventIdSwap = {};
75
83
  }
76
84
 
77
85
  // Full reset of all first-screen state. Used by SSR hydration to re-initialize.
78
86
  function resetFirstScreenSyncState(): void {
79
- isFirstScreenSynced = false;
80
- firstScreenEventIdSwap = {};
81
- isMarkedFirstScreenSyncReady = false;
82
- isFirstScreenTreeReady = false;
87
+ firstScreenSync.isSynced = false;
88
+ firstScreenSync.eventIdSwap = {};
89
+ firstScreenSync.isMarkedReady = false;
90
+ firstScreenSync.isTreeReady = false;
83
91
  }
84
92
 
85
93
  /**
@@ -87,8 +95,7 @@ function resetFirstScreenSyncState(): void {
87
95
  */
88
96
  export {
89
97
  syncFirstScreen,
90
- isFirstScreenSynced,
91
- firstScreenEventIdSwap,
98
+ firstScreenSync,
92
99
  clearFirstScreenEventIdSwap,
93
100
  resetFirstScreenSyncState,
94
101
  onFirstScreenSyncReady,
@@ -9,20 +9,29 @@
9
9
 
10
10
  import { render } from 'preact';
11
11
 
12
+ import { clearDelayedRunOnBackgroundFunctions } from '@lynx-js/react/worklet-runtime/bindings';
13
+
12
14
  import { destroyBackground } from './destroy.js';
13
15
  import { renderMainThread } from './render.js';
16
+ import {
17
+ canReEvalBackgroundEntry,
18
+ canReEvalMainThreadEntry,
19
+ installMainThreadReloadCompleter,
20
+ reEvalBackgroundEntry,
21
+ reEvalMainThreadEntry,
22
+ } from '../../core/entry-reload.js';
14
23
  import { increaseReloadVersion } from '../../core/reload-version.js';
24
+ import { resetFunctionCallReturnListener } from '../../core/thread-function-call/return-value.js';
15
25
  import { __root, setRoot } from '../../root.js';
16
26
  import { profileEnd, profileStart } from '../../shared/profile.js';
17
27
  import { isEmptyObject } from '../../utils.js';
18
28
  import { LifecycleConstant } from '../lifecycle/constant.js';
19
29
  import { __pendingListUpdates } from '../list/pendingListUpdates.js';
20
30
  import { hydrate } from '../renderToOpcodes/hydrate.js';
21
- import { __page } from '../snapshot/definition.js';
31
+ import { __page, setupPage } from '../snapshot/definition.js';
22
32
  import { SnapshotInstance, snapshotInstanceManager } from '../snapshot/snapshot.js';
23
33
  import { applyRefQueue } from '../snapshot/workletRef.js';
24
- import { clearFirstScreenEventIdSwap, isFirstScreenSynced } from './event/firstScreenSync.js';
25
- import { deinitGlobalSnapshotPatch } from './patch/snapshotPatch.js';
34
+ import { clearFirstScreenEventIdSwap, firstScreenSync } from './event/firstScreenSync.js';
26
35
  import { shouldDelayUiOps } from './ref/delay.js';
27
36
 
28
37
  function reloadMainThread(data: unknown, options: UpdatePageOption): void {
@@ -30,6 +39,8 @@ function reloadMainThread(data: unknown, options: UpdatePageOption): void {
30
39
  profileStart('ReactLynx::reloadMainThread');
31
40
  }
32
41
 
42
+ clearDelayedRunOnBackgroundFunctions();
43
+ resetFunctionCallReturnListener();
33
44
  increaseReloadVersion();
34
45
 
35
46
  if (typeof data == 'object' && data !== null && !isEmptyObject(data)) {
@@ -41,10 +52,31 @@ function reloadMainThread(data: unknown, options: UpdatePageOption): void {
41
52
  clearFirstScreenEventIdSwap();
42
53
 
43
54
  const oldRoot = __root;
55
+ if (canReEvalMainThreadEntry()) {
56
+ reEvalMainThreadEntry<MainThreadReloadHandoff>({ oldRoot: oldRoot as SnapshotInstance, page: __page, options });
57
+ return;
58
+ }
44
59
  setRoot(new SnapshotInstance('root'));
45
60
  __root.__jsx = oldRoot.__jsx;
61
+ finishMainThreadReload(oldRoot as SnapshotInstance, options);
62
+ }
63
+
64
+ interface MainThreadReloadHandoff {
65
+ oldRoot: SnapshotInstance;
66
+ page: FiberElement;
67
+ options: UpdatePageOption;
68
+ }
69
+
70
+ function completeMainThreadReload({ oldRoot, page, options }: MainThreadReloadHandoff): void {
71
+ setupPage(page);
72
+ finishMainThreadReload(oldRoot, options);
73
+ }
74
+
75
+ installMainThreadReloadCompleter(completeMainThreadReload);
76
+
77
+ function finishMainThreadReload(oldRoot: SnapshotInstance, options: UpdatePageOption): void {
46
78
  renderMainThread();
47
- hydrate(oldRoot as SnapshotInstance, __root as SnapshotInstance, {
79
+ hydrate(oldRoot, __root as SnapshotInstance, {
48
80
  skipUnRef: true,
49
81
  });
50
82
 
@@ -56,7 +88,7 @@ function reloadMainThread(data: unknown, options: UpdatePageOption): void {
56
88
  lepusng_gc();
57
89
  }
58
90
 
59
- if (isFirstScreenSynced) {
91
+ if (firstScreenSync.isSynced) {
60
92
  __OnLifecycleEvent([
61
93
  LifecycleConstant.firstScreen, /* FIRST_SCREEN */
62
94
  {
@@ -70,18 +102,22 @@ function reloadMainThread(data: unknown, options: UpdatePageOption): void {
70
102
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
71
103
  profileEnd();
72
104
  }
73
- return;
74
105
  }
75
106
 
76
- function reloadBackground(updateData: Record<string, any>): void {
107
+ function reloadBackground(updateData: Record<string, any>, options?: Record<string, unknown>): void {
108
+ if (canReEvalBackgroundEntry()) {
109
+ reEvalBackgroundEntry(updateData, options, () => {
110
+ // A queued `updateGlobalProps` callback must not render it again.
111
+ __root.__jsx = undefined;
112
+ });
113
+ return;
114
+ }
115
+
77
116
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
78
117
  profileStart('ReactLynx::reloadBackground');
79
118
  }
80
119
 
81
- deinitGlobalSnapshotPatch();
82
-
83
120
  destroyBackground();
84
-
85
121
  increaseReloadVersion();
86
122
 
87
123
  // COW when modify `lynx.__initData` to make sure Provider & Consumer works
@@ -96,4 +132,4 @@ function reloadBackground(updateData: Record<string, any>): void {
96
132
  }
97
133
  }
98
134
 
99
- export { reloadBackground, reloadMainThread };
135
+ export { completeMainThreadReload, reloadBackground, reloadMainThread };
@@ -1,13 +1,20 @@
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 { cardLifetime } from '../../core/card-lifetime.js';
4
5
  import { maybePromise } from '../../utils.js';
5
6
  import { LifecycleConstant } from '../lifecycle/constant.js';
6
7
  import type { SnapshotInstance } from '../snapshot/snapshot.js';
7
8
  import { applyRefQueue } from '../snapshot/workletRef.js';
8
9
 
9
- export const gSignMap: Record<number, Map<number, SnapshotInstance>> = {};
10
- export const gRecycleMap: Record<number, Map<string, Map<number, SnapshotInstance>>> = {};
10
+ export const gSignMap: Record<number, Map<number, SnapshotInstance>> = /*#__PURE__*/ cardLifetime(
11
+ 'listSignMap',
12
+ () => ({}),
13
+ );
14
+ export const gRecycleMap: Record<number, Map<string, Map<number, SnapshotInstance>>> = /*#__PURE__*/ cardLifetime(
15
+ 'listRecycleMap',
16
+ () => ({}),
17
+ );
11
18
  const gParentWeakMap: WeakMap<SnapshotInstance, unknown> = /*#__PURE__*/ new WeakMap();
12
19
  const resolvedPromise = /* @__PURE__ */ Promise.resolve();
13
20
 
@@ -6,8 +6,7 @@ import { markTiming, setPipeline } from '../../core/performance.js';
6
6
  import { __root, setRoot } from '../../root.js';
7
7
  import { LifecycleConstant } from '../lifecycle/constant.js';
8
8
  import {
9
- firstScreenEventIdSwap,
10
- isFirstScreenSynced,
9
+ firstScreenSync,
11
10
  onFirstScreenSyncReady,
12
11
  onFirstScreenTreeReady,
13
12
  resetFirstScreenSyncState,
@@ -126,7 +125,7 @@ function updatePage(data: Record<string, unknown> | undefined, options?: UpdateP
126
125
  applyUpdatePageData(data, options);
127
126
 
128
127
  const flushOptions = options ?? {};
129
- if (!isFirstScreenSynced) {
128
+ if (!firstScreenSync.isSynced) {
130
129
  if (__FIRST_SCREEN_SYNC_TIMING__ === 'manual') {
131
130
  // a `markFirstScreenSyncReady` call during the re-render below must not
132
131
  // sync the half-rendered tree; it is deferred to `onFirstScreenTreeReady`
@@ -147,7 +146,7 @@ function updatePage(data: Record<string, unknown> | undefined, options?: UpdateP
147
146
  hydrate(
148
147
  oldRoot as SnapshotInstance,
149
148
  __root as SnapshotInstance,
150
- { skipUnRef: true, swap: firstScreenEventIdSwap },
149
+ { skipUnRef: true, swap: firstScreenSync.eventIdSwap },
151
150
  );
152
151
 
153
152
  // always call this before `__FlushElementTree`
@@ -2,11 +2,15 @@
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
 
5
+ import { cardLifetime } from '../../core/card-lifetime.js';
5
6
  import { componentAtIndexFactory, enqueueComponentFactory, gRecycleMap, gSignMap } from '../list/list.js';
6
7
  import { hydrate } from '../renderToOpcodes/hydrate.js';
7
8
  import type { SnapshotInstance } from '../snapshot/snapshot.js';
8
9
 
9
- const destroyLifetimeHandlerMap = /*#__PURE__*/ new Map<number, () => void>();
10
+ const destroyLifetimeHandlerMap = /*#__PURE__*/ cardLifetime(
11
+ 'listDestroyLifetimeHandlerMap',
12
+ () => new Map<number, () => void>(),
13
+ );
10
14
 
11
15
  export function snapshotCreateList(
12
16
  pageId: number,
@@ -3,20 +3,27 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
5
  import type { SnapshotInstance } from './snapshot.js';
6
+ import { cardLifetime } from '../../core/card-lifetime.js';
6
7
  import { clearSnapshotVNodeSource } from '../debug/vnodeSource.js';
7
8
 
8
- export const snapshotInstanceManager: {
9
+ interface SnapshotInstanceManager {
9
10
  nextId: number;
10
11
  values: Map<number, SnapshotInstance>;
11
12
  clear(): void;
12
- } = {
13
- nextId: 0,
14
- values: /* @__PURE__ */ new Map<number, SnapshotInstance>(),
15
- clear() {
16
- // not resetting `nextId` to prevent id collision
17
- this.values.clear();
18
- if (__DEV__) {
19
- clearSnapshotVNodeSource();
20
- }
21
- },
22
- };
13
+ }
14
+
15
+ function clear(this: SnapshotInstanceManager): void {
16
+ // not resetting `nextId` to prevent id collision
17
+ this.values.clear();
18
+ if (__DEV__) {
19
+ clearSnapshotVNodeSource();
20
+ }
21
+ }
22
+
23
+ export const snapshotInstanceManager: SnapshotInstanceManager = /*#__PURE__*/ cardLifetime(
24
+ 'snapshotInstanceManager',
25
+ () => ({ nextId: 0, values: new Map<number, SnapshotInstance>(), clear }),
26
+ );
27
+
28
+ // Rebound so that it clears the debug state of the current entry.
29
+ snapshotInstanceManager.clear = clear;