@lynx-js/react 0.129.0 → 0.129.1

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 (206) hide show
  1. package/CHANGELOG.md +14 -0
  2. package/components/tsconfig.tsbuildinfo +1 -1
  3. package/package.json +3 -4
  4. package/refresh/.turbo/turbo-build.log +1 -1
  5. package/runtime/__test__/core/hooks/useLynxGlobalEventListener.test.jsx +1 -1
  6. package/runtime/__test__/element-template/imports/no-legacy-imports.test.ts +7 -6
  7. package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +1 -2
  8. package/runtime/__test__/element-template/native/main-thread-api.test.ts +5 -2
  9. package/runtime/__test__/element-template/native/mts-destroy.test.ts +83 -0
  10. package/runtime/__test__/element-template/native/reload-dual-thread-communication.test.ts +3 -0
  11. package/runtime/__test__/element-template/native/reload.test.ts +31 -2
  12. package/runtime/__test__/element-template/runtime/background/update-timing.test.ts +15 -0
  13. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +197 -7
  14. package/runtime/__test__/element-template/runtime/patch/update-timing.test.ts +4 -1
  15. package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +4 -1
  16. package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +4 -1
  17. package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +101 -1
  18. package/runtime/__test__/element-template/runtime/template/element-template-handle.test.ts +32 -0
  19. package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +121 -26
  20. package/runtime/__test__/element-template/test-utils/debug/envManager.ts +8 -5
  21. package/runtime/__test__/element-template/test-utils/mock/mockNativePapi.ts +35 -2
  22. package/runtime/__test__/guardrails/snapshot-containment.test.ts +45 -11
  23. package/runtime/__test__/shared/component-stack.test.ts +211 -0
  24. package/runtime/__test__/snapshot/debug/formatPatch.test.ts +0 -8
  25. package/runtime/__test__/snapshot/gesture/prepareGestureForCommit.test.js +1 -1
  26. package/runtime/__test__/snapshot/lynx/timing.test.jsx +3 -0
  27. package/runtime/__test__/snapshot/preact.test.jsx +0 -3
  28. package/runtime/__test__/snapshot/snapshotPatch.test.jsx +0 -33
  29. package/runtime/__test__/worklet-runtime/jsFunctionLifecycle.test.js +12 -0
  30. package/runtime/__test__/worklet-runtime/loadRuntime.test.js +68 -0
  31. package/runtime/__test__/worklet-runtime/workletRuntime.test.js +30 -0
  32. package/runtime/lazy/element-template-internal.js +0 -1
  33. package/runtime/lazy/internal.js +1 -1
  34. package/runtime/lazy/jsx-dev-runtime.js +1 -1
  35. package/runtime/lazy/jsx-runtime.js +1 -1
  36. package/runtime/lazy/legacy-react-runtime.js +1 -1
  37. package/runtime/lazy/react-lepus.js +1 -1
  38. package/runtime/lazy/react.js +1 -1
  39. package/runtime/lib/core/capture-main-thread-object.js +2 -1
  40. package/runtime/lib/core/capture-main-thread-object.js.map +1 -1
  41. package/runtime/lib/core/compat/legacy-react-runtime.js +1 -1
  42. package/runtime/lib/core/compat/legacy-react-runtime.js.map +1 -1
  43. package/runtime/lib/core/lynx/lazy-bundle.js +1 -1
  44. package/runtime/lib/core/lynx/lazy-bundle.js.map +1 -1
  45. package/runtime/lib/core/lynx-data-processors.d.ts +125 -1
  46. package/runtime/lib/core/lynx-data-processors.js +3 -0
  47. package/runtime/lib/core/lynx-data-processors.js.map +1 -1
  48. package/runtime/lib/core/runtime-destroy.js +1 -1
  49. package/runtime/lib/core/runtime-destroy.js.map +1 -1
  50. package/runtime/lib/document.js +0 -5
  51. package/runtime/lib/document.js.map +1 -1
  52. package/runtime/lib/element-template/background/commit-hook.js +11 -12
  53. package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
  54. package/runtime/lib/element-template/client/root.d.ts +1 -1
  55. package/runtime/lib/element-template/client/root.js.map +1 -1
  56. package/runtime/lib/element-template/index.d.ts +2 -2
  57. package/runtime/lib/element-template/index.js.map +1 -1
  58. package/runtime/lib/element-template/internal.d.ts +1 -1
  59. package/runtime/lib/element-template/internal.js +0 -13
  60. package/runtime/lib/element-template/internal.js.map +1 -1
  61. package/runtime/lib/element-template/lynx/env.js.map +1 -1
  62. package/runtime/lib/element-template/native/flush.d.ts +1 -0
  63. package/runtime/lib/element-template/native/flush.js +9 -0
  64. package/runtime/lib/element-template/native/flush.js.map +1 -0
  65. package/runtime/lib/element-template/native/main-thread-api.js +3 -7
  66. package/runtime/lib/element-template/native/main-thread-api.js.map +1 -1
  67. package/runtime/lib/element-template/native/mts-destroy.js +8 -2
  68. package/runtime/lib/element-template/native/mts-destroy.js.map +1 -1
  69. package/runtime/lib/element-template/native/patch-listener.js +57 -50
  70. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  71. package/runtime/lib/element-template/native/reload-main-thread.js +2 -1
  72. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  73. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  74. package/runtime/lib/element-template/runtime/page/root-instance.d.ts +0 -1
  75. package/runtime/lib/element-template/runtime/page/root-instance.js.map +1 -1
  76. package/runtime/lib/element-template/runtime/patch.js +4 -0
  77. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  78. package/runtime/lib/element-template/runtime/render/render-components.js +4 -4
  79. package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -1
  80. package/runtime/lib/element-template/runtime/render/render-main-thread.js +2 -0
  81. package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
  82. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js +8 -1
  83. package/runtime/lib/element-template/runtime/template/main-thread-background-function.js.map +1 -1
  84. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +1 -0
  85. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +41 -21
  86. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  87. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +1 -1
  88. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  89. package/runtime/lib/internal.d.ts +1 -1
  90. package/runtime/lib/internal.js +1 -1
  91. package/runtime/lib/internal.js.map +1 -1
  92. package/runtime/lib/lynx-api.d.ts +2 -125
  93. package/runtime/lib/lynx-api.js.map +1 -1
  94. package/runtime/lib/shared/component-stack.d.ts +0 -4
  95. package/runtime/lib/shared/component-stack.js +21 -5
  96. package/runtime/lib/shared/component-stack.js.map +1 -1
  97. package/runtime/lib/snapshot/gesture/{processGestureBagkround.js → processGestureBackground.js} +1 -1
  98. package/runtime/lib/snapshot/gesture/processGestureBackground.js.map +1 -0
  99. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.d.ts +0 -1
  100. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js +0 -5
  101. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatch.js.map +1 -1
  102. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +0 -10
  103. package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
  104. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +56 -51
  105. package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
  106. package/runtime/lib/snapshot/list/listUpdateInfo.js +0 -30
  107. package/runtime/lib/snapshot/list/listUpdateInfo.js.map +1 -1
  108. package/runtime/lib/snapshot/lynx/calledByNative.js +11 -8
  109. package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
  110. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js +0 -52
  111. package/runtime/lib/snapshot/renderToOpcodes/hydrate.js.map +1 -1
  112. package/runtime/lib/snapshot/renderToOpcodes/index.js +10 -10
  113. package/runtime/lib/snapshot/renderToOpcodes/index.js.map +1 -1
  114. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js +0 -51
  115. package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
  116. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js +1 -1
  117. package/runtime/lib/snapshot/snapshot/backgroundSnapshot.js.map +1 -1
  118. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  119. package/runtime/lib/worklet-runtime/bindings/index.d.ts +1 -0
  120. package/runtime/lib/worklet-runtime/bindings/index.js +1 -0
  121. package/runtime/lib/worklet-runtime/bindings/index.js.map +1 -1
  122. package/runtime/lib/worklet-runtime/bindings/loadRuntime.d.ts +8 -0
  123. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js +23 -0
  124. package/runtime/lib/worklet-runtime/bindings/loadRuntime.js.map +1 -0
  125. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.d.ts +2 -0
  126. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js +6 -0
  127. package/runtime/lib/worklet-runtime/jsFunctionLifecycle.js.map +1 -1
  128. package/runtime/lib/worklet-runtime/workletRuntime.js +2 -0
  129. package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
  130. package/runtime/package.json +2 -3
  131. package/runtime/src/core/capture-main-thread-object.ts +2 -1
  132. package/runtime/src/core/compat/legacy-react-runtime.ts +1 -1
  133. package/runtime/src/core/lynx/lazy-bundle.ts +1 -1
  134. package/runtime/src/core/lynx-data-processors.ts +124 -1
  135. package/runtime/src/core/runtime-destroy.ts +1 -1
  136. package/runtime/src/document.ts +0 -6
  137. package/runtime/src/element-template/background/commit-hook.ts +18 -12
  138. package/runtime/src/element-template/client/root.ts +1 -3
  139. package/runtime/src/element-template/index.ts +2 -2
  140. package/runtime/src/element-template/internal.ts +0 -20
  141. package/runtime/src/element-template/lynx/env.ts +1 -1
  142. package/runtime/src/element-template/native/flush.ts +10 -0
  143. package/runtime/src/element-template/native/main-thread-api.ts +3 -6
  144. package/runtime/src/element-template/native/mts-destroy.ts +7 -2
  145. package/runtime/src/element-template/native/patch-listener.ts +62 -57
  146. package/runtime/src/element-template/native/reload-main-thread.ts +2 -1
  147. package/runtime/src/element-template/protocol/types.ts +0 -13
  148. package/runtime/src/element-template/runtime/page/root-instance.ts +0 -1
  149. package/runtime/src/element-template/runtime/patch.ts +5 -0
  150. package/runtime/src/element-template/runtime/render/render-components.ts +3 -3
  151. package/runtime/src/element-template/runtime/render/render-main-thread.ts +2 -0
  152. package/runtime/src/element-template/runtime/template/main-thread-background-function.ts +8 -1
  153. package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +51 -21
  154. package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +2 -2
  155. package/runtime/src/element-template/types.d.ts +6 -0
  156. package/runtime/src/internal.ts +1 -1
  157. package/runtime/src/lynx-api.ts +2 -124
  158. package/runtime/src/shared/component-stack.ts +22 -5
  159. package/runtime/src/snapshot/lifecycle/patch/snapshotPatch.ts +0 -5
  160. package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +0 -10
  161. package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +62 -58
  162. package/runtime/src/snapshot/list/listUpdateInfo.ts +0 -52
  163. package/runtime/src/snapshot/lynx/calledByNative.ts +11 -9
  164. package/runtime/src/snapshot/renderToOpcodes/hydrate.ts +0 -58
  165. package/runtime/src/snapshot/renderToOpcodes/index.ts +8 -8
  166. package/runtime/src/snapshot/renderToOpcodes/opcodes.ts +0 -57
  167. package/runtime/src/snapshot/snapshot/backgroundSnapshot.ts +1 -1
  168. package/runtime/src/worklet-runtime/README.md +30 -10
  169. package/runtime/src/worklet-runtime/bindings/index.ts +2 -0
  170. package/runtime/src/worklet-runtime/bindings/loadRuntime.ts +25 -0
  171. package/runtime/src/worklet-runtime/jsFunctionLifecycle.ts +6 -0
  172. package/runtime/src/worklet-runtime/workletRuntime.ts +2 -0
  173. package/runtime/vitest.config.ts +0 -2
  174. package/testing-library/.turbo/turbo-build.log +6 -6
  175. package/testing-library/src/__tests__/act.test.jsx +0 -7
  176. package/testing-library/src/__tests__/alog.test.jsx +12 -54
  177. package/testing-library/src/__tests__/end-to-end.test.jsx +0 -7
  178. package/testing-library/src/__tests__/renderComponent.test.jsx +0 -14
  179. package/testing-library/src/__tests__/worklet.test.jsx +49 -0
  180. package/transform/.turbo/turbo-build$colon$wasm.log +98 -376
  181. package/transform/__test__/defines.spec.js +30 -0
  182. package/transform/__test__/object-methods.spec.js +79 -0
  183. package/transform/crates/swc_plugin_worklet/extract_ident.rs +99 -49
  184. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +56 -48
  185. package/transform/crates/swc_plugin_worklet/lib.rs +393 -589
  186. package/transform/crates/swc_plugin_worklet/napi.rs +4 -6
  187. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +6 -1
  188. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +7 -1
  189. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +9 -0
  190. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +12 -1
  191. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +1 -1
  192. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +1 -1
  193. package/transform/dist/wasm.cjs +1 -1
  194. package/transform/package.json +0 -1
  195. package/transform/src/lib.rs +1 -1
  196. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +113 -42
  197. package/transform/swc-plugin-reactlynx/src/lib.rs +1 -4
  198. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  199. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +273 -66
  200. package/runtime/lib/snapshot/gesture/processGestureBagkround.js.map +0 -1
  201. package/runtime/lib/snapshot/worklet/hmr.d.ts +0 -1
  202. package/runtime/lib/snapshot/worklet/hmr.js +0 -28
  203. package/runtime/lib/snapshot/worklet/hmr.js.map +0 -1
  204. package/runtime/src/snapshot/worklet/hmr.ts +0 -34
  205. /package/runtime/lib/snapshot/gesture/{processGestureBagkround.d.ts → processGestureBackground.d.ts} +0 -0
  206. /package/runtime/src/snapshot/gesture/{processGestureBagkround.ts → processGestureBackground.ts} +0 -0
