@lynx-js/react 0.126.2 → 0.127.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 (190) hide show
  1. package/CHANGELOG.md +18 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/docs/directives.md +90 -0
  4. package/docs/global-events.md +50 -0
  5. package/docs/import-attributes.md +43 -0
  6. package/docs/macros.md +285 -0
  7. package/package.json +3 -2
  8. package/refresh/.turbo/turbo-build.log +1 -1
  9. package/refresh/dist/index.js +26 -6
  10. package/refresh/package.json +1 -1
  11. package/runtime/__test__/core/main-thread-object.test.js +149 -0
  12. package/runtime/__test__/element-template/fixtures/background/main-thread-function/run-on-main-thread/index.tsx +26 -3
  13. package/runtime/__test__/element-template/imports/main-thread-object-api.test.ts +10 -0
  14. package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +3 -0
  15. package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +4 -0
  16. package/runtime/__test__/snapshot/ctxNotFound.test.ts +271 -0
  17. package/runtime/__test__/snapshot/event.test.jsx +7 -7
  18. package/runtime/__test__/snapshot/lazy.test.js +73 -1
  19. package/runtime/__test__/snapshot/lynx/portals.test.jsx +75 -37
  20. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +59 -47
  21. package/runtime/__test__/snapshot/spread.test.jsx +10 -10
  22. package/runtime/__test__/snapshot/worklet/main-thread-object.test.jsx +306 -0
  23. package/runtime/__test__/snapshot/worklet/workletRef.test.jsx +1 -0
  24. package/runtime/__test__/worklet-runtime/mainThreadObject.test.js +182 -0
  25. package/runtime/__test__/worklet-runtime/object-methods.test.js +60 -0
  26. package/runtime/__test__/worklet-runtime/workletRef.test.js +362 -2
  27. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +246 -1
  28. package/runtime/lazy/compat.js +2 -0
  29. package/runtime/lazy/element-template-internal.js +1 -0
  30. package/runtime/lazy/element-template.js +2 -0
  31. package/runtime/lazy/internal.js +2 -0
  32. package/runtime/lazy/react.js +2 -0
  33. package/runtime/lib/core/background-function/run-on-background.d.ts +16 -0
  34. package/runtime/lib/core/background-function/run-on-background.js +16 -0
  35. package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
  36. package/runtime/lib/core/capture-main-thread-object.d.ts +1 -0
  37. package/runtime/lib/core/capture-main-thread-object.js +16 -0
  38. package/runtime/lib/core/capture-main-thread-object.js.map +1 -0
  39. package/runtime/lib/core/globalProps.d.ts +2 -0
  40. package/runtime/lib/core/globalProps.js.map +1 -1
  41. package/runtime/lib/core/hooks/react.d.ts +4 -0
  42. package/runtime/lib/core/hooks/react.js +4 -0
  43. package/runtime/lib/core/hooks/react.js.map +1 -1
  44. package/runtime/lib/core/hooks/useLynxGlobalEventListener.d.ts +2 -0
  45. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js +2 -0
  46. package/runtime/lib/core/hooks/useLynxGlobalEventListener.js.map +1 -1
  47. package/runtime/lib/core/initData.d.ts +2 -0
  48. package/runtime/lib/core/initData.js +2 -0
  49. package/runtime/lib/core/initData.js.map +1 -1
  50. package/runtime/lib/core/main-thread-object-handle-registry.d.ts +3 -0
  51. package/runtime/lib/core/main-thread-object-handle-registry.js +17 -0
  52. package/runtime/lib/core/main-thread-object-handle-registry.js.map +1 -0
  53. package/runtime/lib/core/main-thread-object.d.ts +77 -0
  54. package/runtime/lib/core/main-thread-object.js +261 -0
  55. package/runtime/lib/core/main-thread-object.js.map +1 -0
  56. package/runtime/lib/core/main-thread-ref-id.d.ts +1 -0
  57. package/runtime/lib/core/main-thread-ref-id.js +14 -0
  58. package/runtime/lib/core/main-thread-ref-id.js.map +1 -0
  59. package/runtime/lib/core/main-thread-ref-init-value.d.ts +1 -1
  60. package/runtime/lib/core/main-thread-ref-init-value.js +4 -2
  61. package/runtime/lib/core/main-thread-ref-init-value.js.map +1 -1
  62. package/runtime/lib/core/main-thread-ref.d.ts +4 -0
  63. package/runtime/lib/core/main-thread-ref.js +5 -8
  64. package/runtime/lib/core/main-thread-ref.js.map +1 -1
  65. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  66. package/runtime/lib/element-template/index.d.ts +2 -0
  67. package/runtime/lib/element-template/index.js +1 -0
  68. package/runtime/lib/element-template/index.js.map +1 -1
  69. package/runtime/lib/element-template/internal.d.ts +1 -0
  70. package/runtime/lib/element-template/internal.js +1 -0
  71. package/runtime/lib/element-template/internal.js.map +1 -1
  72. package/runtime/lib/element-template/protocol/types.d.ts +1 -1
  73. package/runtime/lib/internal.d.ts +1 -0
  74. package/runtime/lib/internal.js +1 -0
  75. package/runtime/lib/internal.js.map +1 -1
  76. package/runtime/lib/lynx-api.d.ts +34 -0
  77. package/runtime/lib/lynx-api.js +21 -0
  78. package/runtime/lib/lynx-api.js.map +1 -1
  79. package/runtime/lib/lynx.js +4 -1
  80. package/runtime/lib/lynx.js.map +1 -1
  81. package/runtime/lib/snapshot/lifecycle/patch/error.d.ts +11 -1
  82. package/runtime/lib/snapshot/lifecycle/patch/error.js +24 -16
  83. package/runtime/lib/snapshot/lifecycle/patch/error.js.map +1 -1
  84. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.d.ts +1 -1
  85. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +29 -7
  86. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  87. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +2 -2
  88. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  89. package/runtime/lib/snapshot/lynx/children.d.ts +2 -0
  90. package/runtime/lib/snapshot/lynx/children.js.map +1 -1
  91. package/runtime/lib/snapshot/lynx/element.d.ts +8 -0
  92. package/runtime/lib/snapshot/lynx/element.js +4 -0
  93. package/runtime/lib/snapshot/lynx/element.js.map +1 -1
  94. package/runtime/lib/snapshot/worklet/call/runOnMainThread.d.ts +2 -0
  95. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +2 -0
  96. package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
  97. package/runtime/lib/snapshot/worklet/ref/updateInitValue.js.map +1 -1
  98. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  99. package/runtime/lib/worklet-runtime/bindings/bindings.d.ts +3 -3
  100. package/runtime/lib/worklet-runtime/bindings/bindings.js +9 -1
  101. package/runtime/lib/worklet-runtime/bindings/bindings.js.map +1 -1
  102. package/runtime/lib/worklet-runtime/bindings/types.d.ts +3 -6
  103. package/runtime/lib/worklet-runtime/bindings/types.js.map +1 -1
  104. package/runtime/lib/worklet-runtime/hydrate.js +26 -7
  105. package/runtime/lib/worklet-runtime/hydrate.js.map +1 -1
  106. package/runtime/lib/worklet-runtime/mainThreadObject.d.ts +8 -0
  107. package/runtime/lib/worklet-runtime/mainThreadObject.js +67 -0
  108. package/runtime/lib/worklet-runtime/mainThreadObject.js.map +1 -0
  109. package/runtime/lib/worklet-runtime/workletRef.d.ts +11 -7
  110. package/runtime/lib/worklet-runtime/workletRef.js +72 -9
  111. package/runtime/lib/worklet-runtime/workletRef.js.map +1 -1
  112. package/runtime/lib/worklet-runtime/workletRuntime.js +68 -9
  113. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  114. package/runtime/package.json +2 -3
  115. package/runtime/src/core/background-function/run-on-background.ts +16 -0
  116. package/runtime/src/core/capture-main-thread-object.ts +17 -0
  117. package/runtime/src/core/globalProps.ts +2 -0
  118. package/runtime/src/core/hooks/react.ts +4 -0
  119. package/runtime/src/core/hooks/useLynxGlobalEventListener.ts +2 -0
  120. package/runtime/src/core/initData.ts +2 -0
  121. package/runtime/src/core/main-thread-object-handle-registry.ts +25 -0
  122. package/runtime/src/core/main-thread-object.ts +387 -0
  123. package/runtime/src/core/main-thread-ref-id.ts +16 -0
  124. package/runtime/src/core/main-thread-ref-init-value.ts +12 -4
  125. package/runtime/src/core/main-thread-ref.ts +7 -8
  126. package/runtime/src/element-template/background/commit-hook.ts +1 -1
  127. package/runtime/src/element-template/index.ts +6 -0
  128. package/runtime/src/element-template/internal.ts +2 -0
  129. package/runtime/src/element-template/protocol/types.ts +1 -1
  130. package/runtime/src/internal.ts +1 -0
  131. package/runtime/src/lynx-api.ts +38 -0
  132. package/runtime/src/lynx.ts +5 -1
  133. package/runtime/src/snapshot/lifecycle/patch/error.ts +36 -19
  134. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +29 -7
  135. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +2 -2
  136. package/runtime/src/snapshot/lynx/children.ts +2 -0
  137. package/runtime/src/snapshot/lynx/element.ts +8 -0
  138. package/runtime/src/snapshot/worklet/call/runOnMainThread.ts +2 -0
  139. package/runtime/src/snapshot/worklet/ref/updateInitValue.ts +1 -1
  140. package/runtime/src/worklet-runtime/bindings/bindings.ts +17 -2
  141. package/runtime/src/worklet-runtime/bindings/types.ts +7 -11
  142. package/runtime/src/worklet-runtime/hydrate.ts +43 -10
  143. package/runtime/src/worklet-runtime/mainThreadObject.ts +117 -0
  144. package/runtime/src/worklet-runtime/workletRef.ts +120 -19
  145. package/runtime/src/worklet-runtime/workletRuntime.ts +83 -11
  146. package/runtime/worklet-runtime/dev.js +154 -17
  147. package/runtime/worklet-runtime/main.js +1 -1
  148. package/testing-library/.turbo/turbo-build.log +6 -6
  149. package/testing-library/dist/index.d.ts +165 -2
  150. package/testing-library/package.json +2 -1
  151. package/testing-library/src/__tests__/lynx.test.jsx +1 -1
  152. package/testing-library/src/__tests__/worklet.test.jsx +46 -1
  153. package/testing-library/tsconfig.docs.json +9 -0
  154. package/testing-library/typedoc.json +22 -0
  155. package/transform/.turbo/turbo-build$colon$wasm.log +150 -78
  156. package/transform/__test__/fixture.spec.js +72 -30
  157. package/transform/__test__/object-methods.spec.js +84 -0
  158. package/transform/crates/swc_plugin_worklet/decl_collect.rs +10 -0
  159. package/transform/crates/swc_plugin_worklet/extract_ident.rs +14 -0
  160. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +202 -3
  161. package/transform/crates/swc_plugin_worklet/lib.rs +468 -3
  162. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +8 -1
  163. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +10 -5
  164. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_js.js +27 -0
  165. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_lepus.js +45 -0
  166. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +16 -14
  167. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +16 -16
  168. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +7 -5
  169. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +7 -7
  170. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_computed_object_method_key_lepus.js +24 -0
  171. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_object_method_local_decls_lepus.js +16 -0
  172. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +42 -0
  173. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +58 -0
  174. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +3 -1
  175. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +3 -3
  176. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_js.js +9 -0
  177. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_lepus.js +16 -0
  178. package/transform/dist/wasm.cjs +1 -1
  179. package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
  180. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +66 -469
  181. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  182. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +364 -30
  183. package/transform/swc-plugin-reactlynx-compat/swc_plugin_reactlynx_compat.wasm +0 -0
  184. package/transform/vitest.config.ts +5 -1
  185. package/types/react.docs.d.ts +130 -11
  186. package/testing-library/node_modules/.bin/rsbuild +0 -68
  187. package/testing-library/node_modules/.bin/rspeedy +0 -68
  188. package/testing-library/node_modules/.bin/rstest +0 -68
  189. package/transform/node_modules/.bin/esbuild +0 -59
  190. package/transform/node_modules/.bin/napi +0 -68
