@lynx-js/react 0.129.1 → 0.129.3

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 (129) hide show
  1. package/CHANGELOG.md +18 -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/globalProps.test.ts +27 -6
  6. package/runtime/__test__/element-template/fixtures/gesture/direct/index.tsx +33 -0
  7. package/runtime/__test__/element-template/fixtures/spread/mts/index.tsx +197 -0
  8. package/runtime/__test__/element-template/native/callDestroyLifetimeFun.test.ts +89 -3
  9. package/runtime/__test__/element-template/native/mts-destroy.test.ts +8 -3
  10. package/runtime/__test__/element-template/native/reload.test.ts +87 -0
  11. package/runtime/__test__/element-template/runtime/background/global-props/compiled-fixtures.test.tsx +5 -5
  12. package/runtime/__test__/element-template/runtime/background/global-props.test.tsx +1 -1
  13. package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +70 -0
  14. package/runtime/__test__/element-template/runtime/background/ref/compiled-fixtures.test.tsx +3 -4
  15. package/runtime/__test__/element-template/runtime/background/reload.test.ts +74 -0
  16. package/runtime/__test__/element-template/runtime/patch/element-template-patch.test.tsx +15 -0
  17. package/runtime/__test__/element-template/runtime/prop-adapters/gesture-compiled.test.ts +200 -0
  18. package/runtime/__test__/element-template/runtime/prop-adapters/gesture.test.ts +420 -0
  19. package/runtime/__test__/element-template/runtime/prop-adapters/spread-mts-compiled.test.ts +527 -0
  20. package/runtime/__test__/element-template/runtime/prop-adapters/spread.test.ts +185 -6
  21. package/runtime/__test__/element-template/runtime/render/render-direct-native.test.tsx +21 -1
  22. package/runtime/__test__/element-template/runtime/template/main-thread-dynamic-attr-state.test.ts +188 -0
  23. package/runtime/__test__/snapshot/compat/export.test.jsx +8 -2
  24. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.event.test.jsx +2 -2
  25. package/runtime/__test__/snapshot/lifecycle/updateGlobalProps.test.jsx +3 -3
  26. package/runtime/__test__/snapshot/ref.test.jsx +30 -0
  27. package/runtime/__test__/worklet-runtime/initEntry.test.js +12 -0
  28. package/runtime/compat/index.d.ts +17 -1
  29. package/runtime/compat/index.js +3 -2
  30. package/runtime/lazy/compat.js +1 -0
  31. package/runtime/lazy/element-template-internal.js +1 -0
  32. package/runtime/lib/core/globalProps.js +1 -1
  33. package/runtime/lib/core/globalProps.js.map +1 -1
  34. package/runtime/lib/element-template/background/attr-slots.js +5 -2
  35. package/runtime/lib/element-template/background/attr-slots.js.map +1 -1
  36. package/runtime/lib/element-template/background/destroy.js +16 -11
  37. package/runtime/lib/element-template/background/destroy.js.map +1 -1
  38. package/runtime/lib/element-template/background/hydrate.js +23 -0
  39. package/runtime/lib/element-template/background/hydrate.js.map +1 -1
  40. package/runtime/lib/element-template/debug/alog.d.ts +1 -1
  41. package/runtime/lib/element-template/debug/alog.js +4 -1
  42. package/runtime/lib/element-template/debug/alog.js.map +1 -1
  43. package/runtime/lib/element-template/internal.d.ts +1 -1
  44. package/runtime/lib/element-template/internal.js +1 -1
  45. package/runtime/lib/element-template/internal.js.map +1 -1
  46. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js +6 -1
  47. package/runtime/lib/element-template/native/callDestroyLifetimeFun.js.map +1 -1
  48. package/runtime/lib/element-template/native/patch-listener.js +1 -1
  49. package/runtime/lib/element-template/native/patch-listener.js.map +1 -1
  50. package/runtime/lib/element-template/native/reload-background.js +1 -1
  51. package/runtime/lib/element-template/native/reload-background.js.map +1 -1
  52. package/runtime/lib/element-template/native/reload-main-thread.js +33 -9
  53. package/runtime/lib/element-template/native/reload-main-thread.js.map +1 -1
  54. package/runtime/lib/element-template/prop-adapters/gesture.d.ts +23 -0
  55. package/runtime/lib/element-template/prop-adapters/gesture.js +97 -0
  56. package/runtime/lib/element-template/prop-adapters/gesture.js.map +1 -0
  57. package/runtime/lib/element-template/prop-adapters/spread.d.ts +6 -1
  58. package/runtime/lib/element-template/prop-adapters/spread.js +72 -2
  59. package/runtime/lib/element-template/prop-adapters/spread.js.map +1 -1
  60. package/runtime/lib/element-template/protocol/opcodes.d.ts +1 -0
  61. package/runtime/lib/element-template/protocol/opcodes.js +1 -0
  62. package/runtime/lib/element-template/protocol/opcodes.js.map +1 -1
  63. package/runtime/lib/element-template/protocol/types.d.ts +7 -1
  64. package/runtime/lib/element-template/protocol/types.js.map +1 -1
  65. package/runtime/lib/element-template/runtime/patch.js +27 -2
  66. package/runtime/lib/element-template/runtime/patch.js.map +1 -1
  67. package/runtime/lib/element-template/runtime/template/attr-slot-plan.d.ts +4 -2
  68. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js +17 -4
  69. package/runtime/lib/element-template/runtime/template/attr-slot-plan.js.map +1 -1
  70. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.d.ts +7 -2
  71. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js +368 -58
  72. package/runtime/lib/element-template/runtime/template/main-thread-dynamic-attr-state.js.map +1 -1
  73. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.d.ts +2 -1
  74. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js +10 -1
  75. package/runtime/lib/element-template/runtime/template/main-thread-ref-ctx.js.map +1 -1
  76. package/runtime/lib/element-template/runtime/template/typed-attributes.js +3 -3
  77. package/runtime/lib/element-template/runtime/template/typed-attributes.js.map +1 -1
  78. package/runtime/lib/snapshot/lifecycle/destroy.js +23 -17
  79. package/runtime/lib/snapshot/lifecycle/destroy.js.map +1 -1
  80. package/runtime/lib/snapshot/lifecycle/ref/delay.d.ts +5 -0
  81. package/runtime/lib/snapshot/lifecycle/ref/delay.js +1 -1
  82. package/runtime/lib/snapshot/lifecycle/ref/delay.js.map +1 -1
  83. package/runtime/lib/tsconfig.tsbuildinfo +1 -1
  84. package/runtime/lib/worklet-runtime/utils/version.js +4 -1
  85. package/runtime/lib/worklet-runtime/utils/version.js.map +1 -1
  86. package/runtime/src/core/globalProps.ts +1 -1
  87. package/runtime/src/element-template/background/attr-slots.ts +5 -1
  88. package/runtime/src/element-template/background/destroy.ts +16 -13
  89. package/runtime/src/element-template/background/hydrate.ts +23 -0
  90. package/runtime/src/element-template/debug/alog.ts +5 -2
  91. package/runtime/src/element-template/internal.ts +1 -0
  92. package/runtime/src/element-template/native/callDestroyLifetimeFun.ts +5 -1
  93. package/runtime/src/element-template/native/patch-listener.ts +1 -1
  94. package/runtime/src/element-template/native/reload-background.ts +1 -1
  95. package/runtime/src/element-template/native/reload-main-thread.ts +30 -10
  96. package/runtime/src/element-template/prop-adapters/gesture.ts +144 -0
  97. package/runtime/src/element-template/prop-adapters/spread.ts +110 -2
  98. package/runtime/src/element-template/protocol/opcodes.ts +1 -0
  99. package/runtime/src/element-template/protocol/types.ts +8 -0
  100. package/runtime/src/element-template/runtime/patch.ts +33 -0
  101. package/runtime/src/element-template/runtime/template/attr-slot-plan.ts +31 -4
  102. package/runtime/src/element-template/runtime/template/main-thread-dynamic-attr-state.ts +441 -65
  103. package/runtime/src/element-template/runtime/template/main-thread-ref-ctx.ts +12 -2
  104. package/runtime/src/element-template/runtime/template/typed-attributes.ts +3 -3
  105. package/runtime/src/snapshot/lifecycle/destroy.ts +23 -18
  106. package/runtime/src/snapshot/lifecycle/ref/delay.ts +1 -1
  107. package/runtime/src/worklet-runtime/utils/version.ts +4 -1
  108. package/runtime/types/types.d.ts +2 -1
  109. package/testing-library/.turbo/turbo-build.log +7 -7
  110. package/transform/.turbo/turbo-build$colon$wasm.log +72 -71
  111. package/transform/__test__/fixture.spec.js +11 -11
  112. package/transform/__test__/main-thread-object-compat.spec.js +111 -0
  113. package/transform/crates/swc_plugin_element_template/lib.rs +11 -0
  114. package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +113 -0
  115. package/transform/crates/swc_plugin_worklet/gen_stmt.rs +7 -9
  116. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +3 -3
  117. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +3 -3
  118. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +7 -7
  119. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +7 -7
  120. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +8 -8
  121. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +8 -8
  122. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +5 -5
  123. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +5 -5
  124. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +1 -1
  125. package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +1 -1
  126. package/transform/dist/wasm.cjs +1 -1
  127. package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +97 -97
  128. package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
  129. package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +177 -178
