@lynx-js/react 0.126.1 → 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.
- package/CHANGELOG.md +40 -0
- package/components/tsconfig.tsbuildinfo +1 -1
- package/docs/directives.md +90 -0
- package/docs/global-events.md +50 -0
- package/docs/import-attributes.md +43 -0
- package/docs/macros.md +285 -0
- package/package.json +4 -3
- package/refresh/.turbo/turbo-build.log +4 -4
- package/refresh/dist/index.js +27 -8
- package/refresh/package.json +2 -2
- package/runtime/__test__/core/main-thread-object.test.js +149 -0
- package/runtime/__test__/element-template/debug/profile-installation.test.tsx +97 -1
- package/runtime/__test__/element-template/fixtures/background/hydrate-compiled/children.non-string-raw-text-key-on-main/output.txt +1 -1
- package/runtime/__test__/element-template/fixtures/background/main-thread-function/run-on-main-thread/index.tsx +26 -3
- package/runtime/__test__/element-template/fixtures/render/child-siblings/index.js.txt +15 -11
- package/runtime/__test__/element-template/fixtures/render/component/index.js.txt +8 -7
- package/runtime/__test__/element-template/fixtures/render/component-slot-content/index.js.txt +14 -10
- package/runtime/__test__/element-template/fixtures/render/css-scope-dynamic-bundle-url/index.js.txt +4 -3
- package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/_shared.ts +2 -9
- package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/appends-root-text-via-append-element/case.ts +2 -4
- package/runtime/__test__/element-template/fixtures/render/direct-native/creates-template-from-attrs-and-slot-text/case.ts +20 -0
- package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/handles-multiple-template-children-in-the-same-slot/case.ts +3 -16
- package/runtime/__test__/element-template/fixtures/render/direct-native/handles-multiple-text-nodes-in-the-same-slot/case.ts +18 -0
- package/runtime/__test__/element-template/fixtures/render/direct-native/inserts-nested-templates-into-parent-slots/case.ts +20 -0
- package/runtime/__test__/element-template/fixtures/render/direct-native/keeps-slot-children-separated-and-ordered/case.ts +18 -0
- package/runtime/__test__/element-template/fixtures/render/direct-native/passes-bundle-url-to-native-create/case.ts +15 -0
- package/runtime/__test__/element-template/fixtures/render/mapped-view-children/index.js.txt +12 -11
- package/runtime/__test__/element-template/fixtures/render/mixed-children/index.js.txt +8 -7
- package/runtime/__test__/element-template/fixtures/render/multiple-text/index.js.txt +15 -13
- package/runtime/__test__/element-template/fixtures/render/nested-templates/index.js.txt +9 -7
- package/runtime/__test__/element-template/fixtures/render/react-example/index.js.txt +17 -20
- package/runtime/__test__/element-template/fixtures/render/sparse-element-slot/index.js.txt +13 -10
- package/runtime/__test__/element-template/fixtures/render/template-dedupe/index.js.txt +14 -11
- package/runtime/__test__/element-template/imports/main-thread-object-api.test.ts +10 -0
- package/runtime/__test__/element-template/imports/suspense-lazy.test.ts +52 -7
- package/runtime/__test__/element-template/internal/legacy-internal-guardrail.test.ts +3 -0
- package/runtime/__test__/element-template/native/reload.test.ts +10 -26
- package/runtime/__test__/element-template/runtime/background/component-stack.test.tsx +76 -0
- package/runtime/__test__/element-template/runtime/background/hydrate.test.ts +87 -43
- package/runtime/__test__/element-template/runtime/background/instance.test.ts +14 -0
- package/runtime/__test__/element-template/runtime/background/main-thread-function/compiled-fixtures.test.tsx +4 -0
- package/runtime/__test__/element-template/runtime/render/compact-host-debug.test.ts +57 -0
- package/runtime/__test__/element-template/runtime/render/compact-host.test.ts +199 -0
- package/runtime/__test__/element-template/runtime/render/render-components.test.jsx +292 -0
- package/runtime/__test__/element-template/runtime/render/{render-opcodes-into-element-template.et.test.tsx → render-direct-native.test.tsx} +50 -314
- package/runtime/__test__/element-template/runtime/render/render-direct-suspense.test.ts +378 -0
- package/runtime/__test__/element-template/runtime/render/render-direct.test.ts +491 -0
- package/runtime/__test__/element-template/runtime/render/render-main-thread.contract.test.ts +2 -28
- package/runtime/__test__/element-template/runtime/render/render-main-thread.test.ts +21 -35
- package/runtime/__test__/element-template/runtime/render/render-transform.contract.test.ts +6 -5
- package/runtime/__test__/element-template/test-utils/debug/renderFixtureRunner.ts +4 -4
- package/runtime/__test__/element-template/vitest.config.ts +2 -1
- package/runtime/__test__/snapshot/ctxNotFound.test.ts +271 -0
- package/runtime/__test__/snapshot/debug/main-thread-children-with-same-key.test.ts +28 -0
- package/runtime/__test__/snapshot/debug/profile.test.jsx +108 -3
- package/runtime/__test__/snapshot/event.test.jsx +7 -7
- package/runtime/__test__/snapshot/lazy.test.js +114 -7
- package/runtime/__test__/snapshot/lifecycle/reloadGc.test.jsx +66 -0
- package/runtime/__test__/snapshot/lynx/portals.test.jsx +75 -37
- package/runtime/__test__/snapshot/lynx/runtime-version.test.ts +140 -0
- package/runtime/__test__/snapshot/snapshotPatch.test.jsx +59 -47
- package/runtime/__test__/snapshot/spread.test.jsx +32 -25
- package/runtime/__test__/snapshot/ssr.legacy-slot.test.jsx +168 -0
- package/runtime/__test__/snapshot/ssr.list.test.jsx +211 -0
- package/runtime/__test__/snapshot/worklet/main-thread-object.test.jsx +306 -0
- package/runtime/__test__/snapshot/worklet/workletRef.test.jsx +1 -0
- package/runtime/__test__/worklet-runtime/hydrate.test.js +92 -0
- package/runtime/__test__/worklet-runtime/mainThreadObject.test.js +182 -0
- package/runtime/__test__/worklet-runtime/object-methods.test.js +60 -0
- package/runtime/__test__/worklet-runtime/runOnBackground.test.js +8 -6
- package/runtime/__test__/worklet-runtime/workletRef.test.js +362 -2
- package/runtime/__test__/worklet-runtime/workletRuntime.test.js +249 -3
- package/runtime/lazy/compat.js +2 -0
- package/runtime/lazy/element-template-import.js +0 -4
- package/runtime/lazy/element-template-internal.js +19 -8
- package/runtime/lazy/element-template-jsx-dev-runtime.js +1 -8
- package/runtime/lazy/element-template-jsx-runtime.js +1 -3
- package/runtime/lazy/element-template.js +3 -3
- package/runtime/lazy/import.js +0 -4
- package/runtime/lazy/internal.js +17 -1
- package/runtime/lazy/react.js +2 -0
- package/runtime/lazy/target.js +0 -21
- package/runtime/lib/core/background-function/run-on-background.d.ts +16 -0
- package/runtime/lib/core/background-function/run-on-background.js +16 -0
- package/runtime/lib/core/background-function/run-on-background.js.map +1 -1
- package/runtime/lib/core/capture-main-thread-object.js +16 -0
- package/runtime/lib/core/capture-main-thread-object.js.map +1 -0
- package/runtime/lib/core/globalProps.d.ts +2 -0
- package/runtime/lib/core/globalProps.js.map +1 -1
- package/runtime/lib/core/hooks/react.d.ts +6 -0
- package/runtime/lib/core/hooks/react.js +6 -0
- package/runtime/lib/core/hooks/react.js.map +1 -1
- package/runtime/lib/core/hooks/useLynxGlobalEventListener.d.ts +2 -0
- package/runtime/lib/core/hooks/useLynxGlobalEventListener.js +2 -0
- package/runtime/lib/core/hooks/useLynxGlobalEventListener.js.map +1 -1
- package/runtime/lib/core/initData.d.ts +2 -0
- package/runtime/lib/core/initData.js +2 -0
- package/runtime/lib/core/initData.js.map +1 -1
- package/runtime/lib/core/lynx/runtime-version.d.ts +16 -0
- package/runtime/lib/core/lynx/runtime-version.js +51 -0
- package/runtime/lib/core/lynx/runtime-version.js.map +1 -0
- package/runtime/lib/core/main-thread-object-handle-registry.d.ts +3 -0
- package/runtime/lib/core/main-thread-object-handle-registry.js +17 -0
- package/runtime/lib/core/main-thread-object-handle-registry.js.map +1 -0
- package/runtime/lib/core/main-thread-object.d.ts +77 -0
- package/runtime/lib/core/main-thread-object.js +261 -0
- package/runtime/lib/core/main-thread-object.js.map +1 -0
- package/runtime/lib/core/main-thread-ref-id.js +14 -0
- package/runtime/lib/core/main-thread-ref-id.js.map +1 -0
- package/runtime/lib/core/main-thread-ref-init-value.d.ts +1 -1
- package/runtime/lib/core/main-thread-ref-init-value.js +4 -2
- package/runtime/lib/core/main-thread-ref-init-value.js.map +1 -1
- package/runtime/lib/core/main-thread-ref.d.ts +4 -0
- package/runtime/lib/core/main-thread-ref.js +5 -8
- package/runtime/lib/core/main-thread-ref.js.map +1 -1
- package/runtime/lib/element-template/background/commit-hook.js.map +1 -1
- package/runtime/lib/element-template/background/document.d.ts +1 -1
- package/runtime/lib/element-template/background/document.js +1 -1
- package/runtime/lib/element-template/background/document.js.map +1 -1
- package/runtime/lib/element-template/background/hydrate.js +51 -19
- package/runtime/lib/element-template/background/hydrate.js.map +1 -1
- package/runtime/lib/element-template/background/instance.js +5 -6
- package/runtime/lib/element-template/background/instance.js.map +1 -1
- package/runtime/lib/element-template/debug/profile.js +38 -22
- package/runtime/lib/element-template/debug/profile.js.map +1 -1
- package/runtime/lib/element-template/index.d.ts +3 -1
- package/runtime/lib/element-template/index.js +2 -1
- package/runtime/lib/element-template/index.js.map +1 -1
- package/runtime/lib/element-template/internal.d.ts +5 -1
- package/runtime/lib/element-template/internal.js +7 -1
- package/runtime/lib/element-template/internal.js.map +1 -1
- package/runtime/lib/element-template/native/index.js +4 -0
- package/runtime/lib/element-template/native/index.js.map +1 -1
- package/runtime/lib/element-template/protocol/types.d.ts +1 -1
- package/runtime/lib/element-template/runtime/list/list.d.ts +1 -0
- package/runtime/lib/element-template/runtime/list/list.js +3 -0
- package/runtime/lib/element-template/runtime/list/list.js.map +1 -1
- package/runtime/lib/element-template/runtime/render/create-rendered-host.d.ts +7 -0
- package/runtime/lib/element-template/runtime/render/create-rendered-host.js +65 -0
- package/runtime/lib/element-template/runtime/render/create-rendered-host.js.map +1 -0
- package/runtime/lib/element-template/runtime/render/host.d.ts +18 -0
- package/runtime/lib/element-template/runtime/render/host.js +25 -0
- package/runtime/lib/element-template/runtime/render/host.js.map +1 -0
- package/runtime/lib/element-template/runtime/render/render-components.d.ts +6 -0
- package/runtime/lib/element-template/runtime/render/render-components.js +190 -0
- package/runtime/lib/element-template/runtime/render/render-components.js.map +1 -0
- package/runtime/lib/element-template/runtime/render/{render-opcodes.d.ts → render-direct.d.ts} +3 -1
- package/runtime/lib/element-template/runtime/render/render-direct.js +194 -0
- package/runtime/lib/element-template/runtime/render/render-direct.js.map +1 -0
- package/runtime/lib/element-template/runtime/render/render-main-thread.js +10 -9
- package/runtime/lib/element-template/runtime/render/render-main-thread.js.map +1 -1
- package/runtime/lib/element-template/runtime/template/handle.d.ts +2 -1
- package/runtime/lib/element-template/runtime/template/handle.js +12 -4
- package/runtime/lib/element-template/runtime/template/handle.js.map +1 -1
- package/runtime/lib/element-template/runtime-marker.d.ts +1 -0
- package/runtime/lib/element-template/runtime-marker.js +13 -0
- package/runtime/lib/element-template/runtime-marker.js.map +1 -0
- package/runtime/lib/index.d.ts +1 -1
- package/runtime/lib/index.js +1 -1
- package/runtime/lib/index.js.map +1 -1
- package/runtime/lib/internal.d.ts +4 -1
- package/runtime/lib/internal.js +6 -1
- package/runtime/lib/internal.js.map +1 -1
- package/runtime/lib/lynx-api.d.ts +49 -6
- package/runtime/lib/lynx-api.js +34 -4
- package/runtime/lib/lynx-api.js.map +1 -1
- package/runtime/lib/lynx.js +4 -1
- package/runtime/lib/lynx.js.map +1 -1
- package/runtime/lib/runtime-marker.d.ts +1 -0
- package/runtime/lib/runtime-marker.js +13 -0
- package/runtime/lib/runtime-marker.js.map +1 -0
- package/runtime/lib/shared/component-stack.js +5 -1
- package/runtime/lib/shared/component-stack.js.map +1 -1
- package/runtime/lib/snapshot/debug/profileHooks.js +38 -22
- package/runtime/lib/snapshot/debug/profileHooks.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/error.d.ts +11 -1
- package/runtime/lib/snapshot/lifecycle/patch/error.js +24 -16
- package/runtime/lib/snapshot/lifecycle/patch/error.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.d.ts +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js +29 -7
- package/runtime/lib/snapshot/lifecycle/patch/snapshotPatchApply.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js +2 -2
- package/runtime/lib/snapshot/lifecycle/patch/updateMainThread.js.map +1 -1
- package/runtime/lib/snapshot/lifecycle/reload.js +3 -0
- package/runtime/lib/snapshot/lifecycle/reload.js.map +1 -1
- package/runtime/lib/snapshot/lynx/calledByNative.js +3 -0
- package/runtime/lib/snapshot/lynx/calledByNative.js.map +1 -1
- package/runtime/lib/snapshot/lynx/children.d.ts +2 -0
- package/runtime/lib/snapshot/lynx/children.js.map +1 -1
- package/runtime/lib/snapshot/lynx/element.d.ts +10 -0
- package/runtime/lib/snapshot/lynx/element.js +6 -0
- package/runtime/lib/snapshot/lynx/element.js.map +1 -1
- package/runtime/lib/snapshot/renderToOpcodes/opcodes.js +2 -1
- package/runtime/lib/snapshot/renderToOpcodes/opcodes.js.map +1 -1
- package/runtime/lib/snapshot/worklet/call/runOnMainThread.d.ts +2 -0
- package/runtime/lib/snapshot/worklet/call/runOnMainThread.js +2 -0
- package/runtime/lib/snapshot/worklet/call/runOnMainThread.js.map +1 -1
- package/runtime/lib/snapshot/worklet/ref/updateInitValue.js.map +1 -1
- package/runtime/lib/tsconfig.tsbuildinfo +1 -1
- package/runtime/lib/worklet-runtime/bindings/bindings.d.ts +3 -3
- package/runtime/lib/worklet-runtime/bindings/bindings.js +9 -1
- package/runtime/lib/worklet-runtime/bindings/bindings.js.map +1 -1
- package/runtime/lib/worklet-runtime/bindings/types.d.ts +3 -6
- package/runtime/lib/worklet-runtime/bindings/types.js.map +1 -1
- package/runtime/lib/worklet-runtime/hydrate.js +27 -8
- package/runtime/lib/worklet-runtime/hydrate.js.map +1 -1
- package/runtime/lib/worklet-runtime/mainThreadObject.d.ts +8 -0
- package/runtime/lib/worklet-runtime/mainThreadObject.js +67 -0
- package/runtime/lib/worklet-runtime/mainThreadObject.js.map +1 -0
- package/runtime/lib/worklet-runtime/workletRef.d.ts +11 -7
- package/runtime/lib/worklet-runtime/workletRef.js +72 -9
- package/runtime/lib/worklet-runtime/workletRef.js.map +1 -1
- package/runtime/lib/worklet-runtime/workletRuntime.js +69 -19
- package/runtime/lib/worklet-runtime/workletRuntime.js.map +1 -1
- package/runtime/package.json +2 -3
- package/runtime/src/core/background-function/run-on-background.ts +16 -0
- package/runtime/src/core/capture-main-thread-object.ts +17 -0
- package/runtime/src/core/globalProps.ts +2 -0
- package/runtime/src/core/hooks/react.ts +6 -0
- package/runtime/src/core/hooks/useLynxGlobalEventListener.ts +2 -0
- package/runtime/src/core/initData.ts +2 -0
- package/runtime/src/core/lynx/runtime-version.ts +62 -0
- package/runtime/src/core/main-thread-object-handle-registry.ts +25 -0
- package/runtime/src/core/main-thread-object.ts +387 -0
- package/runtime/src/core/main-thread-ref-id.ts +16 -0
- package/runtime/src/core/main-thread-ref-init-value.ts +12 -4
- package/runtime/src/core/main-thread-ref.ts +7 -8
- package/runtime/src/element-template/background/commit-hook.ts +1 -1
- package/runtime/src/element-template/background/document.ts +3 -3
- package/runtime/src/element-template/background/hydrate.ts +61 -29
- package/runtime/src/element-template/background/instance.ts +5 -6
- package/runtime/src/element-template/debug/profile.ts +48 -36
- package/runtime/src/element-template/index.ts +7 -1
- package/runtime/src/element-template/internal.ts +14 -1
- package/runtime/src/element-template/native/index.ts +4 -0
- package/runtime/src/element-template/protocol/types.ts +1 -1
- package/runtime/src/element-template/runtime/list/list.ts +4 -0
- package/runtime/src/element-template/runtime/render/create-rendered-host.ts +126 -0
- package/runtime/src/element-template/runtime/render/host.ts +51 -0
- package/runtime/src/element-template/runtime/render/render-components.ts +255 -0
- package/runtime/src/element-template/runtime/render/render-direct.ts +275 -0
- package/runtime/src/element-template/runtime/render/render-main-thread.ts +10 -9
- package/runtime/src/element-template/runtime/template/handle.ts +17 -7
- package/runtime/src/element-template/runtime-marker.ts +17 -0
- package/runtime/src/index.ts +1 -1
- package/runtime/src/internal.ts +11 -1
- package/runtime/src/lynx-api.ts +53 -6
- package/runtime/src/lynx.ts +5 -1
- package/runtime/src/runtime-marker.ts +17 -0
- package/runtime/src/shared/component-stack.ts +5 -1
- package/runtime/src/snapshot/debug/profileHooks.ts +52 -40
- package/runtime/src/snapshot/lifecycle/patch/error.ts +36 -19
- package/runtime/src/snapshot/lifecycle/patch/snapshotPatchApply.ts +29 -7
- package/runtime/src/snapshot/lifecycle/patch/updateMainThread.ts +2 -2
- package/runtime/src/snapshot/lifecycle/reload.ts +4 -0
- package/runtime/src/snapshot/lynx/calledByNative.ts +4 -0
- package/runtime/src/snapshot/lynx/children.ts +2 -0
- package/runtime/src/snapshot/lynx/element.ts +10 -0
- package/runtime/src/snapshot/renderToOpcodes/opcodes.ts +2 -1
- package/runtime/src/snapshot/worklet/call/runOnMainThread.ts +2 -0
- package/runtime/src/snapshot/worklet/ref/updateInitValue.ts +1 -1
- package/runtime/src/worklet-runtime/bindings/bindings.ts +17 -2
- package/runtime/src/worklet-runtime/bindings/types.ts +7 -11
- package/runtime/src/worklet-runtime/hydrate.ts +44 -11
- package/runtime/src/worklet-runtime/mainThreadObject.ts +117 -0
- package/runtime/src/worklet-runtime/workletRef.ts +120 -19
- package/runtime/src/worklet-runtime/workletRuntime.ts +84 -21
- package/runtime/types/types.d.ts +2 -0
- package/runtime/worklet-runtime/dev.js +155 -30
- package/runtime/worklet-runtime/main.js +1 -1
- package/testing-library/.turbo/turbo-build.log +12 -12
- package/testing-library/dist/env/index.js +2 -2
- package/testing-library/dist/index.d.ts +167 -4
- package/testing-library/dist/plugins/index.js +1 -1
- package/testing-library/dist/pure.js +45 -44
- package/testing-library/etc/react-lynx-testing-library.api.md +1 -1
- package/testing-library/package.json +4 -3
- package/testing-library/src/__tests__/lynx.test.jsx +1 -1
- package/testing-library/src/__tests__/reload/reloadGc.test.jsx +64 -0
- package/testing-library/src/__tests__/reload/reloadTemplate.js +39 -0
- package/testing-library/src/__tests__/worklet.test.jsx +46 -1
- package/testing-library/tsconfig.docs.json +9 -0
- package/testing-library/typedoc.json +22 -0
- package/testing-library/types/entry.d.ts +2 -2
- package/transform/.turbo/turbo-build$colon$wasm.log +151 -67
- package/transform/__test__/fixture.spec.js +89 -50
- package/transform/__test__/object-methods.spec.js +84 -0
- package/transform/__test__/text.spec.js +75 -0
- package/transform/crates/swc_plugin_compat/lib.rs +4 -3
- package/transform/crates/swc_plugin_compat/napi.rs +4 -3
- package/transform/crates/swc_plugin_define_dce/napi.rs +1 -1
- package/transform/crates/swc_plugin_dynamic_import/lib.rs +105 -3
- package/transform/crates/swc_plugin_dynamic_import/tests/__swc_snapshots__/lib.rs/should_suffix_webpack_chunk_name.js +5 -0
- package/transform/crates/swc_plugin_element_template/host_lowering.rs +111 -0
- package/transform/crates/swc_plugin_element_template/lib.rs +41 -5
- package/transform/crates/swc_plugin_element_template/lowering.rs +39 -7
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_generate_attribute_slots_for_dynamic_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_background_conditional_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_boolean_and_number_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_complex_text_structure.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_dataset_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_deeply_nested_user_components.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_dynamic_class_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_events_lepus.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_id_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_inline_styles.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_interpolated_text_with_siblings_lepus.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_mixed_content.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_nested_structure_and_dynamic_content.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_refs_lepus.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_sibling_user_components.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_spread_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_handle_user_component.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_isolate_arrays_with_child_slot_placeholder.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_keep_code_and_template_attribute_slots_in_sync_for_spread.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_output_element_template_simple_lepus.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_output_template_with_static_attributes.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_verify_template_structure_complex.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/__combined_snapshots__/should_verify_text_attribute_and_child_text_slots.snap +1 -1
- package/transform/crates/swc_plugin_element_template/tests/element_template.rs +70 -66
- package/transform/crates/swc_plugin_element_template/tests/element_template_contract.rs +94 -8
- package/transform/crates/swc_plugin_shake/lib.rs +1 -1
- package/transform/crates/swc_plugin_shake/napi.rs +1 -1
- package/transform/crates/swc_plugin_text/lib.rs +57 -16
- package/transform/crates/swc_plugin_text/tests/__swc_snapshots__/lib.rs/should_keep_children_for_dynamic_or_multiple_text.js +13 -0
- package/transform/crates/swc_plugin_text/tests/__swc_snapshots__/lib.rs/should_transform_string_expressions_to_text_attr.js +13 -0
- package/transform/crates/swc_plugin_worklet/decl_collect.rs +10 -0
- package/transform/crates/swc_plugin_worklet/extract_ident.rs +14 -0
- package/transform/crates/swc_plugin_worklet/gen_stmt.rs +202 -3
- package/transform/crates/swc_plugin_worklet/lib.rs +468 -3
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_js.js +8 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_capture_env_lepus.js +10 -5
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_js.js +27 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_expose_object_method_capture_channels_lepus.js +45 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_js.js +16 -14
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_2_lepus.js +16 -16
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_js.js +7 -5
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_extract_member_expr_lepus.js +7 -7
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_computed_object_method_key_lepus.js +24 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_not_capture_object_method_local_decls_lepus.js +16 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_js.js +42 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_preserve_main_thread_objects_in_class_lepus.js +58 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_js.js +3 -1
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_lepus.js +3 -3
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_js.js +9 -0
- package/transform/crates/swc_plugin_worklet/tests/__swc_snapshots__/lib.rs/should_transform_object_methods_lepus.js +16 -0
- package/transform/crates/swc_plugins_shared/jsx_helpers.rs +8 -4
- package/transform/dist/wasm.cjs +1 -1
- package/transform/index.d.ts +7 -6
- package/transform/rspack-napi/tsconfig.tsbuildinfo +1 -1
- package/transform/src/swc_plugin_extract_str/mod.rs +1 -1
- package/transform/swc-plugin-reactlynx/.turbo/turbo-build.log +63 -270
- package/transform/swc-plugin-reactlynx/index.d.ts +2 -2
- package/transform/swc-plugin-reactlynx/swc_plugin_reactlynx.wasm +0 -0
- package/transform/swc-plugin-reactlynx-compat/.turbo/turbo-build.log +306 -170
- package/transform/swc-plugin-reactlynx-compat/index.d.ts +3 -3
- package/transform/swc-plugin-reactlynx-compat/swc_plugin_reactlynx_compat.wasm +0 -0
- package/transform/vitest.config.ts +5 -1
- package/types/react.docs.d.ts +130 -11
- package/runtime/__test__/element-template/fixtures/render/opcodes-into-element-template/creates-template-from-attrs-and-slot-text/case.ts +0 -39
- package/runtime/__test__/element-template/fixtures/render/opcodes-into-element-template/handles-multiple-text-nodes-in-the-same-slot/case.ts +0 -29
- package/runtime/__test__/element-template/fixtures/render/opcodes-into-element-template/inserts-nested-templates-into-parent-slots/case.ts +0 -42
- package/runtime/__test__/element-template/fixtures/render/opcodes-into-element-template/keeps-slot-children-separated-and-ordered/case.ts +0 -31
- package/runtime/__test__/element-template/fixtures/render/opcodes-into-element-template/passes-bundle-url-to-native-create/case.ts +0 -20
- package/runtime/__test__/element-template/runtime/render/render-to-opcodes.et.test.jsx +0 -522
- package/runtime/lib/element-template/runtime/render/render-opcodes.js +0 -216
- package/runtime/lib/element-template/runtime/render/render-opcodes.js.map +0 -1
- package/runtime/lib/element-template/runtime/render/render-to-opcodes.d.ts +0 -16
- package/runtime/lib/element-template/runtime/render/render-to-opcodes.js +0 -324
- package/runtime/lib/element-template/runtime/render/render-to-opcodes.js.map +0 -1
- package/runtime/lib/element-template/runtime-backend-marker.js +0 -6
- package/runtime/lib/element-template/runtime-backend-marker.js.map +0 -1
- package/runtime/lib/runtime-backend-marker.js +0 -6
- package/runtime/lib/runtime-backend-marker.js.map +0 -1
- package/runtime/src/element-template/runtime/render/render-opcodes.ts +0 -313
- package/runtime/src/element-template/runtime/render/render-to-opcodes.ts +0 -411
- package/runtime/src/element-template/runtime-backend-marker.ts +0 -7
- package/runtime/src/runtime-backend-marker.ts +0 -7
- package/testing-library/node_modules/.bin/rsbuild +0 -55
- package/testing-library/node_modules/.bin/rspeedy +0 -55
- package/testing-library/node_modules/.bin/rstest +0 -55
- package/transform/node_modules/.bin/esbuild +0 -46
- package/transform/node_modules/.bin/napi +0 -55
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/appends-root-text-via-append-element/native-log.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/appends-root-text-via-append-element/output.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/creates-template-from-attrs-and-slot-text/native-log.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/creates-template-from-attrs-and-slot-text/output.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/handles-multiple-template-children-in-the-same-slot/native-log.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/handles-multiple-template-children-in-the-same-slot/output.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/handles-multiple-text-nodes-in-the-same-slot/native-log.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/handles-multiple-text-nodes-in-the-same-slot/output.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/inserts-nested-templates-into-parent-slots/native-log.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/inserts-nested-templates-into-parent-slots/output.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/keeps-slot-children-separated-and-ordered/native-log.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/keeps-slot-children-separated-and-ordered/output.txt +0 -0
- /package/runtime/__test__/element-template/fixtures/render/{opcodes-into-element-template → direct-native}/passes-bundle-url-to-native-create/native-log.txt +0 -0
- /package/runtime/lib/{element-template/runtime-backend-marker.d.ts → core/capture-main-thread-object.d.ts} +0 -0
- /package/runtime/lib/{runtime-backend-marker.d.ts → core/main-thread-ref-id.d.ts} +0 -0
|
@@ -101,6 +101,22 @@ export function resetBackgroundFunctionRuntime(): void {
|
|
|
101
101
|
* @param f - The js function to be called.
|
|
102
102
|
* @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the background thread. This function returns a promise that resolves to the return value of the js function.
|
|
103
103
|
* @public
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
*
|
|
107
|
+
* ```ts
|
|
108
|
+
* import { runOnBackground } from '@lynx-js/react';
|
|
109
|
+
*
|
|
110
|
+
* async function someMainthreadFunction() {
|
|
111
|
+
* 'main thread';
|
|
112
|
+
* const fn = runOnBackground(() => {
|
|
113
|
+
* return 'hello';
|
|
114
|
+
* });
|
|
115
|
+
* const result = await fn();
|
|
116
|
+
* }
|
|
117
|
+
* ```
|
|
118
|
+
*
|
|
119
|
+
* @group Functions
|
|
104
120
|
*/
|
|
105
121
|
export function runOnBackground<R, Fn extends (...args: any[]) => R>(
|
|
106
122
|
f: Fn,
|
|
@@ -0,0 +1,17 @@
|
|
|
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 { isMainThreadObjectHandle } from './main-thread-object-handle-registry.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Preserve opaque main-thread object handles when the worklet transform narrows a
|
|
9
|
+
* captured member expression. Ordinary objects keep the transform's compact
|
|
10
|
+
* fallback shape. The transform evaluates that shape lazily with
|
|
11
|
+
* `captureMainThreadObject(source) ?? fallback`.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export function captureMainThreadObject<T>(source: T): T | undefined {
|
|
16
|
+
return isMainThreadObjectHandle(source) ? source : undefined;
|
|
17
|
+
}
|
|
@@ -133,7 +133,10 @@ const useState: typeof usePreactState = (__BACKGROUND__ && isProfiling)
|
|
|
133
133
|
* @param effect - Imperative function that can return a cleanup function
|
|
134
134
|
* @param deps - If present, effect will only activate if the values in the list change (using ===).
|
|
135
135
|
*
|
|
136
|
+
* @function
|
|
136
137
|
* @public
|
|
138
|
+
*
|
|
139
|
+
* @group Hooks
|
|
137
140
|
*/
|
|
138
141
|
const useEffect: (effect: EffectCallback, deps?: DependencyList) => void = (__BACKGROUND__ && isProfiling)
|
|
139
142
|
? useEffectProfiled
|
|
@@ -147,8 +150,11 @@ const useEffect: (effect: EffectCallback, deps?: DependencyList) => void = (__BA
|
|
|
147
150
|
* @param effect - Imperative function that can return a cleanup function
|
|
148
151
|
* @param deps - If present, effect will only activate if the values in the list change (using ===).
|
|
149
152
|
*
|
|
153
|
+
* @function
|
|
150
154
|
* @public
|
|
151
155
|
*
|
|
156
|
+
* @group Hooks
|
|
157
|
+
*
|
|
152
158
|
* @deprecated `useLayoutEffect` in the background thread cannot offer the precise timing for reading layout information and synchronously re-render, which is different from React.
|
|
153
159
|
*/
|
|
154
160
|
const useLayoutEffect: (effect: EffectCallback, deps?: DependencyList) => void = (__BACKGROUND__ && isProfiling)
|
|
@@ -30,6 +30,8 @@ import { useEffect, useMemo, useRef } from '@lynx-js/react/hooks';
|
|
|
30
30
|
* @param eventName - Event name to listen
|
|
31
31
|
* @param listener - Event handler
|
|
32
32
|
* @public
|
|
33
|
+
*
|
|
34
|
+
* @group Hooks
|
|
33
35
|
*/
|
|
34
36
|
export function useLynxGlobalEventListener<T extends (...args: any[]) => void>(
|
|
35
37
|
eventName: string,
|
|
@@ -123,6 +123,8 @@ export const RESET_WITH_INIT_DATA_IN_STATE_ERROR =
|
|
|
123
123
|
* export default withInitDataInState(App);
|
|
124
124
|
* ```
|
|
125
125
|
* @public
|
|
126
|
+
*
|
|
127
|
+
* @group Functions
|
|
126
128
|
*/
|
|
127
129
|
export function withInitDataInState<P, S>(App: ComponentClass<P, S>): ComponentClass<P, S> {
|
|
128
130
|
const isClassComponent = 'prototype' in App && 'render' in App.prototype;
|
|
@@ -0,0 +1,62 @@
|
|
|
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
|
+
export const sRuntimeVersion: symbol = Symbol.for('__REACT_LYNX_RUNTIME_VERSION__');
|
|
6
|
+
|
|
7
|
+
function getTarget(): typeof globalThis & Record<symbol, unknown> {
|
|
8
|
+
return (__LEPUS__ ? globalThis : lynx) as typeof globalThis & Record<symbol, unknown>;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Record the `@lynx-js/react` runtime version compiled into this bundle.
|
|
13
|
+
*
|
|
14
|
+
* Every lazy bundle bundles its own copy of the `@lynx-js/react` runtime. The
|
|
15
|
+
* host template is loaded first, so the first registration wins and the
|
|
16
|
+
* recorded value reflects the host's runtime version. This function only
|
|
17
|
+
* records the version so it is available for later inspection; it does not
|
|
18
|
+
* compare versions or throw.
|
|
19
|
+
*/
|
|
20
|
+
export function registerRuntimeVersion(version: string | undefined): void {
|
|
21
|
+
// Older versions of the build plugin do not stamp `__RUNTIME_VERSION__`, so
|
|
22
|
+
// there is nothing to record.
|
|
23
|
+
if (!version) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const target = getTarget();
|
|
28
|
+
const currentVersion = target[sRuntimeVersion] as string | undefined;
|
|
29
|
+
|
|
30
|
+
// Log host and bundle versions so online reports can diagnose mismatches.
|
|
31
|
+
// Only emit on the background thread — the main-thread copy would be a
|
|
32
|
+
// duplicate report of the same values.
|
|
33
|
+
// TODO: on repeated registration, compare and surface a compatibility check.
|
|
34
|
+
if (__JS__) {
|
|
35
|
+
console.alog?.(
|
|
36
|
+
`[ReactLynx] new runtime version: ${version}${
|
|
37
|
+
currentVersion === undefined ? '' : `, current version: ${currentVersion}`
|
|
38
|
+
}`,
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
// The host registers first; keep its version and let later lazy bundles be
|
|
43
|
+
// no-ops so the recorded value always represents the host runtime.
|
|
44
|
+
if (currentVersion !== undefined) {
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
Object.defineProperty(target, sRuntimeVersion, {
|
|
49
|
+
value: version,
|
|
50
|
+
enumerable: false,
|
|
51
|
+
writable: false,
|
|
52
|
+
configurable: true,
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
/**
|
|
57
|
+
* Read the `@lynx-js/react` runtime version recorded by the host template, or
|
|
58
|
+
* `undefined` when the host was built by a plugin that does not stamp it.
|
|
59
|
+
*/
|
|
60
|
+
export function getRuntimeVersion(): string | undefined {
|
|
61
|
+
return getTarget()[sRuntimeVersion] as string | undefined;
|
|
62
|
+
}
|
|
@@ -0,0 +1,25 @@
|
|
|
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
|
+
let mainThreadObjectHandleTypes: WeakMap<object, string> | undefined;
|
|
6
|
+
|
|
7
|
+
export function registerMainThreadObjectHandle(
|
|
8
|
+
handle: object,
|
|
9
|
+
type: string,
|
|
10
|
+
): void {
|
|
11
|
+
(mainThreadObjectHandleTypes ??= new WeakMap()).set(handle, type);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export function isMainThreadObjectHandle(value: unknown): value is object {
|
|
15
|
+
return typeof value === 'object' && value !== null
|
|
16
|
+
&& mainThreadObjectHandleTypes?.has(value) === true;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function getMainThreadObjectHandleType(
|
|
20
|
+
value: unknown,
|
|
21
|
+
): string | undefined {
|
|
22
|
+
return typeof value === 'object' && value !== null
|
|
23
|
+
? mainThreadObjectHandleTypes?.get(value)
|
|
24
|
+
: undefined;
|
|
25
|
+
}
|
|
@@ -0,0 +1,387 @@
|
|
|
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 {
|
|
6
|
+
WorkletEvents,
|
|
7
|
+
loadWorkletRuntime,
|
|
8
|
+
registerMainThreadObjectType,
|
|
9
|
+
} from '@lynx-js/react/worklet-runtime/bindings';
|
|
10
|
+
import type { Worklet, WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bindings';
|
|
11
|
+
|
|
12
|
+
import { useMemo } from './hooks/react.js';
|
|
13
|
+
import { getMainThreadObjectHandleType, registerMainThreadObjectHandle } from './main-thread-object-handle-registry.js';
|
|
14
|
+
import { allocateMainThreadRefId } from './main-thread-ref-id.js';
|
|
15
|
+
import { addMainThreadRefInitValue } from './main-thread-ref-init-value.js';
|
|
16
|
+
|
|
17
|
+
const mainThreadObjectTypeDefinitions = new WeakMap<
|
|
18
|
+
object,
|
|
19
|
+
MainThreadObjectTypeDefinition<unknown, object>
|
|
20
|
+
>();
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* Describes how a serializable initialization payload becomes a stable object
|
|
24
|
+
* in the main-thread runtime.
|
|
25
|
+
*
|
|
26
|
+
* @public
|
|
27
|
+
*/
|
|
28
|
+
export interface MainThreadObjectTypeDefinition<I, O extends object> {
|
|
29
|
+
/** A globally unique and stable type key. */
|
|
30
|
+
readonly type: string;
|
|
31
|
+
/**
|
|
32
|
+
* Create the realized main-thread object.
|
|
33
|
+
*
|
|
34
|
+
* This must be a capture-free Main Thread Function. Imports used by the
|
|
35
|
+
* function must come from shared-runtime modules.
|
|
36
|
+
*/
|
|
37
|
+
readonly create: (initialValue: I) => O;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* An immutable MainThreadObject type token with source-handle inspection.
|
|
42
|
+
*
|
|
43
|
+
* The factory function belongs to the input definition and is not exposed
|
|
44
|
+
* by the returned token. In the background runtime they compile to opaque
|
|
45
|
+
* Main Thread Function descriptors rather than bundling their implementation.
|
|
46
|
+
*
|
|
47
|
+
* @public
|
|
48
|
+
*/
|
|
49
|
+
export interface MainThreadObjectType<I, O extends object> {
|
|
50
|
+
/** A globally unique and stable type key. */
|
|
51
|
+
readonly type: string;
|
|
52
|
+
/**
|
|
53
|
+
* Narrow a value to a source-runtime handle of this exact type.
|
|
54
|
+
*
|
|
55
|
+
* This does not create or access the realized main-thread object.
|
|
56
|
+
*/
|
|
57
|
+
readonly downcast: (value: unknown) => MainThreadObjectHandle<I, O> | undefined;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* An opaque source-runtime handle for an object realized on the main thread.
|
|
62
|
+
*
|
|
63
|
+
* A handle has logical identity and is serializable, but it is not the target
|
|
64
|
+
* object or an RPC stub. Capturing it in a main-thread function resolves it to
|
|
65
|
+
* the corresponding local target object. Development builds may wrap the
|
|
66
|
+
* background handle in a throwing guard solely to diagnose invalid access;
|
|
67
|
+
* that guard never forwards operations between runtimes.
|
|
68
|
+
*
|
|
69
|
+
* @public
|
|
70
|
+
*/
|
|
71
|
+
export abstract class MainThreadObjectHandle<I, O extends object> {
|
|
72
|
+
/** @internal */
|
|
73
|
+
declare private readonly _mainThreadObjectTargetType: O;
|
|
74
|
+
/** @internal */
|
|
75
|
+
protected _wvid: number;
|
|
76
|
+
/** @internal */
|
|
77
|
+
protected _initValue: I;
|
|
78
|
+
/** @internal */
|
|
79
|
+
protected _type: string;
|
|
80
|
+
/** @internal */
|
|
81
|
+
protected _releaseObserver?: unknown;
|
|
82
|
+
|
|
83
|
+
/** @internal */
|
|
84
|
+
protected constructor(initialValue: I, type: string) {
|
|
85
|
+
if (__DEV__ && __JS__) {
|
|
86
|
+
assertSerializableMainThreadObjectPayload(initialValue, type);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
this._wvid = allocateMainThreadRefId();
|
|
90
|
+
this._initValue = initialValue;
|
|
91
|
+
this._type = type;
|
|
92
|
+
registerMainThreadObjectHandle(this, type);
|
|
93
|
+
|
|
94
|
+
if (__JS__) {
|
|
95
|
+
addMainThreadRefInitValue(
|
|
96
|
+
this._wvid,
|
|
97
|
+
initialValue,
|
|
98
|
+
type,
|
|
99
|
+
);
|
|
100
|
+
|
|
101
|
+
const id = this._wvid;
|
|
102
|
+
this._releaseObserver = lynx.getNativeApp().createJSObjectDestructionObserver?.(() => {
|
|
103
|
+
lynx.getCoreContext?.().dispatchEvent({
|
|
104
|
+
type: WorkletEvents.releaseWorkletRef,
|
|
105
|
+
data: { id },
|
|
106
|
+
});
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The payload used to create the realized object.
|
|
113
|
+
*
|
|
114
|
+
* Readonly is a TypeScript contract; the value is not frozen at runtime.
|
|
115
|
+
*/
|
|
116
|
+
public get creationPayload(): Readonly<I> {
|
|
117
|
+
return this._initValue;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/** @internal */
|
|
121
|
+
toJSON(): Pick<WorkletRefImpl<unknown>, '_wvid' | '_initValue' | '_type'> {
|
|
122
|
+
return {
|
|
123
|
+
_wvid: this._wvid,
|
|
124
|
+
_initValue: this._initValue,
|
|
125
|
+
_type: this._type,
|
|
126
|
+
};
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
class MainThreadObjectHandleImpl<I, O extends object> extends MainThreadObjectHandle<I, O> {
|
|
131
|
+
constructor(initialValue: I, type: string) {
|
|
132
|
+
super(initialValue, type);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/**
|
|
137
|
+
* Define a main-thread object type for use by a library-provided hook.
|
|
138
|
+
*
|
|
139
|
+
* @param definition - Stable type key and target-object factory function.
|
|
140
|
+
* @returns An immutable object type definition.
|
|
141
|
+
* @public
|
|
142
|
+
*/
|
|
143
|
+
export function defineMainThreadObjectType<I, O extends object>(
|
|
144
|
+
definition: MainThreadObjectTypeDefinition<I, O>,
|
|
145
|
+
): MainThreadObjectType<I, O> {
|
|
146
|
+
// Read accessor-backed fields once so validation and deferred registration
|
|
147
|
+
// operate on the same definition values.
|
|
148
|
+
const { type, create } = definition;
|
|
149
|
+
if (typeof type !== 'string' || type.length === 0) {
|
|
150
|
+
throw new Error('MainThreadObject type must be a non-empty string.');
|
|
151
|
+
}
|
|
152
|
+
if (type === 'main-thread') {
|
|
153
|
+
throw new Error(
|
|
154
|
+
'MainThreadObject type "main-thread" is reserved for MainThreadRef.',
|
|
155
|
+
);
|
|
156
|
+
}
|
|
157
|
+
if (!isMainThreadLifecycleFunction(create)) {
|
|
158
|
+
throw new Error(
|
|
159
|
+
`MainThreadObject type "${type}" must provide a create Main Thread Function.`,
|
|
160
|
+
);
|
|
161
|
+
}
|
|
162
|
+
if (__DEV__) {
|
|
163
|
+
assertCaptureFreeLifecycleFunction(type, 'create', create);
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
// Keep the validated fields independent from the caller-owned definition.
|
|
167
|
+
// Registration may be deferred until the first main-thread hook use, so a
|
|
168
|
+
// later mutation of `definition` must not change the type or factory that is
|
|
169
|
+
// registered for this token.
|
|
170
|
+
const validatedDefinition = Object.freeze({
|
|
171
|
+
type,
|
|
172
|
+
create,
|
|
173
|
+
});
|
|
174
|
+
const objectType = Object.freeze({
|
|
175
|
+
type,
|
|
176
|
+
downcast(value: unknown): MainThreadObjectHandle<I, O> | undefined {
|
|
177
|
+
return getMainThreadObjectHandleType(value) === type
|
|
178
|
+
? value as MainThreadObjectHandle<I, O>
|
|
179
|
+
: undefined;
|
|
180
|
+
},
|
|
181
|
+
});
|
|
182
|
+
mainThreadObjectTypeDefinitions.set(
|
|
183
|
+
objectType,
|
|
184
|
+
validatedDefinition as unknown as MainThreadObjectTypeDefinition<unknown, object>,
|
|
185
|
+
);
|
|
186
|
+
registerMainThreadObjectDefinition(validatedDefinition);
|
|
187
|
+
return objectType;
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Create a stable handle that resolves to an object of the supplied type in
|
|
192
|
+
* main-thread functions.
|
|
193
|
+
*
|
|
194
|
+
* This primitive is intended for library hooks. The returned target type is
|
|
195
|
+
* only callable in a main-thread function; it must not be used from the
|
|
196
|
+
* background runtime.
|
|
197
|
+
*
|
|
198
|
+
* @param objectType - Definition used to realize the target object.
|
|
199
|
+
* @param initialValue - JSON-serializable initialization payload.
|
|
200
|
+
* @returns The main-thread-only target type.
|
|
201
|
+
* @public
|
|
202
|
+
*/
|
|
203
|
+
export function useMainThreadObject<I, O extends object>(
|
|
204
|
+
objectType: MainThreadObjectType<I, O>,
|
|
205
|
+
initialValue: I,
|
|
206
|
+
): O {
|
|
207
|
+
return useMemo(() => {
|
|
208
|
+
const definition = mainThreadObjectTypeDefinitions.get(objectType);
|
|
209
|
+
if (definition === undefined) {
|
|
210
|
+
throw new Error(
|
|
211
|
+
`Invalid MainThreadObject type token for "${objectType.type}". Create it with defineMainThreadObjectType().`,
|
|
212
|
+
);
|
|
213
|
+
}
|
|
214
|
+
// A library module normally registers its type while the MTS bundle is
|
|
215
|
+
// evaluated. Register again at the first hook use so runtimes that retain
|
|
216
|
+
// the module but reset their per-page worklet registry remain correct.
|
|
217
|
+
// The registry treats an equivalent duplicate as an idempotent lookup.
|
|
218
|
+
registerMainThreadObjectDefinition(definition);
|
|
219
|
+
const handle = new MainThreadObjectHandleImpl<I, O>(initialValue, objectType.type);
|
|
220
|
+
return guardBackgroundMainThreadObjectAccess(handle, objectType.type) as unknown as O;
|
|
221
|
+
}, []);
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function registerMainThreadObjectDefinition<I, O extends object>(
|
|
225
|
+
definition: MainThreadObjectTypeDefinition<I, O>,
|
|
226
|
+
): void {
|
|
227
|
+
if (__JS__) {
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const schema = (globalThis as { globDynamicComponentEntry?: string })
|
|
232
|
+
.globDynamicComponentEntry;
|
|
233
|
+
loadWorkletRuntime(schema);
|
|
234
|
+
registerMainThreadObjectType(
|
|
235
|
+
definition.type,
|
|
236
|
+
definition.create as ((initialValue: unknown) => object) | Worklet,
|
|
237
|
+
);
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function isMainThreadLifecycleFunction(
|
|
241
|
+
value: unknown,
|
|
242
|
+
): value is ((...args: never[]) => unknown) | Worklet {
|
|
243
|
+
return typeof value === 'function'
|
|
244
|
+
|| (typeof value === 'object' && value !== null
|
|
245
|
+
&& typeof (value as Partial<Worklet>)._wkltId === 'string');
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
function assertCaptureFreeLifecycleFunction(
|
|
249
|
+
type: string,
|
|
250
|
+
name: 'create',
|
|
251
|
+
value: ((...args: never[]) => unknown) | Worklet,
|
|
252
|
+
): void {
|
|
253
|
+
if (typeof value === 'function') {
|
|
254
|
+
return;
|
|
255
|
+
}
|
|
256
|
+
const descriptors = Object.getOwnPropertyDescriptors(value);
|
|
257
|
+
const hasCaptures = Reflect.ownKeys(descriptors).some(key => {
|
|
258
|
+
if (typeof key !== 'string') {
|
|
259
|
+
return true;
|
|
260
|
+
}
|
|
261
|
+
if (key === '_wkltId' || key === '_workletType' || key === '_lepusWorkletHash') {
|
|
262
|
+
return false;
|
|
263
|
+
}
|
|
264
|
+
if (key === '_c') {
|
|
265
|
+
const descriptor = descriptors[key]!;
|
|
266
|
+
const closure = 'value' in descriptor ? descriptor.value : undefined;
|
|
267
|
+
return !('value' in descriptor)
|
|
268
|
+
|| (closure !== undefined
|
|
269
|
+
&& (typeof closure !== 'object' || closure === null
|
|
270
|
+
|| Reflect.ownKeys(closure).length !== 0));
|
|
271
|
+
}
|
|
272
|
+
return true;
|
|
273
|
+
});
|
|
274
|
+
if (hasCaptures) {
|
|
275
|
+
throw new Error(
|
|
276
|
+
`MainThreadObject ${name} function for "${type}" must not capture values. Import dependencies from a shared-runtime module instead.`,
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
function guardBackgroundMainThreadObjectAccess<I, O extends object>(
|
|
282
|
+
handle: MainThreadObjectHandle<I, O>,
|
|
283
|
+
type: string,
|
|
284
|
+
): MainThreadObjectHandle<I, O> {
|
|
285
|
+
if (!__DEV__ || !__JS__) {
|
|
286
|
+
return handle;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// This development-only guard never forwards a call to the main thread. It
|
|
290
|
+
// only turns an otherwise opaque "method is not a function" failure into a
|
|
291
|
+
// diagnostic. Production handles remain plain serializable objects.
|
|
292
|
+
const guardedHandle = new Proxy(handle, {
|
|
293
|
+
get(target, property, receiver): unknown {
|
|
294
|
+
if (property in target) {
|
|
295
|
+
return Reflect.get(target, property, receiver) as unknown;
|
|
296
|
+
}
|
|
297
|
+
if (
|
|
298
|
+
typeof property === 'symbol'
|
|
299
|
+
|| property === 'then'
|
|
300
|
+
|| property === '$$typeof'
|
|
301
|
+
) {
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
throw new Error(
|
|
305
|
+
`MainThreadObject handle for "${type}" cannot access "${
|
|
306
|
+
String(property)
|
|
307
|
+
}" in the background runtime. Use the object only inside a main-thread function.`,
|
|
308
|
+
);
|
|
309
|
+
},
|
|
310
|
+
set(target, property, value, receiver) {
|
|
311
|
+
if (property in target) {
|
|
312
|
+
return Reflect.set(target, property, value, receiver);
|
|
313
|
+
}
|
|
314
|
+
throw new Error(
|
|
315
|
+
`MainThreadObject handle for "${type}" cannot set "${
|
|
316
|
+
String(property)
|
|
317
|
+
}" in the background runtime. Use the object only inside a main-thread function.`,
|
|
318
|
+
);
|
|
319
|
+
},
|
|
320
|
+
});
|
|
321
|
+
registerMainThreadObjectHandle(guardedHandle, type);
|
|
322
|
+
return guardedHandle;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
function assertSerializableMainThreadObjectPayload(value: unknown, type: string): void {
|
|
326
|
+
const invalidPath = findNonSerializablePath(value, '$', new Set<object>());
|
|
327
|
+
if (invalidPath !== undefined) {
|
|
328
|
+
throw new Error(
|
|
329
|
+
`MainThreadObject initial value for "${type}" must be JSON-serializable; invalid value at ${invalidPath}.`,
|
|
330
|
+
);
|
|
331
|
+
}
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
function findNonSerializablePath(
|
|
335
|
+
value: unknown,
|
|
336
|
+
path: string,
|
|
337
|
+
ancestors: Set<object>,
|
|
338
|
+
): string | undefined {
|
|
339
|
+
if (
|
|
340
|
+
value === undefined
|
|
341
|
+
|| typeof value === 'function'
|
|
342
|
+
|| typeof value === 'symbol'
|
|
343
|
+
|| typeof value === 'bigint'
|
|
344
|
+
|| (typeof value === 'number' && !Number.isFinite(value))
|
|
345
|
+
) {
|
|
346
|
+
return path;
|
|
347
|
+
}
|
|
348
|
+
if (value === null || typeof value !== 'object') {
|
|
349
|
+
return undefined;
|
|
350
|
+
}
|
|
351
|
+
if (ancestors.has(value)) {
|
|
352
|
+
return path;
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
const prototype = Object.getPrototypeOf(value) as object | null;
|
|
356
|
+
if (!Array.isArray(value) && prototype !== Object.prototype && prototype !== null) {
|
|
357
|
+
return path;
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
ancestors.add(value);
|
|
361
|
+
if (Array.isArray(value)) {
|
|
362
|
+
for (let index = 0; index < value.length; index++) {
|
|
363
|
+
if (!Object.prototype.hasOwnProperty.call(value, index)) {
|
|
364
|
+
continue;
|
|
365
|
+
}
|
|
366
|
+
const invalidPath = findNonSerializablePath(
|
|
367
|
+
value[index],
|
|
368
|
+
`${path}.${index}`,
|
|
369
|
+
ancestors,
|
|
370
|
+
);
|
|
371
|
+
if (invalidPath !== undefined) {
|
|
372
|
+
ancestors.delete(value);
|
|
373
|
+
return invalidPath;
|
|
374
|
+
}
|
|
375
|
+
}
|
|
376
|
+
} else {
|
|
377
|
+
for (const [key, item] of Object.entries(value)) {
|
|
378
|
+
const invalidPath = findNonSerializablePath(item, `${path}.${key}`, ancestors);
|
|
379
|
+
if (invalidPath !== undefined) {
|
|
380
|
+
ancestors.delete(value);
|
|
381
|
+
return invalidPath;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
ancestors.delete(value);
|
|
386
|
+
return undefined;
|
|
387
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
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
|
+
let lastIdBG = 0;
|
|
6
|
+
let lastIdMT = 0;
|
|
7
|
+
|
|
8
|
+
/** @internal */
|
|
9
|
+
export function allocateMainThreadRefId(): number {
|
|
10
|
+
return __JS__ ? ++lastIdBG : --lastIdMT;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export function clearMainThreadRefIdsForTesting(): void {
|
|
15
|
+
lastIdBG = lastIdMT = 0;
|
|
16
|
+
}
|
|
@@ -2,21 +2,29 @@
|
|
|
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 {
|
|
5
|
+
import type { MainThreadRefInitValuePatch } from '@lynx-js/react/worklet-runtime/bindings';
|
|
6
6
|
|
|
7
|
-
|
|
7
|
+
import { isMtsEnabled } from './mts-capability.js';
|
|
8
8
|
|
|
9
9
|
let mainThreadRefInitValuePatch: MainThreadRefInitValuePatch = [];
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
12
|
* @internal
|
|
13
13
|
*/
|
|
14
|
-
export function addMainThreadRefInitValue(
|
|
14
|
+
export function addMainThreadRefInitValue(
|
|
15
|
+
id: number,
|
|
16
|
+
value: unknown,
|
|
17
|
+
type?: string,
|
|
18
|
+
): void {
|
|
15
19
|
if (!isMtsEnabled()) {
|
|
16
20
|
return;
|
|
17
21
|
}
|
|
18
22
|
|
|
19
|
-
mainThreadRefInitValuePatch.push(
|
|
23
|
+
mainThreadRefInitValuePatch.push(
|
|
24
|
+
type === undefined
|
|
25
|
+
? [id, value]
|
|
26
|
+
: [id, value, type],
|
|
27
|
+
);
|
|
20
28
|
}
|
|
21
29
|
|
|
22
30
|
/**
|
|
@@ -4,16 +4,13 @@
|
|
|
4
4
|
import type { RefObject } from 'react';
|
|
5
5
|
|
|
6
6
|
import { useMemo } from './hooks/react.js';
|
|
7
|
+
import { allocateMainThreadRefId, clearMainThreadRefIdsForTesting } from './main-thread-ref-id.js';
|
|
7
8
|
import { addMainThreadRefInitValue } from './main-thread-ref-init-value.js';
|
|
8
9
|
import { WorkletEvents } from '../worklet-runtime/bindings/events.js';
|
|
9
10
|
import type { WorkletRefImpl } from '../worklet-runtime/bindings/types.js';
|
|
10
11
|
|
|
11
|
-
// Split into two variables for testing purposes.
|
|
12
|
-
let lastIdBG = 0;
|
|
13
|
-
let lastIdMT = 0;
|
|
14
|
-
|
|
15
12
|
export function clearMainThreadRefLastIdForTesting(): void {
|
|
16
|
-
|
|
13
|
+
clearMainThreadRefIdsForTesting();
|
|
17
14
|
}
|
|
18
15
|
|
|
19
16
|
export function isMainThreadRef(value: unknown): value is WorkletRefImpl<unknown> {
|
|
@@ -28,6 +25,8 @@ export function isMainThreadRef(value: unknown): value is WorkletRefImpl<unknown
|
|
|
28
25
|
* The data saved in `current` property of the `MainThreadRef` can be read and written in
|
|
29
26
|
* multiple main thread functions.
|
|
30
27
|
* @public
|
|
28
|
+
*
|
|
29
|
+
* @group Types
|
|
31
30
|
*/
|
|
32
31
|
export class MainThreadRef<T> {
|
|
33
32
|
/**
|
|
@@ -50,8 +49,8 @@ export class MainThreadRef<T> {
|
|
|
50
49
|
constructor(initValue: T) {
|
|
51
50
|
this._initValue = initValue;
|
|
52
51
|
this._type = 'main-thread';
|
|
52
|
+
this._wvid = allocateMainThreadRefId();
|
|
53
53
|
if (__JS__) {
|
|
54
|
-
this._wvid = ++lastIdBG;
|
|
55
54
|
addMainThreadRefInitValue(this._wvid, initValue);
|
|
56
55
|
const id = this._wvid;
|
|
57
56
|
this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver?.(() => {
|
|
@@ -62,8 +61,6 @@ export class MainThreadRef<T> {
|
|
|
62
61
|
},
|
|
63
62
|
});
|
|
64
63
|
});
|
|
65
|
-
} else {
|
|
66
|
-
this._wvid = --lastIdMT;
|
|
67
64
|
}
|
|
68
65
|
}
|
|
69
66
|
|
|
@@ -134,6 +131,8 @@ export class MainThreadRef<T> {
|
|
|
134
131
|
* ```
|
|
135
132
|
*
|
|
136
133
|
* @public
|
|
134
|
+
*
|
|
135
|
+
* @group Hooks
|
|
137
136
|
*/
|
|
138
137
|
export function useMainThreadRef<T>(initValue: T): MainThreadRef<T>;
|
|
139
138
|
|