@@ -0,0 +1,271 @@
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, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
5
+
6
+ import { globalEnvManager } from './utils/envManager.js';
7
+ import { addCtxNotFoundEventListener, reportCtxNotFound } from '../../src/snapshot/lifecycle/patch/error.js';
8
+ import { SnapshotOperation } from '../../src/snapshot/lifecycle/patch/snapshotPatch.js';
9
+ import { snapshotPatchApply } from '../../src/snapshot/lifecycle/patch/snapshotPatchApply.js';
10
+ import { injectUpdateMainThread } from '../../src/snapshot/lifecycle/patch/updateMainThread.js';
11
+ import { LifecycleConstant } from '../../src/snapshot/lifecycle/constant.js';
12
+ import { getReloadVersion } from '../../src/core/reload-version.js';
13
+ import { createSnapshot, setupPage, snapshotManager } from '../../src/snapshot/snapshot/definition.js';
14
+ import { SnapshotInstance } from '../../src/snapshot/snapshot/snapshot.js';
15
+ import { BackgroundSnapshotInstance } from '../../src/snapshot/snapshot/backgroundSnapshot.js';
16
+
17
+ beforeAll(() => {
18
+ globalEnvManager.resetEnv();
19
+ globalEnvManager.switchToBackground();
20
+ addCtxNotFoundEventListener();
21
+ globalEnvManager.switchToMainThread();
22
+ setupPage(__CreatePage('0', 0));
23
+ injectUpdateMainThread();
24
+ });
25
+
26
+ beforeEach(() => {
27
+ globalEnvManager.resetEnv();
28
+ vi.clearAllMocks();
29
+ vi.spyOn(lynx, 'reportError');
30
+ });
31
+
32
+ afterEach(() => {
33
+ vi.restoreAllMocks();
34
+ vi.unstubAllGlobals();
35
+ });
36
+
37
+ function reportedMessage(): string {
38
+ return (vi.mocked(lynx.reportError).mock.calls[0]![0] as Error).message;
39
+ }
40
+
41
+ describe('ctx-not-found diagnostics', () => {
42
+ it.each(['InsertBefore', 'RemoveChild'] as const)(
43
+ 'reports parent and child IDs when either node is missing in %s',
44
+ operation => {
45
+ const cases = [
46
+ { parentExists: false, childExists: true, missingNode: 1 },
47
+ { parentExists: true, childExists: false, missingNode: 2 },
48
+ { parentExists: false, childExists: false, missingNode: 3 },
49
+ ];
50
+ for (const { parentExists, childExists, missingNode } of cases) {
51
+ globalEnvManager.resetEnv();
52
+ vi.clearAllMocks();
53
+ if (parentExists) {
54
+ new SnapshotInstance('view', 100);
55
+ }
56
+ if (childExists) {
57
+ new SnapshotInstance('view', 101);
58
+ }
59
+ const patch = operation === 'InsertBefore'
60
+ ? [SnapshotOperation.InsertBefore, 100, 101, undefined, 0]
61
+ : [SnapshotOperation.RemoveChild, 100, 101];
62
+ snapshotPatchApply(patch, 42, false);
63
+ expect(lynx.getJSContext().dispatchEvent).toHaveBeenCalledExactlyOnceWith({
64
+ type: 'Lynx.Error.CtxNotFound',
65
+ data: {
66
+ id: parentExists ? 101 : 100,
67
+ options: {
68
+ operation,
69
+ commitId: 42,
70
+ isHydration: false,
71
+ parent: 100,
72
+ child: 101,
73
+ missingNode,
74
+ },
75
+ },
76
+ });
77
+ expect(lynx.reportError).toHaveBeenCalledTimes(1);
78
+ expect(reportedMessage()).toBe(
79
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'null', parent: { id: 100, snapshot type: 'null', missing: ${!parentExists} }, child: { id: 101, snapshot type: 'null', missing: ${!childExists} }, operation: '${operation}', commitId: 42, isHydration: false. You can set environment variable \`REACT_ALOG=true\` and restart your dev server for troubleshooting.`,
80
+ );
81
+ }
82
+ },
83
+ );
84
+
85
+ it.each([undefined, false, true])('reports commit IDs and hydration flag %s', flag => {
86
+ vi.stubGlobal('__ALOG__', false);
87
+ vi.stubGlobal('__DEV__', false);
88
+ const isHydration = flag === true;
89
+ const patchList = [
90
+ { id: 41, snapshotPatch: [SnapshotOperation.RemoveChild, 100, 101] },
91
+ { id: 42, snapshotPatch: [SnapshotOperation.SetAttributes, 102, ['attribute-value']] },
92
+ ];
93
+ Reflect.get(globalThis, LifecycleConstant.patchUpdate)({
94
+ data: JSON.stringify({ patchList }),
95
+ patchOptions: { reloadVersion: getReloadVersion(), ...(flag === undefined ? {} : { isHydration: flag }) },
96
+ });
97
+ expect(lynx.getJSContext().dispatchEvent).toHaveBeenCalledTimes(2);
98
+ expect(vi.mocked(lynx.getJSContext().dispatchEvent).mock.calls.map(([event]) => event.data)).toEqual([
99
+ {
100
+ id: 100,
101
+ options: {
102
+ operation: 'RemoveChild',
103
+ commitId: 41,
104
+ isHydration,
105
+ parent: 100,
106
+ child: 101,
107
+ missingNode: 3,
108
+ },
109
+ },
110
+ { id: 102, options: { operation: 'SetAttributes', commitId: 42, isHydration } },
111
+ ]);
112
+ expect(vi.mocked(lynx.reportError).mock.calls.map(([error]) => (error as Error).message)).toEqual([
113
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'null', parent: { id: 100, snapshot type: 'null', missing: true }, child: { id: 101, snapshot type: 'null', missing: true }, operation: 'RemoveChild', commitId: 41, isHydration: ${isHydration}`,
114
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'null', parent: null, operation: 'SetAttributes', commitId: 42, isHydration: ${isHydration}`,
115
+ ]);
116
+ });
117
+
118
+ it.each(['SetAttribute', 'SetAttributes'] as const)('includes the background parent in %s errors', operation => {
119
+ vi.stubGlobal('__DEV__', false);
120
+ globalEnvManager.switchToBackground();
121
+ const parent = new BackgroundSnapshotInstance('view');
122
+ const child = new BackgroundSnapshotInstance('text');
123
+ parent.appendChild(child);
124
+ globalEnvManager.switchToMainThread();
125
+ const patch = operation === 'SetAttribute'
126
+ ? [SnapshotOperation.SetAttribute, child.__id, 0, 'attribute-value']
127
+ : [SnapshotOperation.SetAttributes, child.__id, ['attribute-value']];
128
+ snapshotPatchApply(patch, 42, false);
129
+ expect(lynx.getJSContext().dispatchEvent).toHaveBeenCalledTimes(1);
130
+ expect(reportedMessage()).toBe(
131
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'text', parent: { id: ${parent.__id}, snapshot type: 'view' }, operation: '${operation}', commitId: 42, isHydration: false`,
132
+ );
133
+ });
134
+
135
+ it('includes both snapshot types in one error even when reporting throws', () => {
136
+ vi.stubGlobal('__DEV__', false);
137
+ globalEnvManager.switchToBackground();
138
+ const parent = new BackgroundSnapshotInstance('view');
139
+ const child = new BackgroundSnapshotInstance('text');
140
+ globalEnvManager.switchToMainThread();
141
+ vi.mocked(lynx.reportError).mockImplementationOnce(error => {
142
+ throw error;
143
+ });
144
+ expect(() => snapshotPatchApply([SnapshotOperation.RemoveChild, parent.__id, child.__id], 42, false))
145
+ .toThrowError(
146
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'view', parent: { id: ${parent.__id}, snapshot type: 'view', missing: true }, child: { id: ${child.__id}, snapshot type: 'text', missing: true }, operation: 'RemoveChild', commitId: 42, isHydration: false`,
147
+ );
148
+ expect(lynx.getJSContext().dispatchEvent).toHaveBeenCalledTimes(1);
149
+ expect(lynx.reportError).toHaveBeenCalledTimes(1);
150
+ });
151
+
152
+ it('uses the explicit parent ID instead of the background parent', () => {
153
+ vi.stubGlobal('__DEV__', false);
154
+ globalEnvManager.switchToBackground();
155
+ const backgroundParent = new BackgroundSnapshotInstance('view');
156
+ const child = new BackgroundSnapshotInstance('text');
157
+ const explicitParent = new BackgroundSnapshotInstance('view');
158
+ backgroundParent.appendChild(child);
159
+ reportCtxNotFound({
160
+ id: child.__id,
161
+ options: {
162
+ operation: 'InsertBefore',
163
+ commitId: 42,
164
+ isHydration: true,
165
+ parent: explicitParent.__id,
166
+ child: child.__id,
167
+ },
168
+ });
169
+ expect(lynx.reportError).toHaveBeenCalledTimes(1);
170
+ expect(reportedMessage()).toBe(
171
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'text', parent: { id: ${explicitParent.__id}, snapshot type: 'view' }, child: { id: ${child.__id}, snapshot type: 'text' }, operation: 'InsertBefore', commitId: 42, isHydration: true`,
172
+ );
173
+ });
174
+
175
+ it('preserves zero parent and child IDs', () => {
176
+ vi.stubGlobal('__DEV__', false);
177
+ globalEnvManager.switchToBackground();
178
+ const parent = new BackgroundSnapshotInstance('view');
179
+ const child = new BackgroundSnapshotInstance('text');
180
+ parent.appendChild(child);
181
+ reportCtxNotFound({
182
+ id: child.__id,
183
+ options: {
184
+ operation: 'InsertBefore',
185
+ commitId: 42,
186
+ isHydration: false,
187
+ parent: 0,
188
+ child: 0,
189
+ },
190
+ });
191
+ expect(lynx.reportError).toHaveBeenCalledTimes(1);
192
+ expect(reportedMessage()).toBe(
193
+ 'snapshotPatchApply failed: ctx not found, snapshot type: \'text\', parent: { id: 0, snapshot type: \'null\' }, child: { id: 0, snapshot type: \'null\' }, operation: \'InsertBefore\', commitId: 42, isHydration: false',
194
+ );
195
+ });
196
+
197
+ it.each(['div', null])('preserves null snapshot types for parent and child with type %s', type => {
198
+ vi.stubGlobal('__DEV__', false);
199
+ globalEnvManager.switchToBackground();
200
+ const parent = new BackgroundSnapshotInstance(type as string);
201
+ const child = new BackgroundSnapshotInstance(type as string);
202
+ globalEnvManager.switchToMainThread();
203
+ snapshotPatchApply([SnapshotOperation.RemoveChild, parent.__id, child.__id], 42, false);
204
+ expect(reportedMessage()).toBe(
205
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'null', parent: { id: ${parent.__id}, snapshot type: 'null', missing: true }, child: { id: ${child.__id}, snapshot type: 'null', missing: true }, operation: 'RemoveChild', commitId: 42, isHydration: false`,
206
+ );
207
+ });
208
+
209
+ it.each(['removed', 'replaced'] as const)('reports instance types after their definitions are %s', change => {
210
+ vi.stubGlobal('__DEV__', false);
211
+ globalEnvManager.switchToBackground();
212
+ const parent = new BackgroundSnapshotInstance('ctx-not-found-parent');
213
+ const child = new BackgroundSnapshotInstance('ctx-not-found-child');
214
+ parent.appendChild(child);
215
+ for (const instance of [parent, child]) {
216
+ if (change === 'removed') {
217
+ snapshotManager.values.delete(instance.type);
218
+ } else {
219
+ createSnapshot(instance.type, null, null, [], undefined, undefined, null, true);
220
+ }
221
+ expect(snapshotManager.values.get(instance.type)).not.toBe(instance.__snapshot_def);
222
+ }
223
+ globalEnvManager.switchToMainThread();
224
+ snapshotPatchApply([SnapshotOperation.SetAttributes, child.__id, []], 42, false);
225
+ snapshotPatchApply([SnapshotOperation.RemoveChild, parent.__id, child.__id], 43, false);
226
+ expect(lynx.getJSContext().dispatchEvent).toHaveBeenCalledTimes(2);
227
+ expect(vi.mocked(lynx.reportError).mock.calls.map(([error]) => (error as Error).message)).toEqual([
228
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'ctx-not-found-child', parent: { id: ${parent.__id}, snapshot type: 'ctx-not-found-parent' }, operation: 'SetAttributes', commitId: 42, isHydration: false`,
229
+ `snapshotPatchApply failed: ctx not found, snapshot type: 'ctx-not-found-parent', parent: { id: ${parent.__id}, snapshot type: 'ctx-not-found-parent', missing: true }, child: { id: ${child.__id}, snapshot type: 'ctx-not-found-child', missing: true }, operation: 'RemoveChild', commitId: 43, isHydration: false`,
230
+ ]);
231
+ });
232
+
233
+ it('includes diagnostics in production without attribute values', () => {
234
+ vi.stubGlobal('__DEV__', false);
235
+ snapshotPatchApply([SnapshotOperation.SetAttributes, 999, ['attribute-value']], 42, false);
236
+ expect(reportedMessage()).toBe(
237
+ 'snapshotPatchApply failed: ctx not found, snapshot type: \'null\', parent: null, operation: \'SetAttributes\', commitId: 42, isHydration: false',
238
+ );
239
+ });
240
+
241
+ it.each(['div', null])('preserves the null snapshot type for %s', type => {
242
+ vi.stubGlobal('__DEV__', false);
243
+ globalEnvManager.switchToBackground();
244
+ const instance = new BackgroundSnapshotInstance(type as string);
245
+ globalEnvManager.switchToMainThread();
246
+ snapshotPatchApply([SnapshotOperation.SetAttributes, instance.__id, []], 42, false);
247
+ expect(reportedMessage()).toBe(
248
+ 'snapshotPatchApply failed: ctx not found, snapshot type: \'null\', parent: null, operation: \'SetAttributes\', commitId: 42, isHydration: false',
249
+ );
250
+ });
251
+
252
+ it.each([false, true])('preserves the fallback error with hydration flag %s', isHydration => {
253
+ vi.stubGlobal('lynx', { ...lynx, getJSContext: undefined });
254
+ expect(() => snapshotPatchApply([SnapshotOperation.SetAttributes, 999, []], 42, isHydration))
255
+ .toThrowError(new Error('snapshotPatchApply failed: ctx not found'));
256
+ expect(() => snapshotPatchApply([SnapshotOperation.RemoveChild, 100, 101], 42, isHydration))
257
+ .toThrowError(new Error('snapshotPatchApply failed: ctx not found'));
258
+ });
259
+
260
+ it('reports diagnostics and preserves the fallback error when ALOG is unavailable', () => {
261
+ vi.stubGlobal('__DEV__', false);
262
+ vi.stubGlobal('console', { ...console, alog: undefined });
263
+ snapshotPatchApply([SnapshotOperation.SetAttributes, 999, []], 42, false);
264
+ expect(reportedMessage()).toBe(
265
+ 'snapshotPatchApply failed: ctx not found, snapshot type: \'null\', parent: null, operation: \'SetAttributes\', commitId: 42, isHydration: false',
266
+ );
267
+ vi.stubGlobal('lynx', { ...lynx, getJSContext: undefined });
268
+ expect(() => snapshotPatchApply([SnapshotOperation.SetAttributes, 999, []], 42, true))
269
+ .toThrowError(new Error('snapshotPatchApply failed: ctx not found'));
270
+ });
271
+ });
@@ -221,7 +221,7 @@ describe('eventUpdate', () => {
221
221
  ]
222
222
  `);
223
223
  globalEnvManager.switchToMainThread();
224
- snapshotPatchApply(patch);
224
+ snapshotPatchApply(patch, 42, false);
225
225
  expect(__root.__element_root).toMatchInlineSnapshot(`
226
226
  <page
227
227
  cssId="default-entry-from-native:0"
@@ -324,7 +324,7 @@ describe('eventUpdate', () => {
324
324
  ]
325
325
  `);
326
326
  globalEnvManager.switchToMainThread();
327
- snapshotPatchApply(patch);
327
+ snapshotPatchApply(patch, 42, false);
328
328
  expect(__root.__element_root).toMatchInlineSnapshot(`
329
329
  <page
330
330
  cssId="default-entry-from-native:0"
@@ -437,7 +437,7 @@ describe('eventUpdate', () => {
437
437
  patch = takeGlobalSnapshotPatch();
438
438
  expect(patch).toMatchInlineSnapshot(`[]`);
439
439
  globalEnvManager.switchToMainThread();
440
- snapshotPatchApply(patch);
440
+ snapshotPatchApply(patch, 42, false);
441
441
  globalEnvManager.switchToBackground();
442
442
 
443
443
  lynx.getApp().publishEvent('-2:0:', 'data3');
@@ -616,7 +616,7 @@ describe('eventUpdate', () => {
616
616
  ]
617
617
  `);
618
618
  globalEnvManager.switchToMainThread();
619
- snapshotPatchApply(patch);
619
+ snapshotPatchApply(patch, 42, false);
620
620
  expect(__root.__element_root).toMatchInlineSnapshot(`
621
621
  <page
622
622
  cssId="default-entry-from-native:0"
@@ -784,7 +784,7 @@ describe('event in spread', () => {
784
784
  ]
785
785
  `);
786
786
  globalEnvManager.switchToMainThread();
787
- snapshotPatchApply(patch);
787
+ snapshotPatchApply(patch, 42, false);
788
788
  expect(__root.__element_root).toMatchInlineSnapshot(`
789
789
  <page
790
790
  cssId="default-entry-from-native:0"
@@ -858,7 +858,7 @@ describe('event in spread', () => {
858
858
  `);
859
859
 
860
860
  globalEnvManager.switchToMainThread();
861
- snapshotPatchApply(patch);
861
+ snapshotPatchApply(patch, 42, false);
862
862
  expect(__root.__element_root).toMatchInlineSnapshot(`
863
863
  <page
864
864
  cssId="default-entry-from-native:0"
@@ -953,7 +953,7 @@ describe('event in spread', () => {
953
953
  ]
954
954
  `);
955
955
  globalEnvManager.switchToMainThread();
956
- snapshotPatchApply(patch);
956
+ snapshotPatchApply(patch, 42, false);
957
957
  expect(__root.__element_root).toMatchInlineSnapshot(`
958
958
  <page
959
959
  cssId="default-entry-from-native:0"
@@ -8,7 +8,13 @@ import * as ReactInternalExports from '../../lazy/internal.js';
8
8
  import * as ReactJSXRuntimeExports from '../../lazy/jsx-runtime.js';
9
9
  import * as ReactJSXDevRuntimeExports from '../../lazy/jsx-dev-runtime.js';
10
10
  import * as ReactLegacyReactRuntimeExports from '../../lazy/legacy-react-runtime.js';
11
- import { sRuntimeBackend, target } from '../../lazy/target.js';
11
+ import {
12
+ sExportsReact,
13
+ sExportsReactCompat,
14
+ sExportsReactInternal,
15
+ sRuntimeBackend,
16
+ target,
17
+ } from '../../lazy/target.js';
12
18
  const {
13
19
  RUNTIME_BACKEND_ELEMENT_TEMPLATE,
14
20
  RUNTIME_BACKEND_SNAPSHOT,
@@ -147,6 +153,60 @@ describe('Lazy Exports', () => {
147
153
  );
148
154
  });
149
155
 
156
+ test('forwards MainThreadObject lazy exports from the host runtime', () => {
157
+ expect(ReactExports.defineMainThreadObjectType).toBe(
158
+ target[sExportsReact].defineMainThreadObjectType,
159
+ );
160
+ expect(ReactExports.useMainThreadObject).toBe(target[sExportsReact].useMainThreadObject);
161
+ expect(ReactCompatExports.defineMainThreadObjectType).toBe(
162
+ target[sExportsReactCompat].defineMainThreadObjectType,
163
+ );
164
+ expect(ReactCompatExports.useMainThreadObject).toBe(
165
+ target[sExportsReactCompat].useMainThreadObject,
166
+ );
167
+ expect(ReactInternalExports.captureMainThreadObject).toBe(
168
+ target[sExportsReactInternal].captureMainThreadObject,
169
+ );
170
+ });
171
+
172
+ test('registers a MainThreadObject type while evaluating a lazy MTS module', () => {
173
+ const originalJS = globalThis.__JS__;
174
+ const originalLepus = globalThis.__LEPUS__;
175
+ const originalMainThread = globalThis.__MAIN_THREAD__;
176
+ const originalBackground = globalThis.__BACKGROUND__;
177
+ const originalWorkletImpl = globalThis.lynxWorkletImpl;
178
+ const registerMainThreadObjectType = vi.fn();
179
+ const create = value => ({ value });
180
+
181
+ globalThis.__JS__ = false;
182
+ globalThis.__LEPUS__ = true;
183
+ globalThis.__MAIN_THREAD__ = true;
184
+ globalThis.__BACKGROUND__ = false;
185
+ globalThis.lynxWorkletImpl = {
186
+ _refImpl: {
187
+ registerMainThreadObjectType,
188
+ },
189
+ };
190
+
191
+ try {
192
+ ReactExports.defineMainThreadObjectType({
193
+ type: '@test/lazy-module-evaluation',
194
+ create,
195
+ });
196
+
197
+ expect(registerMainThreadObjectType).toHaveBeenCalledWith(
198
+ '@test/lazy-module-evaluation',
199
+ create,
200
+ );
201
+ } finally {
202
+ globalThis.__JS__ = originalJS;
203
+ globalThis.__LEPUS__ = originalLepus;
204
+ globalThis.__MAIN_THREAD__ = originalMainThread;
205
+ globalThis.__BACKGROUND__ = originalBackground;
206
+ globalThis.lynxWorkletImpl = originalWorkletImpl;
207
+ }
208
+ });
209
+
150
210
  test('target background', async () => {
151
211
  vi.resetModules();
152
212
 
@@ -196,6 +256,18 @@ describe('Lazy Exports', () => {
196
256
  });
197
257
  });
198
258
 
259
+ test('allows a standalone lazy import without a stamped runtime version', async () => {
260
+ await withoutRecordedRuntimeVersion(async () => {
261
+ vi.stubGlobal('__RUNTIME_VERSION__', undefined);
262
+ onTestFinished(() => vi.unstubAllGlobals());
263
+ vi.resetModules();
264
+
265
+ await import('../../lazy/internal.js');
266
+
267
+ expect(ReactInternalExports.getRuntimeVersion()).toBeUndefined();
268
+ });
269
+ });
270
+
199
271
  test('records a lazy backend when no main template marker exists', async () => {
200
272
  await withRuntimeBackend(undefined, () => {
201
273
  registerRuntimeBackend(RUNTIME_BACKEND_SNAPSHOT);
@@ -521,12 +521,16 @@ describe('snapshotPatchApply for nodesRef ops', () => {
521
521
  globalEnvManager.switchToMainThread();
522
522
  lynx.getJSContext().dispatchEvent.mockClear();
523
523
  expect(() =>
524
- snapshotPatchApply([
525
- SnapshotOperation.nodesRefInsertBefore,
526
- '[react-ref-999-0]',
527
- 99999,
528
- undefined,
529
- ])
524
+ snapshotPatchApply(
525
+ [
526
+ SnapshotOperation.nodesRefInsertBefore,
527
+ '[react-ref-999-0]',
528
+ 99999,
529
+ undefined,
530
+ ],
531
+ 42,
532
+ false,
533
+ )
530
534
  ).not.toThrow();
531
535
  expect(lynx.getJSContext().dispatchEvent.mock.calls).toMatchInlineSnapshot(`
532
536
  [
@@ -534,6 +538,11 @@ describe('snapshotPatchApply for nodesRef ops', () => {
534
538
  {
535
539
  "data": {
536
540
  "id": 99999,
541
+ "options": {
542
+ "commitId": 42,
543
+ "isHydration": false,
544
+ "operation": "nodesRefInsertBefore",
545
+ },
537
546
  },
538
547
  "type": "Lynx.Error.CtxNotFound",
539
548
  },
@@ -546,11 +555,15 @@ describe('snapshotPatchApply for nodesRef ops', () => {
546
555
  globalEnvManager.switchToMainThread();
547
556
  lynx.getJSContext().dispatchEvent.mockClear();
548
557
  expect(() =>
549
- snapshotPatchApply([
550
- SnapshotOperation.nodesRefRemoveChild,
551
- '[react-ref-999-0]',
552
- 99999,
553
- ])
558
+ snapshotPatchApply(
559
+ [
560
+ SnapshotOperation.nodesRefRemoveChild,
561
+ '[react-ref-999-0]',
562
+ 99999,
563
+ ],
564
+ 43,
565
+ false,
566
+ )
554
567
  ).not.toThrow();
555
568
  expect(lynx.getJSContext().dispatchEvent.mock.calls).toMatchInlineSnapshot(`
556
569
  [
@@ -558,6 +571,11 @@ describe('snapshotPatchApply for nodesRef ops', () => {
558
571
  {
559
572
  "data": {
560
573
  "id": 99999,
574
+ "options": {
575
+ "commitId": 43,
576
+ "isHydration": false,
577
+ "operation": "nodesRefRemoveChild",
578
+ },
561
579
  },
562
580
  "type": "Lynx.Error.CtxNotFound",
563
581
  },
@@ -576,20 +594,28 @@ describe('snapshotPatchApply for nodesRef ops', () => {
576
594
  it('throws when host selector cannot be resolved on insert', () => {
577
595
  globalEnvManager.switchToMainThread();
578
596
  const childId = -9999;
579
- snapshotPatchApply([
580
- SnapshotOperation.CreateElement,
581
- '__snapshot_a94a8_test_1',
582
- childId,
583
- ]);
597
+ snapshotPatchApply(
598
+ [
599
+ SnapshotOperation.CreateElement,
600
+ '__snapshot_a94a8_test_1',
601
+ childId,
602
+ ],
603
+ 42,
604
+ false,
605
+ );
584
606
  expect(snapshotInstanceManager.values.has(childId)).toBe(true);
585
607
 
586
608
  expect(() =>
587
- snapshotPatchApply([
588
- SnapshotOperation.nodesRefInsertBefore,
589
- '[no-such-attr]',
590
- childId,
591
- undefined,
592
- ])
609
+ snapshotPatchApply(
610
+ [
611
+ SnapshotOperation.nodesRefInsertBefore,
612
+ '[no-such-attr]',
613
+ childId,
614
+ undefined,
615
+ ],
616
+ 42,
617
+ false,
618
+ )
593
619
  ).toThrowErrorMatchingInlineSnapshot(
594
620
  `[Error: [createPortal] cannot resolve host for selector "[no-such-attr]". The host element does not exist on the main thread — check that the \`NodesRef\` passed to \`createPortal\` points at a currently mounted element.]`,
595
621
  );
@@ -600,21 +626,29 @@ describe('snapshotPatchApply for nodesRef ops', () => {
600
626
  // Materialize a child with an `__element_root` (the realistic state at
601
627
  // remove time — an earlier `nodesRefInsertBefore` op would have set it).
602
628
  const childId = -9998;
603
- snapshotPatchApply([
604
- SnapshotOperation.CreateElement,
605
- '__snapshot_a94a8_test_1',
606
- childId,
607
- ]);
629
+ snapshotPatchApply(
630
+ [
631
+ SnapshotOperation.CreateElement,
632
+ '__snapshot_a94a8_test_1',
633
+ childId,
634
+ ],
635
+ 42,
636
+ false,
637
+ );
608
638
  snapshotInstanceManager.values.get(childId).ensureElements();
609
639
 
610
640
  // Remove must not throw even when the host is gone — see the comment
611
641
  // on `applyNodesRefRemoveChild`. The portal child SI is still cleaned up.
612
642
  expect(() =>
613
- snapshotPatchApply([
614
- SnapshotOperation.nodesRefRemoveChild,
615
- '[no-such-attr]',
616
- childId,
617
- ])
643
+ snapshotPatchApply(
644
+ [
645
+ SnapshotOperation.nodesRefRemoveChild,
646
+ '[no-such-attr]',
647
+ childId,
648
+ ],
649
+ 42,
650
+ false,
651
+ )
618
652
  ).not.toThrow();
619
653
  expect(snapshotInstanceManager.values.has(childId)).toBe(false);
620
654
  });
@@ -637,11 +671,15 @@ describe('snapshotPatchApply for nodesRef ops', () => {
637
671
  expect(listElement.componentAtIndex).not.toBeNull();
638
672
 
639
673
  globalEnvManager.switchToMainThread();
640
- snapshotPatchApply([
641
- SnapshotOperation.nodesRefRemoveChild,
642
- '[no-such-attr]',
643
- childId,
644
- ]);
674
+ snapshotPatchApply(
675
+ [
676
+ SnapshotOperation.nodesRefRemoveChild,
677
+ '[no-such-attr]',
678
+ childId,
679
+ ],
680
+ 42,
681
+ false,
682
+ );
645
683
  // After `snapshotDestroyList`, the cleanup trio replaces the real
646
684
  // callbacks: `componentAtIndex` returns `-1`, the others are no-ops.
647
685
  expect(listElement.componentAtIndex()).toBe(-1);