@@ -2,7 +2,10 @@
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
  export function isSdkVersionGt(major, minor) {
5
- const lynxSdkVersion = SystemInfo.lynxSdkVersion ?? '1.0';
5
+ // Worklet initialization can run before ReactLynx installs the global alias.
6
+ const lynxSdkVersion = globalThis.SystemInfo?.lynxSdkVersion
7
+ ?? lynx.SystemInfo?.lynxSdkVersion
8
+ ?? '1.0';
6
9
  const version = lynxSdkVersion.split('.');
7
10
  return (Number(version[0]) > major
8
11
  || (Number(version[0]) == major && Number(version[1]) > minor));
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","sourceRoot":"","sources":["../../../src/worklet-runtime/utils/version.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,KAAa;IACzD,MAAM,cAAc,GAAW,UAAU,CAAC,cAAc,IAAI,KAAK,CAAC;IAClE,MAAM,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC1C,OAAO,CACL,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK;WACvB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAC/D,CAAC;AACJ,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\nexport function isSdkVersionGt(major: number, minor: number): boolean {\n const lynxSdkVersion: string = SystemInfo.lynxSdkVersion ?? '1.0';\n const version = lynxSdkVersion.split('.');\n return (\n Number(version[0]) > major\n || (Number(version[0]) == major && Number(version[1]) > minor)\n );\n}\n"]}
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../../../src/worklet-runtime/utils/version.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,yEAAyE;AACzE,0DAA0D;AAC1D,MAAM,UAAU,cAAc,CAAC,KAAa,EAAE,KAAa;IACzD,6EAA6E;IAC7E,MAAM,cAAc,GAAI,UAA+D,CAAC,UAAU,EAAE,cAAc;WAC5G,IAAmD,CAAC,UAAU,EAAE,cAAc;WAC/E,KAAK,CAAC;IACX,MAAM,OAAO,GAAG,cAAc,CAAC,KAAK,CAAC,GAAG,CAAC,CAAC;IAC1C,OAAO,CACL,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK;WACvB,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,IAAI,KAAK,IAAI,MAAM,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,GAAG,KAAK,CAAC,CAC/D,CAAC;AACJ,CAAC","sourcesContent":["// Copyright 2024 The Lynx Authors. All rights reserved.\n// Licensed under the Apache License Version 2.0 that can be found in the\n// LICENSE file in the root directory of this source tree.\nexport function isSdkVersionGt(major: number, minor: number): boolean {\n // Worklet initialization can run before ReactLynx installs the global alias.\n const lynxSdkVersion = (globalThis as typeof globalThis & { SystemInfo?: ISystemInfo }).SystemInfo?.lynxSdkVersion\n ?? (lynx as typeof lynx & { SystemInfo?: ISystemInfo }).SystemInfo?.lynxSdkVersion\n ?? '1.0';\n const version = lynxSdkVersion.split('.');\n return (\n Number(version[0]) > major\n || (Number(version[0]) == major && Number(version[1]) > minor)\n );\n}\n"]}
@@ -38,7 +38,7 @@ export interface UpdateGlobalPropsOptions {
38
38
  }
39
39
 
40
40
  export function isGlobalPropsEventMode(): boolean {
41
- return typeof __GLOBAL_PROPS_MODE__ !== 'undefined' && __GLOBAL_PROPS_MODE__ === 'event';
41
+ return lynx.__runtime_configs__?.globalPropsMode === 'event';
42
42
  }
43
43
 
44
44
  export function createGlobalProps<Data = GlobalProps>(
@@ -10,6 +10,7 @@ import {
10
10
  __etAttrPlanMap,
11
11
  adaptRefAttrSlot,
12
12
  adaptSpreadAttrSlot,
13
+ adaptTypedSpreadAttrSlot,
13
14
  getMainThreadDynamicAttrSlotKinds,
14
15
  } from '../runtime/template/attr-slot-plan.js';
15
16
  import type { EtAttrAdapter, EtAttrAdapterContext, EtAttrPlan } from '../runtime/template/attr-slot-plan.js';
@@ -53,7 +54,7 @@ function queuePlannedRefAttributeSlotUpdates(
53
54
  continue;
54
55
  }
55
56
 
56
- if (adapter === adaptSpreadAttrSlot) {
57
+ if (adapter === adaptSpreadAttrSlot || adapter === adaptTypedSpreadAttrSlot) {
57
58
  const previousSpreadRef = getSpreadRefFromValue(previousRawSlots?.[attrSlotIndex]);
58
59
  const nextSpreadRef = getSpreadRefFromValue(nextRawSlots?.[attrSlotIndex]);
59
60
  if (previousSpreadRef === undefined && nextSpreadRef === undefined) {
@@ -122,6 +123,9 @@ export function getAttributeSlotUpdateOp(
122
123
  attrSlotIndex: number,
123
124
  ): ElementTemplateUpdateOp {
124
125
  const kind = getMainThreadDynamicAttrSlotKinds(templateType)?.get(attrSlotIndex);
126
+ if (kind === 'gesture') {
127
+ return ElementTemplateUpdateOps.setGesture;
128
+ }
125
129
  if (kind === 'mt-event') {
126
130
  return ElementTemplateUpdateOps.setMainThreadEvent;
127
131
  }
@@ -17,18 +17,21 @@ import { resetElementTemplateMainThreadFunctionRuntime } from '../runtime/templa
17
17
  export function destroyElementTemplateBackgroundRuntime(): void {
18
18
  resetElementTemplateHydrationListener();
19
19
  cancelElementTemplateRemovedSubtreeCleanup();
20
-
21
- withSyncEffectFlush(() => {
22
- render(null, __root as unknown as ContainerNode);
23
- });
24
- // Run user cleanup before dropping the backend side tables; after clearRefState
25
- // the raw ref ownership needed to detach callbacks is gone.
26
- flushPendingRefs();
27
-
28
20
  resetElementTemplateCommitState();
29
- resetElementTemplateBackgroundFunctionRuntime();
30
- resetElementTemplateMainThreadFunctionRuntime();
31
- clearEventState();
32
- clearRefState();
33
- backgroundElementTemplateInstanceManager.clear();
21
+
22
+ try {
23
+ withSyncEffectFlush(() => {
24
+ render(null, __root as unknown as ContainerNode);
25
+ });
26
+ // Run user cleanup before dropping the backend side tables; after clearRefState
27
+ // the raw ref ownership needed to detach callbacks is gone.
28
+ flushPendingRefs();
29
+ } finally {
30
+ resetElementTemplateCommitState();
31
+ resetElementTemplateBackgroundFunctionRuntime();
32
+ resetElementTemplateMainThreadFunctionRuntime();
33
+ clearEventState();
34
+ clearRefState();
35
+ backgroundElementTemplateInstanceManager.clear();
36
+ }
34
37
  }
@@ -16,6 +16,8 @@ import { backgroundElementTemplateInstanceManager } from './manager.js';
16
16
  import { isMainThreadFunction } from '../../core/main-thread-function.js';
17
17
  import { isDirectOrDeepEqual } from '../../utils.js';
18
18
  import { hydrationMap } from '../hydration-map.js';
19
+ import { isGestureSlotValue } from '../prop-adapters/gesture.js';
20
+ import { isMainThreadEventPropKey, isSpreadRecord } from '../prop-adapters/spread.js';
19
21
  import { ElementTemplateUpdateOps } from '../protocol/opcodes.js';
20
22
  import { ELEMENT_TEMPLATE_PAGE_HANDLE_ID, ELEMENT_TEMPLATE_PAGE_ROOT_SLOT_INDEX } from '../protocol/page.js';
21
23
  import { elementTemplateIdentityKey, parseElementTemplateType } from '../protocol/template-type.js';
@@ -629,9 +631,30 @@ function shouldForceMainThreadHydrateSlot(
629
631
  slotKind: MainThreadDynamicAttrKind | undefined,
630
632
  value: SerializableValue | undefined,
631
633
  ): boolean {
634
+ if (slotKind === 'gesture') {
635
+ return value != null;
636
+ }
632
637
  if (slotKind === 'mt-event') {
633
638
  return isMTEventNativeWrapper(value);
634
639
  }
640
+ if (slotKind === 'spread') {
641
+ if (!isSpreadRecord(value)) {
642
+ return false;
643
+ }
644
+ for (const key of Object.keys(value)) {
645
+ if (key === 'main-thread:ref') {
646
+ return true;
647
+ }
648
+ const entry = value[key];
649
+ if (key === 'main-thread:gesture' && isGestureSlotValue(entry)) {
650
+ return true;
651
+ }
652
+ if (isMainThreadEventPropKey(key) && isMTEventNativeWrapper(entry)) {
653
+ return true;
654
+ }
655
+ }
656
+ return false;
657
+ }
635
658
  return slotKind === 'mt-ref'
636
659
  && (value == null || isMainThreadFunction((value as unknown as MTRefNativeWrapper).value));
637
660
  }
@@ -31,7 +31,7 @@ export type FormattedElementTemplateUpdateCommand =
31
31
  value: unknown;
32
32
  }
33
33
  | {
34
- op: 'setMainThreadEvent' | 'setMainThreadRef';
34
+ op: 'setMainThreadEvent' | 'setMainThreadRef' | 'setGesture';
35
35
  targetId: number;
36
36
  attrSlotIndex: number;
37
37
  value: unknown;
@@ -111,10 +111,13 @@ export function formatElementTemplateUpdateCommands(
111
111
 
112
112
  case ElementTemplateUpdateOps.setMainThreadEvent:
113
113
  case ElementTemplateUpdateOps.setMainThreadRef:
114
+ case ElementTemplateUpdateOps.setGesture:
114
115
  result.push({
115
116
  op: op === ElementTemplateUpdateOps.setMainThreadEvent
116
117
  ? 'setMainThreadEvent'
117
- : 'setMainThreadRef',
118
+ : (op === ElementTemplateUpdateOps.setMainThreadRef
119
+ ? 'setMainThreadRef'
120
+ : 'setGesture'),
118
121
  targetId: stream[index++] as number,
119
122
  attrSlotIndex: stream[index++] as number,
120
123
  value: stream[index++],
@@ -51,6 +51,7 @@ export { captureMainThreadObject } from '../core/capture-main-thread-object.js';
51
51
  export {
52
52
  __etAttrPlanMap,
53
53
  adaptEventAttrSlot,
54
+ adaptGestureAttrSlot,
54
55
  adaptMTEventAttrSlot,
55
56
  adaptMTRefAttrSlot,
56
57
  adaptRefAttrSlot,
@@ -6,5 +6,9 @@ import { destroyElementTemplateBackgroundRuntime } from '../background/destroy.j
6
6
 
7
7
  export function callDestroyLifetimeFun(): void {
8
8
  takeMainThreadRefInitValuePatch();
9
- destroyElementTemplateBackgroundRuntime();
9
+ try {
10
+ destroyElementTemplateBackgroundRuntime();
11
+ } finally {
12
+ takeMainThreadRefInitValuePatch();
13
+ }
10
14
  }
@@ -87,7 +87,7 @@ export function installElementTemplatePatchListener(): void {
87
87
  } else {
88
88
  markTiming('mtsRenderEnd');
89
89
  }
90
- applyPendingMainThreadRefAttachments();
90
+ applyPendingMainThreadRefAttachments(isHydration);
91
91
  } finally {
92
92
  if (isHydration) {
93
93
  flushDelayedRunOnBackgroundFunctions();
@@ -39,7 +39,7 @@ export function reloadBackground(updateData: unknown, options?: Record<string, u
39
39
  // Reload creates a new object so InitData Provider / Consumer observers do
40
40
  // not retain the pre-reload object identity.
41
41
  lynx.__initData = Object.assign({}, lynx.__initData);
42
- applyUpdatePageData(updateData);
42
+ applyUpdatePageData(updateData, options);
43
43
 
44
44
  setRoot(new BackgroundPageRootInstance());
45
45
  __root.__jsx = jsx;
@@ -38,6 +38,8 @@ export function reloadMainThread(data: unknown, options: UpdatePageOption): void
38
38
  // TODO: Replace this cleanup-only serialization with a direct page-children
39
39
  // or clear-slot PAPI once native exposes one.
40
40
  const page = __SerializeElementTemplate(__page) as SerializedPageRoot;
41
+ let cleanupError: unknown;
42
+ let didCleanupThrow = false;
41
43
  for (const root of page.childSlots?.[ELEMENT_TEMPLATE_PAGE_ROOT_SLOT_INDEX] ?? []) {
42
44
  const rootRef = elementTemplateRegistry.get(root.uid)!;
43
45
  __RemoveNodeFromElementTemplate(__page, ELEMENT_TEMPLATE_PAGE_ROOT_SLOT_INDEX, rootRef);
@@ -46,22 +48,40 @@ export function reloadMainThread(data: unknown, options: UpdatePageOption): void
46
48
  for (const handleId of removedHandleIds) {
47
49
  elementTemplateRegistry.delete(handleId);
48
50
  }
49
- deleteMainThreadDynamicAttrStateForSubtree(removedHandleIds);
51
+ try {
52
+ deleteMainThreadDynamicAttrStateForSubtree(removedHandleIds);
53
+ } catch (error) {
54
+ if (!didCleanupThrow) {
55
+ cleanupError = error;
56
+ didCleanupThrow = true;
57
+ }
58
+ }
50
59
  }
51
60
  if (canReEvalMainThreadEntry()) {
52
- onMtsDestruction();
53
- increaseReloadVersion();
61
+ try {
62
+ onMtsDestruction();
63
+ } finally {
64
+ increaseReloadVersion();
65
+ }
66
+ if (didCleanupThrow) {
67
+ throw cleanupError;
68
+ }
54
69
  reEvalMainThreadEntry<MainThreadReloadHandoff>({ page: __page, options });
55
70
  return;
56
71
  }
57
72
  elementTemplateRegistry.clear();
58
- clearMainThreadDynamicAttrState();
59
- resetElementTemplateMainThreadBackgroundFunctionRuntime();
60
- resetTemplateId();
61
-
62
- // Ref cleanup still belongs to the old page. Advance only before rendering
63
- // the new tree so calls queued by old ref/unmount callbacks remain stale.
64
- increaseReloadVersion();
73
+ try {
74
+ clearMainThreadDynamicAttrState();
75
+ } finally {
76
+ resetElementTemplateMainThreadBackgroundFunctionRuntime();
77
+ resetTemplateId();
78
+ // Ref cleanup still belongs to the old page. Advance only before rendering
79
+ // the new tree so calls queued by old ref/unmount callbacks remain stale.
80
+ increaseReloadVersion();
81
+ }
82
+ if (didCleanupThrow) {
83
+ throw cleanupError;
84
+ }
65
85
 
66
86
  const oldRoot = __root;
67
87
  setRoot({ __jsx: oldRoot.__jsx });
@@ -0,0 +1,144 @@
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 { hydrateWorkletCtx } from '@lynx-js/react/worklet-runtime/bindings';
6
+ import type { Worklet } from '@lynx-js/react/worklet-runtime/bindings';
7
+
8
+ import { isMainThreadFunction } from '../../core/main-thread-function.js';
9
+ import {
10
+ registerMainThreadBackgroundFunctionCtx,
11
+ retainMainThreadBackgroundFunctionCtx,
12
+ } from '../runtime/template/main-thread-background-function.js';
13
+ import type { MTEventCtx } from '../runtime/template/main-thread-event-ctx.js';
14
+
15
+ interface GestureInput {
16
+ type: number;
17
+ id: number;
18
+ __isGesture?: boolean;
19
+ __isSerialized?: boolean;
20
+ gestures?: GestureInput[];
21
+ config?: Record<string, unknown>;
22
+ callbacks: Record<string, MTEventCtx | null | undefined>;
23
+ waitFor?: { id: number }[];
24
+ simultaneousWith?: { id: number }[];
25
+ continueWith?: { id: number }[];
26
+ }
27
+
28
+ export interface GestureDescriptor {
29
+ id: number;
30
+ type: number;
31
+ config: Record<string, unknown>;
32
+ callbacks: { name: string; callback: MTEventCtx }[];
33
+ relationMap: {
34
+ waitFor: number[];
35
+ simultaneous: number[];
36
+ continueWith: number[];
37
+ };
38
+ }
39
+
40
+ export interface GestureSlotValue {
41
+ type: 'gesture';
42
+ value: GestureDescriptor[];
43
+ }
44
+
45
+ export function isGestureSlotValue(value: unknown): value is GestureSlotValue {
46
+ return value !== null
47
+ && typeof value === 'object'
48
+ && !Array.isArray(value)
49
+ && (value as { type?: unknown }).type === 'gesture'
50
+ && Array.isArray((value as { value?: unknown }).value);
51
+ }
52
+
53
+ // Provenance stays outside the wire value. A repeated prepare before native
54
+ // delivery must reuse its registered ctx, while mutable Gesture config is copied.
55
+ const callbackSources = new WeakMap<MTEventCtx, MTEventCtx>();
56
+
57
+ export function prepareGestureForNative(
58
+ value: unknown,
59
+ previous?: GestureSlotValue | null,
60
+ ): GestureSlotValue | null {
61
+ if (value == null || value === false) {
62
+ return null;
63
+ }
64
+ if (
65
+ typeof value !== 'object'
66
+ || !((value as GestureInput).__isGesture === true || (value as GestureInput).__isSerialized === true)
67
+ ) {
68
+ throw new Error('ElementTemplate main-thread:gesture expects a Gesture object.');
69
+ }
70
+
71
+ const descriptors: GestureDescriptor[] = [];
72
+ const seen = new Set<number>();
73
+ function append(gesture: GestureInput): void {
74
+ if (gesture.type === -1) {
75
+ for (const child of gesture.gestures!) {
76
+ append(child);
77
+ }
78
+ return;
79
+ }
80
+ if (seen.has(gesture.id)) {
81
+ return;
82
+ }
83
+ seen.add(gesture.id);
84
+ const callbacks: GestureDescriptor['callbacks'] = [];
85
+ const previousGesture = previous?.value.find(item => item.id === gesture.id);
86
+ for (const name of Object.keys(gesture.callbacks)) {
87
+ const rawCallback = gesture.callbacks[name];
88
+ if (rawCallback == null) {
89
+ continue;
90
+ }
91
+ if (!isMainThreadFunction(rawCallback)) {
92
+ throw new Error(`Gesture callback '${name}' must be a main-thread function.`);
93
+ }
94
+ const previousCallback = previousGesture?.callbacks.find(item => item.name === name)?.callback;
95
+ let callback = previousCallback;
96
+ if (!callback || callbackSources.get(callback) !== rawCallback) {
97
+ callback = { ...rawCallback };
98
+ callbackSources.set(callback, rawCallback);
99
+ registerMainThreadBackgroundFunctionCtx(callback);
100
+ }
101
+ callbacks.push({ name, callback });
102
+ }
103
+ descriptors.push({
104
+ id: gesture.id,
105
+ type: gesture.type,
106
+ config: { ...gesture.config },
107
+ callbacks,
108
+ relationMap: {
109
+ waitFor: gesture.waitFor?.map(item => item.id) ?? [],
110
+ simultaneous: gesture.simultaneousWith?.map(item => item.id) ?? [],
111
+ continueWith: gesture.continueWith?.map(item => item.id) ?? [],
112
+ },
113
+ });
114
+ }
115
+ append(value as GestureInput);
116
+ return { type: 'gesture', value: descriptors };
117
+ }
118
+
119
+ export function retainGestureCallbacks(value: GestureSlotValue): void {
120
+ for (const gesture of value.value) {
121
+ for (const { callback } of gesture.callbacks) {
122
+ retainMainThreadBackgroundFunctionCtx(callback);
123
+ }
124
+ }
125
+ }
126
+
127
+ export function hydrateGestureCallbacks(value: GestureSlotValue, previous: GestureSlotValue): void {
128
+ // IDs belong to independent thread-local allocators. Pair each type in
129
+ // declaration order; the worklet hydrator also checks callback source identity.
130
+ const remaining = previous.value.slice();
131
+ for (const gesture of value.value) {
132
+ const index = remaining.findIndex(old => old.type === gesture.type);
133
+ if (index === -1) {
134
+ continue;
135
+ }
136
+ const oldGesture = remaining.splice(index, 1)[0]!;
137
+ for (const { name, callback } of gesture.callbacks) {
138
+ const oldCallback = oldGesture.callbacks.find(item => item.name === name)?.callback;
139
+ if (oldCallback) {
140
+ hydrateWorkletCtx(callback as Worklet, oldCallback as Worklet);
141
+ }
142
+ }
143
+ }
144
+ }
@@ -3,24 +3,51 @@
3
3
  // LICENSE file in the root directory of this source tree.
4
4
 
5
5
  import { getEventValue } from './event-value.js';
6
+ import { isGestureSlotValue, prepareGestureForNative } from './gesture.js';
6
7
  import { prepareSpreadRefAttrValue } from './ref.js';
8
+ import { isMainThreadFunction } from '../../core/main-thread-function.js';
7
9
  import { isEventPropKey, isNamespacedEventPropKey, transformAttrName } from '../../shared/attribute-name.js';
8
10
  import type { SerializableValue } from '../protocol/types.js';
11
+ import type { EtAttrAdapterContext } from '../runtime/template/attr-slot-plan.js';
12
+ import { prepareMTEventCtxForNative } from '../runtime/template/main-thread-event-ctx.js';
13
+ import { prepareMTRefForNative } from '../runtime/template/main-thread-ref-ctx.js';
9
14
 
10
- function isSpreadRecord(value: unknown): value is Record<string, unknown> {
15
+ const MAIN_THREAD_NAMESPACE_PREFIX = 'main-thread:';
16
+
17
+ // Keep per-key raw callback/ref provenance outside the wire value so in-place
18
+ // mutation of a caller-reused spread object cannot alias historical raw values.
19
+ const spreadRawSources = new WeakMap<Record<string, unknown>, Record<string, unknown>>();
20
+
21
+ export function isSpreadRecord(value: unknown): value is Record<string, unknown> {
11
22
  return value !== null && typeof value === 'object' && !Array.isArray(value);
12
23
  }
13
24
 
14
- export function prepareSpreadAttrSlot(
25
+ export function hasOwnSpreadKey(record: Record<string, unknown>, key: string): boolean {
26
+ return Object.prototype.hasOwnProperty.call(record, key);
27
+ }
28
+
29
+ export function isMainThreadEventPropKey(key: string): boolean {
30
+ return key.startsWith(MAIN_THREAD_NAMESPACE_PREFIX)
31
+ && isEventPropKey(key.slice(MAIN_THREAD_NAMESPACE_PREFIX.length));
32
+ }
33
+
34
+ function prepareSpreadAttrSlotImpl(
15
35
  handleId: number,
16
36
  attrSlotIndex: number,
17
37
  value: unknown,
38
+ allowMainThreadCapabilities: boolean,
39
+ context?: EtAttrAdapterContext,
18
40
  ): SerializableValue | null {
19
41
  if (value === false || !isSpreadRecord(value)) {
20
42
  return null;
21
43
  }
22
44
 
45
+ const prevPreparedSlot = context?.previousPreparedSlots?.[attrSlotIndex];
46
+ const prevPreparedRecord = isSpreadRecord(prevPreparedSlot) ? prevPreparedSlot : undefined;
47
+ const prevRawSources = prevPreparedRecord ? spreadRawSources.get(prevPreparedRecord) : undefined;
48
+
23
49
  const prepared: Record<string, SerializableValue> = {};
50
+ let rawSources: Record<string, unknown> | undefined;
24
51
 
25
52
  for (const key in value) {
26
53
  const spreadValue = value[key];
@@ -41,6 +68,66 @@ export function prepareSpreadAttrSlot(
41
68
  continue;
42
69
  }
43
70
 
71
+ if (allowMainThreadCapabilities) {
72
+ if (key === 'main-thread:ref') {
73
+ if (!hasOwnSpreadKey(value, key)) {
74
+ continue;
75
+ }
76
+ const preparedRef = prepareMTRefForNative(
77
+ spreadValue,
78
+ prevPreparedRecord?.[key],
79
+ prevRawSources?.[key],
80
+ );
81
+ prepared[key] = preparedRef;
82
+ if (preparedRef !== null) {
83
+ (rawSources ??= Object.create(null) as Record<string, unknown>)[key] = spreadValue;
84
+ }
85
+ continue;
86
+ }
87
+
88
+ if (key === 'main-thread:gesture') {
89
+ if (!hasOwnSpreadKey(value, key)) {
90
+ continue;
91
+ }
92
+ const prevGesture = prevPreparedRecord && hasOwnSpreadKey(prevPreparedRecord, key)
93
+ ? prevPreparedRecord[key]
94
+ : undefined;
95
+ prepared[key] = prepareGestureForNative(
96
+ spreadValue,
97
+ isGestureSlotValue(prevGesture) ? prevGesture : undefined,
98
+ ) as unknown as SerializableValue;
99
+ continue;
100
+ }
101
+
102
+ if (isMainThreadEventPropKey(key)) {
103
+ if (!hasOwnSpreadKey(value, key)) {
104
+ continue;
105
+ }
106
+ if (spreadValue === null || spreadValue === undefined || spreadValue === false) {
107
+ prepared[key] = null;
108
+ continue;
109
+ }
110
+ if (!isMainThreadFunction(spreadValue)) {
111
+ if (__DEV__) {
112
+ lynx.reportError(
113
+ new Error(
114
+ `ElementTemplate main-thread event slot ${handleId}:${attrSlotIndex} (${key}) expects a worklet ctx object.`,
115
+ ),
116
+ );
117
+ }
118
+ prepared[key] = null;
119
+ continue;
120
+ }
121
+ prepared[key] = prepareMTEventCtxForNative(
122
+ spreadValue,
123
+ prevPreparedRecord?.[key],
124
+ prevRawSources?.[key],
125
+ );
126
+ (rawSources ??= Object.create(null) as Record<string, unknown>)[key] = spreadValue;
127
+ continue;
128
+ }
129
+ }
130
+
44
131
  const transformedKey = transformAttrName(key);
45
132
  if (isEventPropKey(transformedKey)) {
46
133
  prepared[transformedKey] = spreadValue === null || spreadValue === undefined || spreadValue === false
@@ -62,5 +149,26 @@ export function prepareSpreadAttrSlot(
62
149
  prepared[transformedKey] = spreadValue as SerializableValue;
63
150
  }
64
151
 
152
+ if (rawSources) {
153
+ spreadRawSources.set(prepared, rawSources);
154
+ }
155
+
65
156
  return prepared;
66
157
  }
158
+
159
+ export function prepareSpreadAttrSlot(
160
+ handleId: number,
161
+ attrSlotIndex: number,
162
+ value: unknown,
163
+ context?: EtAttrAdapterContext,
164
+ ): SerializableValue | null {
165
+ return prepareSpreadAttrSlotImpl(handleId, attrSlotIndex, value, true, context);
166
+ }
167
+
168
+ export function prepareTypedSpreadAttrSlot(
169
+ handleId: number,
170
+ attrSlotIndex: number,
171
+ value: unknown,
172
+ ): SerializableValue | null {
173
+ return prepareSpreadAttrSlotImpl(handleId, attrSlotIndex, value, false);
174
+ }
@@ -13,6 +13,7 @@ export const ElementTemplateUpdateOps = {
13
13
  updateTypedListItem: 8,
14
14
  setMainThreadEvent: 9,
15
15
  setMainThreadRef: 10,
16
+ setGesture: 11,
16
17
  } as const;
17
18
 
18
19
  export type ElementTemplateUpdateOp = typeof ElementTemplateUpdateOps[keyof typeof ElementTemplateUpdateOps];
@@ -128,6 +128,13 @@ export type SetMainThreadEventCommand = [
128
128
  value: SerializableValue | null,
129
129
  ];
130
130
 
131
+ export type SetGestureCommand = [
132
+ typeof ElementTemplateUpdateOps.setGesture,
133
+ targetHandleId: number,
134
+ attrSlotIndex: number,
135
+ value: SerializableValue | null,
136
+ ];
137
+
131
138
  export type SetMainThreadRefCommand = [
132
139
  typeof ElementTemplateUpdateOps.setMainThreadRef,
133
140
  targetHandleId: number,
@@ -186,6 +193,7 @@ export type ElementTemplateUpdateCommand =
186
193
  | SetAttributeCommand
187
194
  | SetMainThreadEventCommand
188
195
  | SetMainThreadRefCommand
196
+ | SetGestureCommand
189
197
  | InsertNodeCommand
190
198
  | RemoveNodeCommand
191
199
  | CreateTypedElementCommand
@@ -43,11 +43,18 @@ import type {
43
43
  import {
44
44
  deleteMainThreadDynamicAttrStateForSubtree,
45
45
  initializeMainThreadDynamicAttrSlots,
46
+ isSpreadDynamicAttrSlot,
46
47
  prepareMainThreadDynamicAttrSlotsForNative,
48
+ prepareSpreadAttrSlotForNative,
49
+ reconcileUnhydratedSpreadMTRefSlots,
50
+ updateGestureAttrSlot,
47
51
  updateMainThreadEventAttrSlot,
48
52
  updateMainThreadRefAttrSlot,
53
+ updateSpreadAttrSlot,
49
54
  } from './template/main-thread-dynamic-attr-state.js';
50
55
  import type { MainThreadDynamicAttrSubtreeHandle } from './template/main-thread-dynamic-attr-state.js';
56
+ import { retainGestureCallbacks } from '../prop-adapters/gesture.js';
57
+ import type { GestureSlotValue } from '../prop-adapters/gesture.js';
51
58
 
52
59
  export type { ElementTemplateUpdateCommandStream } from '../protocol/types.js';
53
60
 
@@ -105,6 +112,7 @@ export function applyElementTemplateUpdateCommands(
105
112
  handleId,
106
113
  templateType,
107
114
  attributeSlots,
115
+ true,
108
116
  );
109
117
  }
110
118
  break;
@@ -128,10 +136,32 @@ export function applyElementTemplateUpdateCommands(
128
136
  break;
129
137
  }
130
138
  }
139
+ if (isSpreadDynamicAttrSlot(targetId, attrSlotIndex)) {
140
+ const nativeValue = prepareSpreadAttrSlotForNative(value);
141
+ __SetAttributeOfElementTemplate(nativeRef, attrSlotIndex, nativeValue);
142
+ updateSpreadAttrSlot(targetId, attrSlotIndex, value, nativeRef, isHydration);
143
+ break;
144
+ }
131
145
  __SetAttributeOfElementTemplate(nativeRef, attrSlotIndex, value);
132
146
  break;
133
147
  }
134
148
 
149
+ case ElementTemplateUpdateOps.setGesture: {
150
+ const targetId = stream[i++] as number;
151
+ const attrSlotIndex = stream[i++] as number;
152
+ const value = stream[i++] as SerializableValue | null;
153
+ const nativeRef = resolveTargetHandle(targetId, 'target');
154
+ if (!nativeRef) {
155
+ continue;
156
+ }
157
+ if (value != null) {
158
+ retainGestureCallbacks(value as unknown as GestureSlotValue);
159
+ }
160
+ __SetAttributeOfElementTemplate(nativeRef, attrSlotIndex, value);
161
+ updateGestureAttrSlot(targetId, attrSlotIndex, value, isHydration);
162
+ break;
163
+ }
164
+
135
165
  case ElementTemplateUpdateOps.setMainThreadEvent: {
136
166
  const targetId = stream[i++] as number;
137
167
  const attrSlotIndex = stream[i++] as number;
@@ -328,6 +358,9 @@ export function applyElementTemplateUpdateCommands(
328
358
  }
329
359
  }
330
360
  }
361
+ if (isHydration) {
362
+ reconcileUnhydratedSpreadMTRefSlots();
363
+ }
331
364
  flushPendingListUpdates();
332
365
  }
333
366