@@ -17,6 +17,7 @@ import { ElementTemplateLifecycleConstant } from '../protocol/lifecycle-constant
17
17
  import type { ElementTemplateUpdateCommitContext } from '../protocol/types.js';
18
18
  import type { ElementTemplateUpdateEvent } from '../protocol/update-event.js';
19
19
  import { applyElementTemplateUpdateCommands } from '../runtime/patch.js';
20
+ import { applyPendingMainThreadRefAttachments } from '../runtime/template/main-thread-dynamic-attr-state.js';
20
21
 
21
22
  let listener:
22
23
  | ((event: Pick<ElementTemplateUpdateEvent, 'data'>) => void)
@@ -38,79 +39,83 @@ export function installElementTemplatePatchListener(): void {
38
39
  });
39
40
  }
40
41
  setPipeline(pipelineOptions);
41
- markTiming('mtsRenderStart');
42
- markTiming('parseChangesStart');
42
+ try {
43
+ markTiming('mtsRenderStart');
44
+ markTiming('parseChangesStart');
43
45
 
44
- const payload = JSON.parse(event.data.payload) as ElementTemplateUpdateCommitContext;
45
- markTiming('parseChangesEnd');
46
+ const payload = JSON.parse(event.data.payload) as ElementTemplateUpdateCommitContext;
47
+ markTiming('parseChangesEnd');
46
48
 
47
- if (
48
- typeof patchOptions.reloadVersion === 'number'
49
- && patchOptions.reloadVersion < getReloadVersion()
50
- ) {
51
- markTiming('mtsRenderEnd');
52
- if (shouldProfilePatch) {
53
- lynx.performance.profileEnd();
49
+ if (
50
+ typeof patchOptions.reloadVersion === 'number'
51
+ && patchOptions.reloadVersion < getReloadVersion()
52
+ ) {
53
+ markTiming('mtsRenderEnd');
54
+ if (shouldProfilePatch) {
55
+ lynx.performance.profileEnd();
56
+ }
57
+ return;
54
58
  }
55
- return;
56
- }
57
59
 
58
- const hasOps = payload.ops.length > 0;
59
- const flushOptions = payload.flushOptions;
60
- const isHydration = payload.isHydration === true;
61
- const delayedRunOnMainThreadData = payload.delayedRunOnMainThreadData;
62
- if (hasOps) {
63
- if (typeof __ALOG__ !== 'undefined' && __ALOG__) {
64
- console.alog?.(
65
- '[ReactLynxDebug] ElementTemplate main-thread patch:\n'
66
- + JSON.stringify(
67
- {
68
- ops: formatElementTemplateUpdateCommands(payload.ops),
69
- flushOptions,
70
- flowIds,
71
- },
72
- null,
73
- 2,
74
- ),
75
- );
76
- }
77
- markTiming('patchChangesStart');
60
+ const hasOps = payload.ops.length > 0;
61
+ const flushOptions = payload.flushOptions;
62
+ const isHydration = payload.isHydration === true;
63
+ const delayedRunOnMainThreadData = payload.delayedRunOnMainThreadData;
78
64
  try {
79
- applyElementTemplateUpdateCommands(payload.ops, isHydration);
65
+ if (hasOps) {
66
+ if (typeof __ALOG__ !== 'undefined' && __ALOG__) {
67
+ console.alog?.(
68
+ '[ReactLynxDebug] ElementTemplate main-thread patch:\n'
69
+ + JSON.stringify(
70
+ {
71
+ ops: formatElementTemplateUpdateCommands(payload.ops),
72
+ flushOptions,
73
+ flowIds,
74
+ },
75
+ null,
76
+ 2,
77
+ ),
78
+ );
79
+ }
80
+ markTiming('patchChangesStart');
81
+ try {
82
+ applyElementTemplateUpdateCommands(payload.ops, isHydration);
83
+ } finally {
84
+ markTiming('patchChangesEnd');
85
+ markTiming('mtsRenderEnd');
86
+ }
87
+ } else {
88
+ markTiming('mtsRenderEnd');
89
+ }
90
+ applyPendingMainThreadRefAttachments();
80
91
  } finally {
81
- markTiming('patchChangesEnd');
82
- markTiming('mtsRenderEnd');
83
92
  if (isHydration) {
84
93
  flushDelayedRunOnBackgroundFunctions();
85
94
  clearFirstScreenMainThreadRefs();
86
95
  }
87
96
  }
88
- } else {
89
- markTiming('mtsRenderEnd');
90
- if (isHydration) {
91
- flushDelayedRunOnBackgroundFunctions();
92
- clearFirstScreenMainThreadRefs();
93
- }
94
- }
95
- if (delayedRunOnMainThreadData?.length) {
96
- setEomShouldFlushElementTree(false);
97
- try {
98
- for (const data of delayedRunOnMainThreadData) {
99
- try {
100
- runRunOnMainThreadTask(data.worklet, data.params as ClosureValueType[], data.resolveId);
101
- } catch (error) {
102
- lynx.reportError(error as Error);
97
+ if (delayedRunOnMainThreadData?.length) {
98
+ setEomShouldFlushElementTree(false);
99
+ try {
100
+ for (const data of delayedRunOnMainThreadData) {
101
+ try {
102
+ runRunOnMainThreadTask(data.worklet, data.params as ClosureValueType[], data.resolveId);
103
+ } catch (error) {
104
+ lynx.reportError(error as Error);
105
+ }
103
106
  }
107
+ } finally {
108
+ setEomShouldFlushElementTree(true);
104
109
  }
105
- } finally {
106
- setEomShouldFlushElementTree(true);
107
110
  }
108
- }
109
111
 
110
- __FlushElementTree(undefined, flushOptions);
112
+ __FlushElementTree(undefined, flushOptions);
111
113
 
112
- if (shouldProfilePatch) {
113
- lynx.performance.profileEnd();
114
+ if (shouldProfilePatch) {
115
+ lynx.performance.profileEnd();
116
+ }
117
+ } finally {
118
+ setPipeline(undefined);
114
119
  }
115
120
  };
116
121
 
@@ -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
 
5
+ import { flushElementTemplate } from './flush.js';
5
6
  import { onMtsDestruction } from './mts-destroy.js';
6
7
  import {
7
8
  canReEvalMainThreadEntry,
@@ -66,7 +67,7 @@ export function reloadMainThread(data: unknown, options: UpdatePageOption): void
66
67
  setRoot({ __jsx: oldRoot.__jsx });
67
68
  renderMainThread();
68
69
 
69
- __FlushElementTree(undefined, options);
70
+ flushElementTemplate(options);
70
71
  } finally {
71
72
  if (typeof __PROFILE__ !== 'undefined' && __PROFILE__) {
72
73
  profileEnd();
@@ -199,20 +199,7 @@ export type ElementTemplateUpdateCommand =
199
199
  export type ElementTemplateUpdateCommandStream = ElementTemplateUpdateCommand[number][];
200
200
 
201
201
  export interface ElementTemplateFlushOptions {
202
- // triggerLayout?: boolean;
203
- // operationID?: any;
204
- // __lynx_timing_flag?: string;
205
- // nativeUpdateDataOrder?: number;
206
- // elementID?: number;
207
- // listID?: number;
208
- // listReuseNotification?: {
209
- // listElement: FiberElement;
210
- // itemKey: string;
211
- // };
212
202
  pipelineOptions?: PipelineOptions;
213
- // elementIDs?: number[];
214
- // operationIDs?: any[];
215
- // asyncFlush?: boolean;
216
203
  triggerDataUpdated?: boolean;
217
204
  emptyPatch?: boolean;
218
205
  }
@@ -7,7 +7,6 @@
7
7
  */
8
8
  let __root: {
9
9
  __jsx?: React.ReactNode;
10
- __opcodes?: any[];
11
10
  nodeType?: Element['nodeType'];
12
11
  };
13
12
 
@@ -16,6 +16,8 @@ import {
16
16
  } from './list/list.js';
17
17
  import type { ETListFlushResult, ETListUpdateItem } from './list/list.js';
18
18
  import { insertElementTemplateSubtree } from './template/handle.js';
19
+ import { retainMainThreadBackgroundFunctionCtx } from './template/main-thread-background-function.js';
20
+ import type { MTEventNativeWrapper } from './template/main-thread-event-ctx.js';
19
21
  import { elementTemplateRegistry } from './template/registry.js';
20
22
  import { TYPED_ELEMENT_ATTRIBUTES_SLOT_INDEX } from './template/typed-attributes.js';
21
23
  import { ElementTemplateUpdateOps } from '../protocol/opcodes.js';
@@ -138,6 +140,9 @@ export function applyElementTemplateUpdateCommands(
138
140
  if (!nativeRef) {
139
141
  continue;
140
142
  }
143
+ if (value != null) {
144
+ retainMainThreadBackgroundFunctionCtx((value as unknown as MTEventNativeWrapper).value);
145
+ }
141
146
  __SetAttributeOfElementTemplate(nativeRef, attrSlotIndex, value);
142
147
  updateMainThreadEventAttrSlot(targetId, attrSlotIndex, value, isHydration);
143
148
  break;
@@ -40,7 +40,7 @@ const assign = /* @__PURE__ */ Object.assign;
40
40
  // Global state for the current render pass
41
41
  export let beforeDiff: ((vnode: unknown) => void) | undefined;
42
42
  export let beforeDiff2: ((vnode: unknown, context: Record<string, unknown>) => void) | undefined;
43
- let afterDiff, renderHook, ummountHook;
43
+ let afterDiff, renderHook, unmountHook;
44
44
 
45
45
  export function renderWithHooks(
46
46
  vnode: unknown,
@@ -62,7 +62,7 @@ export function renderWithHooks(
62
62
  beforeDiff2 = options[DIFF2];
63
63
  afterDiff = options[DIFFED];
64
64
  renderHook = options[RENDER];
65
- ummountHook = options.unmount;
65
+ unmountHook = options.unmount;
66
66
 
67
67
  const parent = h(Fragment, null);
68
68
  parent[CHILDREN] = [vnode];
@@ -141,7 +141,7 @@ function renderClassComponent(vnode, context, globalContext) {
141
141
  export function cleanupVNode(vnode: unknown): void {
142
142
  if (afterDiff) afterDiff(vnode);
143
143
  vnode[PARENT] = undefined;
144
- if (ummountHook) ummountHook(vnode);
144
+ if (unmountHook) unmountHook(vnode);
145
145
  }
146
146
 
147
147
  export function renderComponentVNode(
@@ -16,6 +16,7 @@ import { flushInitialElementTemplateListUpdates } from '../list/list.js';
16
16
  import { __page } from '../page/page.js';
17
17
  import { __root } from '../page/root-instance.js';
18
18
  import { insertElementTemplateSubtree } from '../template/handle.js';
19
+ import { applyPendingMainThreadRefAttachments } from '../template/main-thread-dynamic-attr-state.js';
19
20
  import { getElementTemplateNativeRef } from '../template/registry.js';
20
21
  import { TYPED_ELEMENT_ATTRIBUTES_SLOT_INDEX } from '../template/typed-attributes.js';
21
22
 
@@ -48,6 +49,7 @@ function renderMainThread(): void {
48
49
  );
49
50
  }
50
51
  flushInitialListUpdates();
52
+ applyPendingMainThreadRefAttachments();
51
53
  } finally {
52
54
  profileEnd();
53
55
  }
@@ -11,7 +11,7 @@ import {
11
11
  runOnBackground,
12
12
  } from '../../../core/background-function/run-on-background.js';
13
13
  import { resetFunctionCallReturnListener } from '../../../core/thread-function-call/return-value.js';
14
- import { isRunOnBackgroundEnabled } from '../../../worklet-runtime/jsFunctionLifecycle.js';
14
+ import { JsFunctionLifecycleManager, isRunOnBackgroundEnabled } from '../../../worklet-runtime/jsFunctionLifecycle.js';
15
15
 
16
16
  type BackgroundFunctionCtx = Parameters<typeof registerBackgroundFunctionCtx>[0];
17
17
  type RetainableMainThreadCtx = Parameters<typeof retainWorkletCtx>[0];
@@ -33,6 +33,8 @@ export function registerMainThreadBackgroundFunctionCtx(ctx: MainThreadBackgroun
33
33
  }
34
34
  }
35
35
 
36
+ // Track the input before Native takes ownership. If Native rejects it, collection
37
+ // must still release its already-registered background execution; slot state commits later.
36
38
  export function retainMainThreadBackgroundFunctionCtx(ctx: MainThreadBackgroundFunctionCtx): void {
37
39
  retainWorkletCtx(ctx as RetainableMainThreadCtx);
38
40
  }
@@ -43,6 +45,11 @@ export function resetElementTemplateBackgroundFunctionRuntime(): void {
43
45
  }
44
46
 
45
47
  export function resetElementTemplateMainThreadBackgroundFunctionRuntime(): void {
48
+ const manager = globalThis.lynxWorkletImpl?._jsFunctionLifecycleManager;
49
+ if (manager) {
50
+ manager.dispose();
51
+ globalThis.lynxWorkletImpl._jsFunctionLifecycleManager = new JsFunctionLifecycleManager();
52
+ }
46
53
  clearDelayedRunOnBackgroundFunctions();
47
54
  resetFunctionCallReturnListener();
48
55
  }
@@ -20,7 +20,16 @@ type MainThreadDynamicAttrStateMap<T> = Map<number, Map<number, T>>;
20
20
 
21
21
  const mtEventAttrState: MainThreadDynamicAttrStateMap<MTEventCtx> = new Map();
22
22
  const mtRefAttrState: MainThreadDynamicAttrStateMap<MTRefValue> = new Map();
23
- const materializedMTRefHandleIds = new Set<number>();
23
+ const attachedMTRefHandleIds = new Set<number>();
24
+
25
+ interface PendingMTRefAttachment {
26
+ handleId: number;
27
+ attrSlotIndex: number;
28
+ value: MTRefValue;
29
+ nativeRef: ElementTemplateHandle;
30
+ }
31
+
32
+ let pendingMTRefAttachments: PendingMTRefAttachment[] = [];
24
33
 
25
34
  export interface MainThreadDynamicAttrSubtreeHandle {
26
35
  uid: number;
@@ -63,7 +72,6 @@ function setSlotState<T>(
63
72
  }
64
73
 
65
74
  function setMTEventSlotState(handleId: number, attrSlotIndex: number, nativeHeldValue: MTEventCtx): void {
66
- retainMainThreadBackgroundFunctionCtx(nativeHeldValue);
67
75
  setSlotState(mtEventAttrState, handleId, attrSlotIndex, nativeHeldValue);
68
76
  }
69
77
 
@@ -89,6 +97,9 @@ export function prepareMainThreadDynamicAttrSlotsForNative(
89
97
  }
90
98
  let nativeSlots: SerializableValue[] | undefined;
91
99
  for (const [attrSlotIndex, kind] of slotKinds) {
100
+ if (kind === 'mt-event' && attributeSlots[attrSlotIndex] != null) {
101
+ retainMainThreadBackgroundFunctionCtx((attributeSlots[attrSlotIndex] as MTEventNativeWrapper).value);
102
+ }
92
103
  if (kind !== 'mt-ref') {
93
104
  continue;
94
105
  }
@@ -99,14 +110,18 @@ export function prepareMainThreadDynamicAttrSlotsForNative(
99
110
  }
100
111
 
101
112
  export function clearMainThreadDynamicAttrState(): void {
102
- for (const [handleId, handleState] of mtRefAttrState) {
103
- if (materializedMTRefHandleIds.has(handleId)) {
104
- cleanupMTRefHandleState(handleState);
113
+ try {
114
+ for (const [handleId, handleState] of mtRefAttrState) {
115
+ if (attachedMTRefHandleIds.has(handleId)) {
116
+ cleanupMTRefHandleState(handleState);
117
+ }
105
118
  }
119
+ } finally {
120
+ mtEventAttrState.clear();
121
+ mtRefAttrState.clear();
122
+ attachedMTRefHandleIds.clear();
123
+ pendingMTRefAttachments = [];
106
124
  }
107
- mtEventAttrState.clear();
108
- mtRefAttrState.clear();
109
- materializedMTRefHandleIds.clear();
110
125
  }
111
126
 
112
127
  export function getMainThreadDynamicAttrState(
@@ -171,15 +186,15 @@ export function updateMainThreadRefAttrSlot(
171
186
  isHydration = false,
172
187
  ): void {
173
188
  const previousState = mtRefAttrState.get(handleId)?.get(attrSlotIndex);
174
- const isMaterialized = materializedMTRefHandleIds.has(handleId);
189
+ const isAttached = attachedMTRefHandleIds.has(handleId);
175
190
  if (value == null) {
176
- if (previousState && isMaterialized) {
191
+ if (previousState && isAttached) {
177
192
  cleanupMTRefValue(previousState);
178
193
  }
179
194
  deleteSlotState(mtRefAttrState, handleId, attrSlotIndex);
180
195
  return;
181
196
  }
182
- if (previousState && isMaterialized) {
197
+ if (previousState && isAttached) {
183
198
  cleanupMTRefValue(previousState);
184
199
  }
185
200
  const nextValue = (value as MTRefNativeWrapper).value;
@@ -187,8 +202,8 @@ export function updateMainThreadRefAttrSlot(
187
202
  if (isHydration) {
188
203
  hydrateMTRefValue(nextValue, previousState);
189
204
  }
190
- if (isMaterialized) {
191
- attachMTRefValue(nextValue, nativeRef);
205
+ if (isAttached) {
206
+ pendingMTRefAttachments.push({ handleId, attrSlotIndex, value: nextValue, nativeRef });
192
207
  }
193
208
  }
194
209
 
@@ -197,17 +212,32 @@ export function attachMainThreadDynamicAttrRefsForSubtree(
197
212
  ): void {
198
213
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
199
214
  const handle = handles[handleIndex]!;
200
- if (materializedMTRefHandleIds.has(handle.uid)) {
215
+ if (attachedMTRefHandleIds.has(handle.uid)) {
201
216
  continue;
202
217
  }
203
- materializedMTRefHandleIds.add(handle.uid);
218
+ attachedMTRefHandleIds.add(handle.uid);
204
219
  const handleState = mtRefAttrState.get(handle.uid);
205
220
  if (!handleState) {
206
221
  continue;
207
222
  }
208
- for (const value of handleState.values()) {
209
- attachMTRefValue(value, handle.ref);
223
+ for (const [attrSlotIndex, value] of handleState) {
224
+ pendingMTRefAttachments.push({ handleId: handle.uid, attrSlotIndex, value, nativeRef: handle.ref });
225
+ }
226
+ }
227
+ }
228
+
229
+ // Consume one queue snapshot, matching Snapshot's ref delivery boundary.
230
+ export function applyPendingMainThreadRefAttachments(): void {
231
+ const pending = pendingMTRefAttachments;
232
+ pendingMTRefAttachments = [];
233
+ for (const { handleId, attrSlotIndex, value, nativeRef } of pending) {
234
+ const element = __GetElementByAttrSlot(nativeRef, attrSlotIndex);
235
+ if (!element) {
236
+ throw new Error(
237
+ `ElementTemplate main-thread:ref target ${handleId}:${attrSlotIndex} could not be materialized.`,
238
+ );
210
239
  }
240
+ attachMTRefValue(value, element);
211
241
  }
212
242
  }
213
243
 
@@ -216,10 +246,10 @@ export function detachMainThreadDynamicAttrRefsForSubtree(
216
246
  ): void {
217
247
  for (let handleIndex = 0; handleIndex < handles.length; handleIndex += 1) {
218
248
  const handleId = handles[handleIndex]!.uid;
219
- if (!materializedMTRefHandleIds.has(handleId)) {
249
+ if (!attachedMTRefHandleIds.has(handleId)) {
220
250
  continue;
221
251
  }
222
- materializedMTRefHandleIds.delete(handleId);
252
+ attachedMTRefHandleIds.delete(handleId);
223
253
  const handleState = mtRefAttrState.get(handleId);
224
254
  if (handleState) {
225
255
  cleanupMTRefHandleState(handleState);
@@ -232,10 +262,10 @@ export function deleteMainThreadDynamicAttrStateForSubtree(
232
262
  ): void {
233
263
  for (const handleId of handleIds) {
234
264
  const handleState = mtRefAttrState.get(handleId);
235
- if (handleState && materializedMTRefHandleIds.has(handleId)) {
265
+ if (handleState && attachedMTRefHandleIds.has(handleId)) {
236
266
  cleanupMTRefHandleState(handleState);
237
267
  }
238
- materializedMTRefHandleIds.delete(handleId);
268
+ attachedMTRefHandleIds.delete(handleId);
239
269
  mtEventAttrState.delete(handleId);
240
270
  mtRefAttrState.delete(handleId);
241
271
  }
@@ -41,10 +41,10 @@ export function hydrateMTRefValue(
41
41
 
42
42
  export function attachMTRefValue(
43
43
  value: MTRefValue,
44
- nativeRef: ElementTemplateHandle,
44
+ nativeRef: ElementRef,
45
45
  ): void {
46
46
  if (isMainThreadRef(value)) {
47
- updateWorkletRef(value, nativeRef as unknown as ElementNode);
47
+ updateWorkletRef(value, nativeRef);
48
48
  return;
49
49
  }
50
50
  value._unmount = runWorkletCtx(
@@ -37,6 +37,12 @@ declare global {
37
37
  // explicit flush options. It is not an Element Template handle.
38
38
  function __FlushElementTree(element: undefined, options: FlushOptions): void;
39
39
 
40
+ // Materializes the instance on demand; parent mounting and layout may still be pending.
41
+ function __GetElementByAttrSlot(
42
+ templateInstance: ElementTemplateHandle,
43
+ attrSlotIndex: number,
44
+ ): ElementRef | null;
45
+
40
46
  function __CreateElementTemplate(
41
47
  templateKey: string,
42
48
  bundleUrl: string | null | undefined,
@@ -98,8 +98,8 @@ export const __ComponentIsPolyfill: FC<{ is: string }> = /* @__PURE__ */ factory
98
98
  export { loadLazyBundle } from './core/lynx/lazy-bundle.js';
99
99
 
100
100
  export { createBackgroundFunctionHandle as transformToWorklet } from './core/background-function/handle.js';
101
- export { registerWorkletOnBackground } from './snapshot/worklet/hmr.js';
102
101
 
102
+ export { loadWorkletRuntime } from '@lynx-js/react/worklet-runtime/bindings';
103
103
  export { captureMainThreadObject } from './core/capture-main-thread-object.js';
104
104
 
105
105
  if (__DEV__) {
@@ -10,6 +10,7 @@ import { createGlobalProps } from './core/globalProps.js';
10
10
  import type { GlobalProps } from './core/globalProps.js';
11
11
  import { useLynxGlobalEventListener } from './core/hooks/useLynxGlobalEventListener.js';
12
12
  import { factory, withInitDataInState } from './core/initData.js';
13
+ import type { DataProcessorDefinition, DataProcessors, InitData, InitDataRaw } from './core/lynx-data-processors.js';
13
14
  import { __root } from './root.js';
14
15
  import { profileEnd, profileStart } from './shared/profile.js';
15
16
  import { LifecycleConstant } from './snapshot/lifecycle/constant.js';
@@ -338,132 +339,9 @@ export const useGlobalProps: () => GlobalProps = /* @__PURE__ */ _GlobalProps.us
338
339
  export const useGlobalPropsChanged: (callback: (data: GlobalProps) => void) => void = /* @__PURE__ */ _GlobalProps
339
340
  .useChanged();
340
341
 
341
- /**
342
- * The interface you can extends so that the `defaultDataProcessor` parameter can be customized
343
- *
344
- * Should be used with `lynx.registerDataProcessors`. See more examples at {@link Lynx.registerDataProcessors}.
345
- *
346
- * @public
347
- *
348
- * @group Types
349
- */
350
- export interface InitDataRaw {}
351
- /**
352
- * The interface you can extends so that the `defaultDataProcessor` returning value can be customized
353
- *
354
- * Should be used with `lynx.registerDataProcessors`. See more examples at {@link Lynx.registerDataProcessors}.
355
- *
356
- * @public
357
- *
358
- * @group Types
359
- */
360
- export interface InitData {}
361
-
342
+ export type { DataProcessorDefinition, DataProcessors, InitData, InitDataRaw };
362
343
  export { withInitDataInState };
363
344
 
364
- /**
365
- * The data processors that registered with {@link Lynx.registerDataProcessors}.
366
- *
367
- * @example
368
- *
369
- * Extending `dataProcessors` interface
370
- *
371
- * ```ts
372
- * import type { DataProcessors as WellKnownDataProcessors } from '@lynx-js/react';
373
- *
374
- * declare module '@lynx-js/react' {
375
- * interface DataProcessors extends WellKnownDataProcessors {
376
- * foo(bar: string): number;
377
- * }
378
- * }
379
- * ```
380
- *
381
- * Then you can use `lynx.registerDataProcessors` with types.
382
- *
383
- * ```js
384
- * lynx.registerDataProcessors({
385
- * dataProcessors: {
386
- * foo(bar) {
387
- * return 1;
388
- * }
389
- * }
390
- * })
391
- * ```
392
- *
393
- * @public
394
- *
395
- * @group Types
396
- */
397
- export interface DataProcessors {
398
- /**
399
- * Optional processor to override screen metrics used by the app
400
- *
401
- * @param metrics - The physical screen dimensions in pixels
402
- *
403
- * @returns New screen dimensions to be used by the app
404
- *
405
- * @example
406
- *
407
- * ```ts
408
- * lynx.registerDataProcessors({
409
- * dataProcessors: {
410
- * getScreenMetricsOverride: (metrics) => {
411
- * // Force a specific aspect ratio
412
- * return {
413
- * width: metrics.width,
414
- * height: metrics.width * (16/9)
415
- * };
416
- * }
417
- * }
418
- * });
419
- * ```
420
- */
421
- getScreenMetricsOverride?(metrics: {
422
- /**
423
- * The physical pixel width of the screen
424
- */
425
- width: number;
426
- /**
427
- * The physical pixel height of the screen
428
- */
429
- height: number;
430
- }): { width: number; height: number };
431
-
432
- /**
433
- * Custom unknown data processors.
434
- *
435
- * @remarks
436
- *
437
- * You may extends the `DataProcessors` interface for better TypeScript types. See {@link DataProcessors}.
438
- */
439
- [processorName: string]: (...args: any[]) => any;
440
- }
441
-
442
- /**
443
- * Definition of DataProcessor(s)
444
- * @public
445
- *
446
- * @group Types
447
- */
448
- export interface DataProcessorDefinition {
449
- /**
450
- * You can custom input and output type of `defaultDataProcessor` by extends {@link InitDataRaw} and {@link InitData}
451
- *
452
- * Should be used with `lynx.registerDataProcessors`. See more examples at {@link Lynx.registerDataProcessors}.
453
- *
454
- * @param rawInitData - initData passed from native code
455
- * @returns
456
- * @public
457
- */
458
- defaultDataProcessor?: (rawInitData: InitDataRaw) => InitData;
459
- /**
460
- * Should be used with `lynx.registerDataProcessors`. See more examples at {@link Lynx.registerDataProcessors}.
461
- *
462
- * @public
463
- */
464
- dataProcessors?: DataProcessors;
465
- }
466
-
467
345
  /**
468
346
  * APIs under `lynx` global variable that added by ReactLynx.
469
347
  *