@lynx-js/react 0.105.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 +250 -0
- package/LICENSE +202 -0
- package/README.md +20 -0
- package/components/lib/Page.d.ts +3 -0
- package/components/lib/Page.js +56 -0
- package/components/lib/Page.js.map +1 -0
- package/components/lib/index.d.ts +1 -0
- package/components/lib/index.js +5 -0
- package/components/lib/index.js.map +1 -0
- package/components/src/Page.ts +75 -0
- package/components/src/index.ts +5 -0
- package/package.json +162 -0
- package/refresh/.gitignore +1 -0
- package/refresh/.npmignore +3 -0
- package/refresh/.turbo/turbo-build.log +8 -0
- package/refresh/dist/index.js +311 -0
- package/refresh/package.json +21 -0
- package/refresh/src/index.ts +6 -0
- package/refresh/src/prefresh.d.ts +11 -0
- package/refresh/tsconfig.json +4 -0
- package/refresh/turbo.json +15 -0
- package/runtime/jsx-dev-runtime/index.d.ts +5 -0
- package/runtime/jsx-dev-runtime/index.js +4 -0
- package/runtime/jsx-runtime/index.d.ts +4 -0
- package/runtime/jsx-runtime/index.js +4 -0
- package/runtime/lazy/import.d.ts +1 -0
- package/runtime/lazy/import.js +55 -0
- package/runtime/lazy/internal.js +39 -0
- package/runtime/lazy/jsx-dev-runtime.js +12 -0
- package/runtime/lazy/jsx-runtime.js +12 -0
- package/runtime/lazy/legacy-react-runtime.js +20 -0
- package/runtime/lazy/react-lepus.js +9 -0
- package/runtime/lazy/react.js +46 -0
- package/runtime/lazy/target.js +11 -0
- package/runtime/lepus/index.d.ts +4 -0
- package/runtime/lepus/index.js +43 -0
- package/runtime/lepus/jsx-dev-runtime/index.js +4 -0
- package/runtime/lepus/jsx-runtime/index.js +66 -0
- package/runtime/lib/backgroundSnapshot.d.ts +23 -0
- package/runtime/lib/backgroundSnapshot.js +348 -0
- package/runtime/lib/backgroundSnapshot.js.map +1 -0
- package/runtime/lib/compat/componentIs.d.ts +4 -0
- package/runtime/lib/compat/componentIs.js +20 -0
- package/runtime/lib/compat/componentIs.js.map +1 -0
- package/runtime/lib/compat/initData.d.ts +43 -0
- package/runtime/lib/compat/initData.js +93 -0
- package/runtime/lib/compat/initData.js.map +1 -0
- package/runtime/lib/compat/lynxComponent.d.ts +5 -0
- package/runtime/lib/compat/lynxComponent.js +71 -0
- package/runtime/lib/compat/lynxComponent.js.map +1 -0
- package/runtime/lib/debug/debug.d.ts +2 -0
- package/runtime/lib/debug/debug.js +9 -0
- package/runtime/lib/debug/debug.js.map +1 -0
- package/runtime/lib/debug/printSnapshot.d.ts +3 -0
- package/runtime/lib/debug/printSnapshot.js +21 -0
- package/runtime/lib/debug/printSnapshot.js.map +1 -0
- package/runtime/lib/debug/profile.d.ts +1 -0
- package/runtime/lib/debug/profile.js +51 -0
- package/runtime/lib/debug/profile.js.map +1 -0
- package/runtime/lib/document.d.ts +4 -0
- package/runtime/lib/document.js +49 -0
- package/runtime/lib/document.js.map +1 -0
- package/runtime/lib/gesture/processGesture.d.ts +4 -0
- package/runtime/lib/gesture/processGesture.js +48 -0
- package/runtime/lib/gesture/processGesture.js.map +1 -0
- package/runtime/lib/gesture/processGestureBagkround.d.ts +2 -0
- package/runtime/lib/gesture/processGestureBagkround.js +16 -0
- package/runtime/lib/gesture/processGestureBagkround.js.map +1 -0
- package/runtime/lib/gesture/types.d.ts +34 -0
- package/runtime/lib/gesture/types.js +13 -0
- package/runtime/lib/gesture/types.js.map +1 -0
- package/runtime/lib/hooks/react.d.ts +26 -0
- package/runtime/lib/hooks/react.js +35 -0
- package/runtime/lib/hooks/react.js.map +1 -0
- package/runtime/lib/hooks/useLynxGlobalEventListener.d.ts +29 -0
- package/runtime/lib/hooks/useLynxGlobalEventListener.js +53 -0
- package/runtime/lib/hooks/useLynxGlobalEventListener.js.map +1 -0
- package/runtime/lib/hydrate.d.ts +18 -0
- package/runtime/lib/hydrate.js +271 -0
- package/runtime/lib/hydrate.js.map +1 -0
- package/runtime/lib/index.d.ts +9 -0
- package/runtime/lib/index.js +46 -0
- package/runtime/lib/index.js.map +1 -0
- package/runtime/lib/internal.d.ts +26 -0
- package/runtime/lib/internal.js +41 -0
- package/runtime/lib/internal.js.map +1 -0
- package/runtime/lib/legacy-react-runtime/index.d.ts +7 -0
- package/runtime/lib/legacy-react-runtime/index.js +34 -0
- package/runtime/lib/legacy-react-runtime/index.js.map +1 -0
- package/runtime/lib/lifecycle/delayUnmount.d.ts +8 -0
- package/runtime/lib/lifecycle/delayUnmount.js +68 -0
- package/runtime/lib/lifecycle/delayUnmount.js.map +1 -0
- package/runtime/lib/lifecycle/destroy.d.ts +2 -0
- package/runtime/lib/lifecycle/destroy.js +21 -0
- package/runtime/lib/lifecycle/destroy.js.map +1 -0
- package/runtime/lib/lifecycle/pass.d.ts +3 -0
- package/runtime/lib/lifecycle/pass.js +12 -0
- package/runtime/lib/lifecycle/pass.js.map +1 -0
- package/runtime/lib/lifecycle/patchUpdate.d.ts +23 -0
- package/runtime/lib/lifecycle/patchUpdate.js +179 -0
- package/runtime/lib/lifecycle/patchUpdate.js.map +1 -0
- package/runtime/lib/lifecycle/reload.d.ts +3 -0
- package/runtime/lib/lifecycle/reload.js +63 -0
- package/runtime/lib/lifecycle/reload.js.map +1 -0
- package/runtime/lib/lifecycle/render.d.ts +2 -0
- package/runtime/lib/lifecycle/render.js +44 -0
- package/runtime/lib/lifecycle/render.js.map +1 -0
- package/runtime/lib/lifecycleConstant.d.ts +12 -0
- package/runtime/lib/lifecycleConstant.js +17 -0
- package/runtime/lib/lifecycleConstant.js.map +1 -0
- package/runtime/lib/list.d.ts +40 -0
- package/runtime/lib/list.js +305 -0
- package/runtime/lib/list.js.map +1 -0
- package/runtime/lib/lynx/calledByNative.d.ts +2 -0
- package/runtime/lib/lynx/calledByNative.js +113 -0
- package/runtime/lib/lynx/calledByNative.js.map +1 -0
- package/runtime/lib/lynx/component.d.ts +1 -0
- package/runtime/lib/lynx/component.js +69 -0
- package/runtime/lib/lynx/component.js.map +1 -0
- package/runtime/lib/lynx/dynamic-js.d.ts +2 -0
- package/runtime/lib/lynx/dynamic-js.js +31 -0
- package/runtime/lib/lynx/dynamic-js.js.map +1 -0
- package/runtime/lib/lynx/env.d.ts +1 -0
- package/runtime/lib/lynx/env.js +92 -0
- package/runtime/lib/lynx/env.js.map +1 -0
- package/runtime/lib/lynx/injectLepusMethods.d.ts +2 -0
- package/runtime/lib/lynx/injectLepusMethods.js +50 -0
- package/runtime/lib/lynx/injectLepusMethods.js.map +1 -0
- package/runtime/lib/lynx/lazy-bundle.d.ts +9 -0
- package/runtime/lib/lynx/lazy-bundle.js +119 -0
- package/runtime/lib/lynx/lazy-bundle.js.map +1 -0
- package/runtime/lib/lynx/performance.d.ts +29 -0
- package/runtime/lib/lynx/performance.js +100 -0
- package/runtime/lib/lynx/performance.js.map +1 -0
- package/runtime/lib/lynx/tt.d.ts +4 -0
- package/runtime/lib/lynx/tt.js +193 -0
- package/runtime/lib/lynx/tt.js.map +1 -0
- package/runtime/lib/lynx-api.d.ts +311 -0
- package/runtime/lib/lynx-api.js +116 -0
- package/runtime/lib/lynx-api.js.map +1 -0
- package/runtime/lib/lynx.d.ts +2 -0
- package/runtime/lib/lynx.js +91 -0
- package/runtime/lib/lynx.js.map +1 -0
- package/runtime/lib/opcodes.d.ts +2 -0
- package/runtime/lib/opcodes.js +59 -0
- package/runtime/lib/opcodes.js.map +1 -0
- package/runtime/lib/renderToOpcodes/constants.d.ts +17 -0
- package/runtime/lib/renderToOpcodes/constants.js +23 -0
- package/runtime/lib/renderToOpcodes/constants.js.map +1 -0
- package/runtime/lib/renderToOpcodes/index.d.ts +13 -0
- package/runtime/lib/renderToOpcodes/index.js +227 -0
- package/runtime/lib/renderToOpcodes/index.js.map +1 -0
- package/runtime/lib/root.d.ts +7 -0
- package/runtime/lib/root.js +17 -0
- package/runtime/lib/root.js.map +1 -0
- package/runtime/lib/snapshot/event.d.ts +3 -0
- package/runtime/lib/snapshot/event.js +24 -0
- package/runtime/lib/snapshot/event.js.map +1 -0
- package/runtime/lib/snapshot/gesture.d.ts +2 -0
- package/runtime/lib/snapshot/gesture.js +18 -0
- package/runtime/lib/snapshot/gesture.js.map +1 -0
- package/runtime/lib/snapshot/platformInfo.d.ts +4 -0
- package/runtime/lib/snapshot/platformInfo.js +35 -0
- package/runtime/lib/snapshot/platformInfo.js.map +1 -0
- package/runtime/lib/snapshot/ref.d.ts +10 -0
- package/runtime/lib/snapshot/ref.js +119 -0
- package/runtime/lib/snapshot/ref.js.map +1 -0
- package/runtime/lib/snapshot/spread.d.ts +5 -0
- package/runtime/lib/snapshot/spread.js +289 -0
- package/runtime/lib/snapshot/spread.js.map +1 -0
- package/runtime/lib/snapshot/workletEvent.d.ts +3 -0
- package/runtime/lib/snapshot/workletEvent.js +22 -0
- package/runtime/lib/snapshot/workletEvent.js.map +1 -0
- package/runtime/lib/snapshot/workletRef.d.ts +5 -0
- package/runtime/lib/snapshot/workletRef.js +52 -0
- package/runtime/lib/snapshot/workletRef.js.map +1 -0
- package/runtime/lib/snapshot.d.ts +81 -0
- package/runtime/lib/snapshot.js +477 -0
- package/runtime/lib/snapshot.js.map +1 -0
- package/runtime/lib/snapshotPatch.d.ts +14 -0
- package/runtime/lib/snapshotPatch.js +31 -0
- package/runtime/lib/snapshotPatch.js.map +1 -0
- package/runtime/lib/snapshotPatchApply.d.ts +2 -0
- package/runtime/lib/snapshotPatchApply.js +110 -0
- package/runtime/lib/snapshotPatchApply.js.map +1 -0
- package/runtime/lib/utils.d.ts +4 -0
- package/runtime/lib/utils.js +32 -0
- package/runtime/lib/utils.js.map +1 -0
- package/runtime/lib/worklet/ctx.d.ts +1 -0
- package/runtime/lib/worklet/ctx.js +17 -0
- package/runtime/lib/worklet/ctx.js.map +1 -0
- package/runtime/lib/worklet/execMap.d.ts +1 -0
- package/runtime/lib/worklet/execMap.js +42 -0
- package/runtime/lib/worklet/execMap.js.map +1 -0
- package/runtime/lib/worklet/functionality.d.ts +1 -0
- package/runtime/lib/worklet/functionality.js +11 -0
- package/runtime/lib/worklet/functionality.js.map +1 -0
- package/runtime/lib/worklet/hmr.d.ts +1 -0
- package/runtime/lib/worklet/hmr.js +21 -0
- package/runtime/lib/worklet/hmr.js.map +1 -0
- package/runtime/lib/worklet/indexMap.d.ts +8 -0
- package/runtime/lib/worklet/indexMap.js +17 -0
- package/runtime/lib/worklet/indexMap.js.map +1 -0
- package/runtime/lib/worklet/jsImpl.d.ts +11 -0
- package/runtime/lib/worklet/jsImpl.js +68 -0
- package/runtime/lib/worklet/jsImpl.js.map +1 -0
- package/runtime/lib/worklet/runWorklet.d.ts +14 -0
- package/runtime/lib/worklet/runWorklet.js +82 -0
- package/runtime/lib/worklet/runWorklet.js.map +1 -0
- package/runtime/lib/worklet/workletRef.d.ts +131 -0
- package/runtime/lib/worklet/workletRef.js +96 -0
- package/runtime/lib/worklet/workletRef.js.map +1 -0
- package/runtime/lib/worklet/workletRefPool.d.ts +2 -0
- package/runtime/lib/worklet/workletRefPool.js +22 -0
- package/runtime/lib/worklet/workletRefPool.js.map +1 -0
- package/runtime/src/backgroundSnapshot.ts +452 -0
- package/runtime/src/compat/componentIs.ts +30 -0
- package/runtime/src/compat/initData.ts +140 -0
- package/runtime/src/compat/lynxComponent.ts +80 -0
- package/runtime/src/debug/debug.ts +9 -0
- package/runtime/src/debug/printSnapshot.ts +28 -0
- package/runtime/src/debug/profile.ts +54 -0
- package/runtime/src/document.ts +53 -0
- package/runtime/src/gesture/processGesture.ts +77 -0
- package/runtime/src/gesture/processGestureBagkround.ts +15 -0
- package/runtime/src/gesture/types.ts +39 -0
- package/runtime/src/hooks/react.ts +62 -0
- package/runtime/src/hooks/useLynxGlobalEventListener.ts +59 -0
- package/runtime/src/hydrate.ts +364 -0
- package/runtime/src/index.ts +89 -0
- package/runtime/src/internal.ts +60 -0
- package/runtime/src/legacy-react-runtime/index.ts +40 -0
- package/runtime/src/lifecycle/delayUnmount.ts +77 -0
- package/runtime/src/lifecycle/destroy.ts +26 -0
- package/runtime/src/lifecycle/pass.ts +14 -0
- package/runtime/src/lifecycle/patchUpdate.ts +240 -0
- package/runtime/src/lifecycle/reload.ts +79 -0
- package/runtime/src/lifecycle/render.ts +46 -0
- package/runtime/src/lifecycleConstant.ts +16 -0
- package/runtime/src/list.ts +375 -0
- package/runtime/src/lynx/calledByNative.ts +132 -0
- package/runtime/src/lynx/component.ts +116 -0
- package/runtime/src/lynx/dynamic-js.ts +33 -0
- package/runtime/src/lynx/env.ts +110 -0
- package/runtime/src/lynx/injectLepusMethods.ts +53 -0
- package/runtime/src/lynx/lazy-bundle.ts +126 -0
- package/runtime/src/lynx/performance.ts +117 -0
- package/runtime/src/lynx/tt.ts +212 -0
- package/runtime/src/lynx-api.ts +374 -0
- package/runtime/src/lynx.ts +105 -0
- package/runtime/src/opcodes.ts +64 -0
- package/runtime/src/renderToOpcodes/constants.ts +24 -0
- package/runtime/src/renderToOpcodes/index.ts +301 -0
- package/runtime/src/root.ts +19 -0
- package/runtime/src/snapshot/event.ts +33 -0
- package/runtime/src/snapshot/gesture.ts +25 -0
- package/runtime/src/snapshot/platformInfo.ts +48 -0
- package/runtime/src/snapshot/ref.ts +151 -0
- package/runtime/src/snapshot/spread.ts +287 -0
- package/runtime/src/snapshot/workletEvent.ts +33 -0
- package/runtime/src/snapshot/workletRef.ts +63 -0
- package/runtime/src/snapshot.ts +576 -0
- package/runtime/src/snapshotPatch.ts +56 -0
- package/runtime/src/snapshotPatchApply.ts +117 -0
- package/runtime/src/utils.ts +35 -0
- package/runtime/src/worklet/ctx.ts +22 -0
- package/runtime/src/worklet/execMap.ts +50 -0
- package/runtime/src/worklet/functionality.ts +12 -0
- package/runtime/src/worklet/hmr.ts +26 -0
- package/runtime/src/worklet/indexMap.ts +20 -0
- package/runtime/src/worklet/jsImpl.ts +86 -0
- package/runtime/src/worklet/runWorklet.ts +88 -0
- package/runtime/src/worklet/workletRef.ts +229 -0
- package/runtime/src/worklet/workletRefPool.ts +25 -0
- package/transform/cjs/main.cjs +6 -0
- package/transform/cjs/main.d.cts +3 -0
- package/transform/cjs/package.json +3 -0
- package/transform/dist/wasm.cjs +8478 -0
- package/transform/index.d.ts +187 -0
- package/transform/main.js +15 -0
- package/types/react.d.ts +147 -0
- package/types/react.docs.d.ts +74 -0
- package/worklet-runtime/dist/dev.js +414 -0
- package/worklet-runtime/dist/dev.js.map +8 -0
- package/worklet-runtime/dist/main.js +414 -0
- package/worklet-runtime/dist/main.js.map +8 -0
- package/worklet-runtime/lib/api/element.d.ts +14 -0
- package/worklet-runtime/lib/api/element.js +71 -0
- package/worklet-runtime/lib/api/element.js.map +1 -0
- package/worklet-runtime/lib/api/lepusQuerySelector.d.ts +3 -0
- package/worklet-runtime/lib/api/lepusQuerySelector.js +23 -0
- package/worklet-runtime/lib/api/lepusQuerySelector.js.map +1 -0
- package/worklet-runtime/lib/api/lynxApi.d.ts +2 -0
- package/worklet-runtime/lib/api/lynxApi.js +35 -0
- package/worklet-runtime/lib/api/lynxApi.js.map +1 -0
- package/worklet-runtime/lib/bindings/bindings.d.ts +14 -0
- package/worklet-runtime/lib/bindings/bindings.js +61 -0
- package/worklet-runtime/lib/bindings/bindings.js.map +1 -0
- package/worklet-runtime/lib/bindings/events.d.ts +7 -0
- package/worklet-runtime/lib/bindings/events.js +9 -0
- package/worklet-runtime/lib/bindings/events.js.map +1 -0
- package/worklet-runtime/lib/bindings/index.d.ts +4 -0
- package/worklet-runtime/lib/bindings/index.js +7 -0
- package/worklet-runtime/lib/bindings/index.js.map +1 -0
- package/worklet-runtime/lib/bindings/loadRuntime.d.ts +8 -0
- package/worklet-runtime/lib/bindings/loadRuntime.js +25 -0
- package/worklet-runtime/lib/bindings/loadRuntime.js.map +1 -0
- package/worklet-runtime/lib/bindings/types.d.ts +35 -0
- package/worklet-runtime/lib/bindings/types.js +2 -0
- package/worklet-runtime/lib/bindings/types.js.map +1 -0
- package/worklet-runtime/lib/delayWorkletEvent.d.ts +11 -0
- package/worklet-runtime/lib/delayWorkletEvent.js +49 -0
- package/worklet-runtime/lib/delayWorkletEvent.js.map +1 -0
- package/worklet-runtime/lib/global.d.ts +15 -0
- package/worklet-runtime/lib/global.js +2 -0
- package/worklet-runtime/lib/global.js.map +1 -0
- package/worklet-runtime/lib/index.d.ts +1 -0
- package/worklet-runtime/lib/index.js +12 -0
- package/worklet-runtime/lib/index.js.map +1 -0
- package/worklet-runtime/lib/jsFunctionLifecycle.d.ts +18 -0
- package/worklet-runtime/lib/jsFunctionLifecycle.js +55 -0
- package/worklet-runtime/lib/jsFunctionLifecycle.js.map +1 -0
- package/worklet-runtime/lib/listeners.d.ts +2 -0
- package/worklet-runtime/lib/listeners.js +17 -0
- package/worklet-runtime/lib/listeners.js.map +1 -0
- package/worklet-runtime/lib/utils/profile.d.ts +1 -0
- package/worklet-runtime/lib/utils/profile.js +20 -0
- package/worklet-runtime/lib/utils/profile.js.map +1 -0
- package/worklet-runtime/lib/utils/version.d.ts +1 -0
- package/worklet-runtime/lib/utils/version.js +10 -0
- package/worklet-runtime/lib/utils/version.js.map +1 -0
- package/worklet-runtime/lib/workletRef.d.ts +12 -0
- package/worklet-runtime/lib/workletRef.js +50 -0
- package/worklet-runtime/lib/workletRef.js.map +1 -0
- package/worklet-runtime/lib/workletRuntime.d.ts +2 -0
- package/worklet-runtime/lib/workletRuntime.js +128 -0
- package/worklet-runtime/lib/workletRuntime.js.map +1 -0
|
@@ -0,0 +1,452 @@
|
|
|
1
|
+
// Copyright 2024 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
import type { Worklet } from '@lynx-js/react/worklet-runtime/bindings';
|
|
5
|
+
|
|
6
|
+
import { diffArrayAction, diffArrayLepus } from './hydrate.js';
|
|
7
|
+
import { globalBackgroundSnapshotInstancesToRemove } from './lifecycle/patchUpdate.js';
|
|
8
|
+
import { markRefToRemove } from './snapshot/ref.js';
|
|
9
|
+
import { transformSpread } from './snapshot/spread.js';
|
|
10
|
+
import {
|
|
11
|
+
DynamicPartType,
|
|
12
|
+
backgroundSnapshotInstanceManager,
|
|
13
|
+
snapshotManager,
|
|
14
|
+
traverseSnapshotInstance,
|
|
15
|
+
} from './snapshot.js';
|
|
16
|
+
import type { SerializedSnapshotInstance } from './snapshot.js';
|
|
17
|
+
import {
|
|
18
|
+
SnapshotOperation,
|
|
19
|
+
__globalSnapshotPatch,
|
|
20
|
+
initGlobalSnapshotPatch,
|
|
21
|
+
takeGlobalSnapshotPatch,
|
|
22
|
+
} from './snapshotPatch.js';
|
|
23
|
+
import type { SnapshotPatch } from './snapshotPatch.js';
|
|
24
|
+
import { isDirectOrDeepEqual } from './utils.js';
|
|
25
|
+
import { onPostWorkletCtx } from './worklet/ctx.js';
|
|
26
|
+
import { processGestureBackground } from './gesture/processGestureBagkround.js';
|
|
27
|
+
import type { GestureKind } from './gesture/types.js';
|
|
28
|
+
import { globalPipelineOptions } from './lynx/performance.js';
|
|
29
|
+
|
|
30
|
+
export class BackgroundSnapshotInstance {
|
|
31
|
+
constructor(public type: string) {
|
|
32
|
+
let id;
|
|
33
|
+
id = this.__id = backgroundSnapshotInstanceManager.nextId += 1;
|
|
34
|
+
backgroundSnapshotInstanceManager.values.set(id, this);
|
|
35
|
+
|
|
36
|
+
__globalSnapshotPatch?.push(SnapshotOperation.CreateElement, type, id);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
__id: number;
|
|
40
|
+
__values: any[] | undefined;
|
|
41
|
+
|
|
42
|
+
private __parent: BackgroundSnapshotInstance | null = null;
|
|
43
|
+
private __firstChild: BackgroundSnapshotInstance | null = null;
|
|
44
|
+
private __lastChild: BackgroundSnapshotInstance | null = null;
|
|
45
|
+
private __previousSibling: BackgroundSnapshotInstance | null = null;
|
|
46
|
+
private __nextSibling: BackgroundSnapshotInstance | null = null;
|
|
47
|
+
|
|
48
|
+
get parentNode(): BackgroundSnapshotInstance | null {
|
|
49
|
+
return this.__parent;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
get nextSibling(): BackgroundSnapshotInstance | null {
|
|
53
|
+
return this.__nextSibling;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// get isConnected() {
|
|
57
|
+
// return !!this.__parent;
|
|
58
|
+
// }
|
|
59
|
+
|
|
60
|
+
contains(child: BackgroundSnapshotInstance): boolean {
|
|
61
|
+
return child.parentNode === this;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// TODO: write tests for this
|
|
65
|
+
// This will be called in `lazy`/`Suspense`.
|
|
66
|
+
// We currently ignore this since we did not find a way to test.
|
|
67
|
+
/* v8 ignore start */
|
|
68
|
+
appendChild(child: BackgroundSnapshotInstance): void {
|
|
69
|
+
return this.insertBefore(child);
|
|
70
|
+
}
|
|
71
|
+
/* v8 ignore stop */
|
|
72
|
+
|
|
73
|
+
insertBefore(
|
|
74
|
+
node: BackgroundSnapshotInstance,
|
|
75
|
+
beforeNode?: BackgroundSnapshotInstance,
|
|
76
|
+
): void {
|
|
77
|
+
__globalSnapshotPatch?.push(
|
|
78
|
+
SnapshotOperation.InsertBefore,
|
|
79
|
+
this.__id,
|
|
80
|
+
node.__id,
|
|
81
|
+
beforeNode?.__id,
|
|
82
|
+
);
|
|
83
|
+
|
|
84
|
+
// If the node already has a parent, remove it from its current parent
|
|
85
|
+
const p = node.__parent;
|
|
86
|
+
if (p) {
|
|
87
|
+
if (node.__previousSibling) {
|
|
88
|
+
node.__previousSibling.__nextSibling = node.__nextSibling;
|
|
89
|
+
} else {
|
|
90
|
+
p.__firstChild = node.__nextSibling;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
if (node.__nextSibling) {
|
|
94
|
+
node.__nextSibling.__previousSibling = node.__previousSibling;
|
|
95
|
+
} else {
|
|
96
|
+
p.__lastChild = node.__previousSibling;
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
// If beforeNode is not provided, add the new node as the last child
|
|
101
|
+
if (beforeNode) {
|
|
102
|
+
// If beforeNode is provided, insert the new node before beforeNode
|
|
103
|
+
if (beforeNode.__previousSibling) {
|
|
104
|
+
beforeNode.__previousSibling.__nextSibling = node;
|
|
105
|
+
node.__previousSibling = beforeNode.__previousSibling;
|
|
106
|
+
} else {
|
|
107
|
+
this.__firstChild = node;
|
|
108
|
+
node.__previousSibling = null;
|
|
109
|
+
}
|
|
110
|
+
beforeNode.__previousSibling = node;
|
|
111
|
+
node.__nextSibling = beforeNode;
|
|
112
|
+
node.__parent = this;
|
|
113
|
+
} else {
|
|
114
|
+
if (this.__lastChild) {
|
|
115
|
+
this.__lastChild.__nextSibling = node;
|
|
116
|
+
node.__previousSibling = this.__lastChild;
|
|
117
|
+
} else {
|
|
118
|
+
this.__firstChild = node;
|
|
119
|
+
node.__previousSibling = null;
|
|
120
|
+
}
|
|
121
|
+
this.__lastChild = node;
|
|
122
|
+
node.__parent = this;
|
|
123
|
+
node.__nextSibling = null;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
removeChild(node: BackgroundSnapshotInstance): void {
|
|
128
|
+
__globalSnapshotPatch?.push(
|
|
129
|
+
SnapshotOperation.RemoveChild,
|
|
130
|
+
this.__id,
|
|
131
|
+
node.__id,
|
|
132
|
+
);
|
|
133
|
+
|
|
134
|
+
if (node.__parent !== this) {
|
|
135
|
+
throw new Error('The node to be removed is not a child of this node.');
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (node.__previousSibling) {
|
|
139
|
+
node.__previousSibling.__nextSibling = node.__nextSibling;
|
|
140
|
+
} else {
|
|
141
|
+
this.__firstChild = node.__nextSibling;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (node.__nextSibling) {
|
|
145
|
+
node.__nextSibling.__previousSibling = node.__previousSibling;
|
|
146
|
+
} else {
|
|
147
|
+
this.__lastChild = node.__previousSibling;
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
node.__parent = null;
|
|
151
|
+
node.__previousSibling = null;
|
|
152
|
+
node.__nextSibling = null;
|
|
153
|
+
|
|
154
|
+
traverseSnapshotInstance(node, v => {
|
|
155
|
+
v.__parent = null;
|
|
156
|
+
globalBackgroundSnapshotInstancesToRemove.push(v.__id);
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
get childNodes(): BackgroundSnapshotInstance[] {
|
|
161
|
+
const nodes: BackgroundSnapshotInstance[] = [];
|
|
162
|
+
let node = this.__firstChild;
|
|
163
|
+
while (node) {
|
|
164
|
+
nodes.push(node);
|
|
165
|
+
if (node === this.__lastChild) {
|
|
166
|
+
break;
|
|
167
|
+
}
|
|
168
|
+
node = node.__nextSibling;
|
|
169
|
+
}
|
|
170
|
+
return nodes;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
setAttribute(key: string | number, value: any): void {
|
|
174
|
+
if (__PROFILE__) {
|
|
175
|
+
console.profile('setAttribute');
|
|
176
|
+
}
|
|
177
|
+
if (key === 'values') {
|
|
178
|
+
if (__globalSnapshotPatch) {
|
|
179
|
+
const oldValues = this.__values;
|
|
180
|
+
if (oldValues) {
|
|
181
|
+
for (let index = 0; index < value.length; index++) {
|
|
182
|
+
const { needUpdate, valueToCommit } = this.setAttributeImpl(value[index], oldValues[index], index);
|
|
183
|
+
if (needUpdate) {
|
|
184
|
+
__globalSnapshotPatch?.push(
|
|
185
|
+
SnapshotOperation.SetAttribute,
|
|
186
|
+
this.__id,
|
|
187
|
+
index,
|
|
188
|
+
valueToCommit,
|
|
189
|
+
);
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
} else {
|
|
193
|
+
const patch = [];
|
|
194
|
+
const length = value.length;
|
|
195
|
+
for (let index = 0; index < length; ++index) {
|
|
196
|
+
const { valueToCommit } = this.setAttributeImpl(value[index], null, index);
|
|
197
|
+
patch[index] = valueToCommit;
|
|
198
|
+
}
|
|
199
|
+
__globalSnapshotPatch?.push(
|
|
200
|
+
SnapshotOperation.SetAttributes,
|
|
201
|
+
this.__id,
|
|
202
|
+
patch,
|
|
203
|
+
);
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
this.__values = value;
|
|
207
|
+
if (__PROFILE__) {
|
|
208
|
+
console.profileEnd();
|
|
209
|
+
}
|
|
210
|
+
return;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
// old path (`<__snapshot_xxxx_xxxx __0={} __1={} />` or `this.setAttribute(0, xxx)`)
|
|
214
|
+
// is reserved as slow path
|
|
215
|
+
const index = typeof key === 'string' ? Number(key.slice(2)) : key;
|
|
216
|
+
(this.__values ??= [])[index] = value;
|
|
217
|
+
|
|
218
|
+
__globalSnapshotPatch?.push(
|
|
219
|
+
SnapshotOperation.SetAttribute,
|
|
220
|
+
this.__id,
|
|
221
|
+
index,
|
|
222
|
+
value,
|
|
223
|
+
);
|
|
224
|
+
if (__PROFILE__) {
|
|
225
|
+
console.profileEnd();
|
|
226
|
+
}
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
private setAttributeImpl(newValue: any, oldValue: any, index: number): {
|
|
230
|
+
needUpdate: boolean;
|
|
231
|
+
valueToCommit: any;
|
|
232
|
+
} {
|
|
233
|
+
if (!newValue) {
|
|
234
|
+
if (oldValue && oldValue.__ref) {
|
|
235
|
+
markRefToRemove(`${this.__id}:${index}:`, oldValue);
|
|
236
|
+
}
|
|
237
|
+
return { needUpdate: oldValue !== newValue, valueToCommit: newValue };
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const newType = typeof newValue;
|
|
241
|
+
if (newType === 'object') {
|
|
242
|
+
if (newValue.__spread) {
|
|
243
|
+
const oldSpread = oldValue ? oldValue.__spread : oldValue;
|
|
244
|
+
const newSpread = transformSpread(this, index, newValue);
|
|
245
|
+
const needUpdate = !isDirectOrDeepEqual(oldSpread, newSpread);
|
|
246
|
+
// use __spread to cache the transform result for next diff
|
|
247
|
+
newValue.__spread = newSpread;
|
|
248
|
+
if (needUpdate) {
|
|
249
|
+
if (oldSpread && oldSpread.ref) {
|
|
250
|
+
markRefToRemove(`${this.__id}:${index}:ref`, oldValue.ref);
|
|
251
|
+
}
|
|
252
|
+
for (let key in newSpread) {
|
|
253
|
+
const newSpreadValue = newSpread[key];
|
|
254
|
+
if (!newSpreadValue) {
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
if ((newSpreadValue as any)._wkltId) {
|
|
258
|
+
newSpread[key] = onPostWorkletCtx(newSpreadValue as Worklet);
|
|
259
|
+
} else if ((newSpreadValue as any).__isGesture) {
|
|
260
|
+
processGestureBackground(newSpreadValue as GestureKind);
|
|
261
|
+
} else if (key == '__lynx_timing_flag' && oldSpread?.[key] != newSpreadValue) {
|
|
262
|
+
if (globalPipelineOptions) {
|
|
263
|
+
globalPipelineOptions.needTimestamps = true;
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
return { needUpdate, valueToCommit: newSpread };
|
|
269
|
+
}
|
|
270
|
+
if (newValue.__ref) {
|
|
271
|
+
// force update to update ref value
|
|
272
|
+
// TODO: ref: optimize this. The ref update maybe can be done on the background thread to reduce updating.
|
|
273
|
+
// The old ref must have a place to be stored because it needs to be cleared when the main thread returns.
|
|
274
|
+
markRefToRemove(`${this.__id}:${index}:`, oldValue);
|
|
275
|
+
// update ref. On the main thread, the ref id will be replaced with value's sign when updating.
|
|
276
|
+
return { needUpdate: true, valueToCommit: newValue.__ref };
|
|
277
|
+
}
|
|
278
|
+
if (newValue._wkltId) {
|
|
279
|
+
return { needUpdate: true, valueToCommit: onPostWorkletCtx(newValue) };
|
|
280
|
+
}
|
|
281
|
+
if (newValue.__isGesture) {
|
|
282
|
+
processGestureBackground(newValue);
|
|
283
|
+
return { needUpdate: true, valueToCommit: newValue };
|
|
284
|
+
}
|
|
285
|
+
if (newValue.__ltf) {
|
|
286
|
+
// __lynx_timing_flag
|
|
287
|
+
if (globalPipelineOptions && oldValue?.__ltf != newValue.__ltf) {
|
|
288
|
+
globalPipelineOptions.needTimestamps = true;
|
|
289
|
+
return { needUpdate: true, valueToCommit: newValue };
|
|
290
|
+
}
|
|
291
|
+
return { needUpdate: false, valueToCommit: newValue };
|
|
292
|
+
}
|
|
293
|
+
return { needUpdate: !isDirectOrDeepEqual(oldValue, newValue), valueToCommit: newValue };
|
|
294
|
+
}
|
|
295
|
+
if (newType === 'function') {
|
|
296
|
+
if (newValue.__ref) {
|
|
297
|
+
markRefToRemove(`${this.__id}:${index}:`, oldValue);
|
|
298
|
+
return { needUpdate: true, valueToCommit: newValue.__ref };
|
|
299
|
+
}
|
|
300
|
+
/* event */
|
|
301
|
+
return { needUpdate: !oldValue, valueToCommit: 1 };
|
|
302
|
+
}
|
|
303
|
+
return { needUpdate: oldValue !== newValue, valueToCommit: newValue };
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export function hydrate(
|
|
308
|
+
before: SerializedSnapshotInstance,
|
|
309
|
+
after: BackgroundSnapshotInstance,
|
|
310
|
+
): SnapshotPatch {
|
|
311
|
+
initGlobalSnapshotPatch();
|
|
312
|
+
|
|
313
|
+
const helper2 = (afters: BackgroundSnapshotInstance[], parentId: number) => {
|
|
314
|
+
for (const child of afters) {
|
|
315
|
+
const id = child.__id;
|
|
316
|
+
__globalSnapshotPatch!.push(SnapshotOperation.CreateElement, child.type, id);
|
|
317
|
+
const values = child.__values;
|
|
318
|
+
if (values) {
|
|
319
|
+
child.__values = undefined;
|
|
320
|
+
child.setAttribute('values', values);
|
|
321
|
+
}
|
|
322
|
+
helper2(child.childNodes, id);
|
|
323
|
+
__globalSnapshotPatch!.push(SnapshotOperation.InsertBefore, parentId, id, undefined);
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
|
|
327
|
+
const helper = (
|
|
328
|
+
before: SerializedSnapshotInstance,
|
|
329
|
+
after: BackgroundSnapshotInstance,
|
|
330
|
+
) => {
|
|
331
|
+
backgroundSnapshotInstanceManager.updateId(after.__id, before.id);
|
|
332
|
+
after.__values?.forEach((value, index) => {
|
|
333
|
+
const old = before.values![index];
|
|
334
|
+
|
|
335
|
+
if (value) {
|
|
336
|
+
if (value.__spread) {
|
|
337
|
+
// `value.__spread` my contain event ids using snapshot ids before hydration. Remove it.
|
|
338
|
+
delete value.__spread;
|
|
339
|
+
value = transformSpread(after, index, value);
|
|
340
|
+
for (let key in value) {
|
|
341
|
+
if (value[key] && value[key]._wkltId) {
|
|
342
|
+
onPostWorkletCtx(value[key]);
|
|
343
|
+
} else if (value[key] && value[key].__isGesture) {
|
|
344
|
+
processGestureBackground(value[key] as GestureKind);
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
after.__values![index]!.__spread = value;
|
|
348
|
+
} else if (value.__ref) {
|
|
349
|
+
if (old) {
|
|
350
|
+
// skip patch
|
|
351
|
+
value = old;
|
|
352
|
+
} else {
|
|
353
|
+
value = value.__ref;
|
|
354
|
+
}
|
|
355
|
+
} else if (typeof value === 'function') {
|
|
356
|
+
value = `${after.__id}:${index}:`;
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (value && value._wkltId) {
|
|
361
|
+
onPostWorkletCtx(value);
|
|
362
|
+
} else if (value && value.__isGesture) {
|
|
363
|
+
processGestureBackground(value);
|
|
364
|
+
}
|
|
365
|
+
if (!isDirectOrDeepEqual(value, old)) {
|
|
366
|
+
__globalSnapshotPatch!.push(
|
|
367
|
+
SnapshotOperation.SetAttribute,
|
|
368
|
+
after.__id,
|
|
369
|
+
index,
|
|
370
|
+
value,
|
|
371
|
+
);
|
|
372
|
+
}
|
|
373
|
+
});
|
|
374
|
+
|
|
375
|
+
const { slot } = snapshotManager.values.get(after.type)!;
|
|
376
|
+
|
|
377
|
+
const beforeChildNodes = before.children || [];
|
|
378
|
+
const afterChildNodes = after.childNodes;
|
|
379
|
+
|
|
380
|
+
if (!slot) {
|
|
381
|
+
return;
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
slot.forEach(([type], index) => {
|
|
385
|
+
switch (type) {
|
|
386
|
+
case DynamicPartType.Slot:
|
|
387
|
+
case DynamicPartType.MultiChildren: {
|
|
388
|
+
// TODO: the following null assertions are not 100% safe
|
|
389
|
+
const v1 = beforeChildNodes[index]!;
|
|
390
|
+
const v2 = afterChildNodes[index]!;
|
|
391
|
+
helper(v1, v2);
|
|
392
|
+
break;
|
|
393
|
+
}
|
|
394
|
+
case DynamicPartType.Children:
|
|
395
|
+
case DynamicPartType.ListChildren: {
|
|
396
|
+
const diffResult = diffArrayLepus(
|
|
397
|
+
beforeChildNodes,
|
|
398
|
+
afterChildNodes,
|
|
399
|
+
(a, b) => a.type === b.type,
|
|
400
|
+
(a, b) => {
|
|
401
|
+
helper(a, b);
|
|
402
|
+
},
|
|
403
|
+
);
|
|
404
|
+
diffArrayAction(
|
|
405
|
+
beforeChildNodes,
|
|
406
|
+
diffResult,
|
|
407
|
+
(node, target) => {
|
|
408
|
+
__globalSnapshotPatch!.push(
|
|
409
|
+
SnapshotOperation.CreateElement,
|
|
410
|
+
node.type,
|
|
411
|
+
node.__id,
|
|
412
|
+
);
|
|
413
|
+
helper2(node.childNodes, node.__id);
|
|
414
|
+
const values = node.__values;
|
|
415
|
+
if (values) {
|
|
416
|
+
node.__values = undefined;
|
|
417
|
+
node.setAttribute('values', values);
|
|
418
|
+
}
|
|
419
|
+
__globalSnapshotPatch!.push(
|
|
420
|
+
SnapshotOperation.InsertBefore,
|
|
421
|
+
before.id,
|
|
422
|
+
node.__id,
|
|
423
|
+
target?.id,
|
|
424
|
+
);
|
|
425
|
+
return undefined as unknown as SerializedSnapshotInstance;
|
|
426
|
+
},
|
|
427
|
+
node => {
|
|
428
|
+
__globalSnapshotPatch!.push(
|
|
429
|
+
SnapshotOperation.RemoveChild,
|
|
430
|
+
before.id,
|
|
431
|
+
node.id,
|
|
432
|
+
);
|
|
433
|
+
},
|
|
434
|
+
(node, target) => {
|
|
435
|
+
// changedList.push([SnapshotOperation.RemoveChild, before.id, node.id]);
|
|
436
|
+
__globalSnapshotPatch!.push(
|
|
437
|
+
SnapshotOperation.InsertBefore,
|
|
438
|
+
before.id,
|
|
439
|
+
node.id,
|
|
440
|
+
target?.id,
|
|
441
|
+
);
|
|
442
|
+
},
|
|
443
|
+
);
|
|
444
|
+
break;
|
|
445
|
+
}
|
|
446
|
+
}
|
|
447
|
+
});
|
|
448
|
+
};
|
|
449
|
+
|
|
450
|
+
helper(before, after);
|
|
451
|
+
return takeGlobalSnapshotPatch()!;
|
|
452
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
// Copyright 2024 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 type { FC } from 'react';
|
|
6
|
+
|
|
7
|
+
// for better reuse if runtime is changed
|
|
8
|
+
export function factory(
|
|
9
|
+
{ createElement, useMemo, Suspense, lazy }: typeof import('react'),
|
|
10
|
+
loadLazyBundle: any,
|
|
11
|
+
): FC<{ is: string }> {
|
|
12
|
+
/**
|
|
13
|
+
* @internal a polyfill for <component is=? />
|
|
14
|
+
*/
|
|
15
|
+
const __ComponentIsPolyfill: FC<{ is: string }> = ({ is, ...props }) => {
|
|
16
|
+
if (typeof is !== 'string') {
|
|
17
|
+
lynx.reportError(
|
|
18
|
+
new Error(
|
|
19
|
+
'You must provide a string to props `is` when using syntax `<component is=? />`.',
|
|
20
|
+
),
|
|
21
|
+
);
|
|
22
|
+
return null;
|
|
23
|
+
}
|
|
24
|
+
// @ts-ignore
|
|
25
|
+
const D = useMemo(() => lazy(() => loadLazyBundle(is)), [is]);
|
|
26
|
+
return createElement(Suspense, { key: is }, createElement(D, props));
|
|
27
|
+
};
|
|
28
|
+
|
|
29
|
+
return __ComponentIsPolyfill;
|
|
30
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// Copyright 2024 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
import type { ComponentClass, Consumer, Context, FC, PropsWithChildren, ReactNode } from 'react';
|
|
5
|
+
import { useLynxGlobalEventListener } from '../hooks/useLynxGlobalEventListener.js';
|
|
6
|
+
|
|
7
|
+
type Getter<T> = {
|
|
8
|
+
[key in keyof T]: () => T[key];
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
// for better reuse if runtime is changed
|
|
12
|
+
export function factory<Data>(
|
|
13
|
+
{ createContext, useState, createElement, useLynxGlobalEventListener: useListener }: typeof import('react') & {
|
|
14
|
+
useLynxGlobalEventListener: typeof useLynxGlobalEventListener;
|
|
15
|
+
},
|
|
16
|
+
prop: '__globalProps' | '__initData',
|
|
17
|
+
eventName: string,
|
|
18
|
+
): Getter<{
|
|
19
|
+
Context: Context<Data>;
|
|
20
|
+
Provider: FC<{
|
|
21
|
+
children?: ReactNode | undefined;
|
|
22
|
+
}>;
|
|
23
|
+
Consumer: Consumer<Data>;
|
|
24
|
+
use: () => Data;
|
|
25
|
+
useChanged: (callback: (data: Data) => void) => void;
|
|
26
|
+
}> {
|
|
27
|
+
const Context = createContext({} as Data);
|
|
28
|
+
|
|
29
|
+
const Provider: FC<PropsWithChildren> = ({ children }) => {
|
|
30
|
+
const [__, set] = useState<Data>(lynx[prop] as Data);
|
|
31
|
+
|
|
32
|
+
const handleChange = () => {
|
|
33
|
+
set(lynx[prop] as Data);
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
useChanged(handleChange);
|
|
37
|
+
|
|
38
|
+
return createElement(
|
|
39
|
+
Context.Provider,
|
|
40
|
+
{
|
|
41
|
+
value: __,
|
|
42
|
+
},
|
|
43
|
+
children,
|
|
44
|
+
);
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
const Consumer: Consumer<Data> = Context.Consumer;
|
|
48
|
+
|
|
49
|
+
const use = (): Data => {
|
|
50
|
+
const [__, set] = useState(lynx[prop]);
|
|
51
|
+
useChanged(() => {
|
|
52
|
+
set(lynx[prop]);
|
|
53
|
+
});
|
|
54
|
+
|
|
55
|
+
return __ as Data;
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
const useChanged = (callback: (__: Data) => void) => {
|
|
59
|
+
if (!__LEPUS__) {
|
|
60
|
+
// @ts-ignore
|
|
61
|
+
useListener<(__: unknown) => void>(eventName, callback);
|
|
62
|
+
}
|
|
63
|
+
};
|
|
64
|
+
|
|
65
|
+
return {
|
|
66
|
+
Context: () => Context,
|
|
67
|
+
Provider: () => Provider,
|
|
68
|
+
Consumer: () => Consumer,
|
|
69
|
+
use: () => use,
|
|
70
|
+
useChanged: () => useChanged,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Higher-Order Component (HOC) that injects `initData` into the state of the given class component.
|
|
76
|
+
*
|
|
77
|
+
* This HOC checks if the provided component is a class component. If it is, it wraps the component
|
|
78
|
+
* and injects the `initData` into its state. It also adds a listener
|
|
79
|
+
* to update the state when data changes, and removes the listener when the component unmounts.
|
|
80
|
+
*
|
|
81
|
+
* @typeParam P - The type of the props of the wrapped component.
|
|
82
|
+
* @typeParam S - The type of the state of the wrapped component.
|
|
83
|
+
*
|
|
84
|
+
* @param App - The class component to be wrapped by the HOC.
|
|
85
|
+
*
|
|
86
|
+
* @returns The original component if it is not a class component, otherwise a new class component
|
|
87
|
+
* with `initData` injection and state update functionality.
|
|
88
|
+
*
|
|
89
|
+
* @example
|
|
90
|
+
* ```typescript
|
|
91
|
+
* class App extends React.Component<MyProps, MyState> {
|
|
92
|
+
* // component implementation
|
|
93
|
+
* }
|
|
94
|
+
*
|
|
95
|
+
* export default withInitDataInState(App);
|
|
96
|
+
* ```
|
|
97
|
+
* @public
|
|
98
|
+
*/
|
|
99
|
+
export function withInitDataInState<P, S>(App: ComponentClass<P, S>): ComponentClass<P, S> {
|
|
100
|
+
const isClassComponent = 'prototype' in App && App.prototype.render;
|
|
101
|
+
if (!isClassComponent) {
|
|
102
|
+
// return as-is when not class component
|
|
103
|
+
return App;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
class C extends App {
|
|
107
|
+
h?: Function;
|
|
108
|
+
|
|
109
|
+
constructor(props: any) {
|
|
110
|
+
super(props);
|
|
111
|
+
this.state = {
|
|
112
|
+
...this.state,
|
|
113
|
+
...lynx.__initData,
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
if (!__LEPUS__) {
|
|
117
|
+
// @ts-ignore
|
|
118
|
+
lynx.getJSModule('GlobalEventEmitter').addListener(
|
|
119
|
+
'onDataChanged',
|
|
120
|
+
this.h = () => {
|
|
121
|
+
this.setState(lynx.__initData);
|
|
122
|
+
},
|
|
123
|
+
);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
override componentWillUnmount(): void {
|
|
128
|
+
super.componentWillUnmount?.();
|
|
129
|
+
if (!__LEPUS__) {
|
|
130
|
+
// @ts-ignore
|
|
131
|
+
lynx.getJSModule('GlobalEventEmitter').removeListener(
|
|
132
|
+
'onDataChanged',
|
|
133
|
+
this.h,
|
|
134
|
+
);
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
return C;
|
|
140
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
// Copyright 2024 The Lynx Authors. All rights reserved.
|
|
2
|
+
// Licensed under the Apache License Version 2.0 that can be found in the
|
|
3
|
+
// LICENSE file in the root directory of this source tree.
|
|
4
|
+
import { Component } from 'preact';
|
|
5
|
+
import type { ReactNode } from 'react';
|
|
6
|
+
|
|
7
|
+
export function wrapWithLynxComponent(
|
|
8
|
+
jsxSnapshot: (c: ReactNode, spread?: Record<string, any>) => ReactNode,
|
|
9
|
+
jsxComponent: any,
|
|
10
|
+
): ReactNode {
|
|
11
|
+
const C = jsxComponent.type;
|
|
12
|
+
if (typeof C === 'function') {
|
|
13
|
+
if (C === ComponentFromReactRuntime || C.prototype instanceof ComponentFromReactRuntime) {
|
|
14
|
+
if (jsxSnapshot.length === 1) {
|
|
15
|
+
return jsxSnapshot(jsxComponent);
|
|
16
|
+
} else {
|
|
17
|
+
// spread
|
|
18
|
+
if (!jsxComponent.props.removeComponentElement) {
|
|
19
|
+
return jsxSnapshot(jsxComponent, takeComponentAttributes(jsxComponent));
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
return jsxComponent;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
// @ts-expect-error
|
|
28
|
+
export class ComponentFromReactRuntime extends Component {
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const __COMPONENT_ATTRIBUTES__ = /* @__PURE__ */ new Set([
|
|
32
|
+
'name',
|
|
33
|
+
'style',
|
|
34
|
+
'class',
|
|
35
|
+
'flatten',
|
|
36
|
+
'clip-radius',
|
|
37
|
+
'overlap',
|
|
38
|
+
'user-interaction-enabled',
|
|
39
|
+
'native-interaction-enabled',
|
|
40
|
+
'block-native-event',
|
|
41
|
+
'enableLayoutOnly',
|
|
42
|
+
'cssAlignWithLegacyW3C',
|
|
43
|
+
'intersection-observers',
|
|
44
|
+
'trigger-global-event',
|
|
45
|
+
'exposure-scene',
|
|
46
|
+
'exposure-id',
|
|
47
|
+
'exposure-screen-margin-top',
|
|
48
|
+
'exposure-screen-margin-bottom',
|
|
49
|
+
'exposure-screen-margin-left',
|
|
50
|
+
'exposure-screen-margin-right',
|
|
51
|
+
'focusable',
|
|
52
|
+
'focus-index',
|
|
53
|
+
'accessibility-label',
|
|
54
|
+
'accessibility-element',
|
|
55
|
+
'accessibility-traits',
|
|
56
|
+
'enable-new-animator',
|
|
57
|
+
]);
|
|
58
|
+
|
|
59
|
+
function takeComponentAttributes(jsxComponent: any): Record<string, any> {
|
|
60
|
+
const attributes: Record<string, any> = {};
|
|
61
|
+
Object.keys(jsxComponent.props).forEach((k) => {
|
|
62
|
+
// let re1 = Regex::new(r"^(global-bind|bind|catch|capture-bind|capture-catch)([A-Za-z]+)$").unwrap();
|
|
63
|
+
// let re2 = Regex::new(r"^data-([A-Za-z]+)$").unwrap();
|
|
64
|
+
if (
|
|
65
|
+
__COMPONENT_ATTRIBUTES__.has(k)
|
|
66
|
+
|| k === 'id'
|
|
67
|
+
|| k === 'className'
|
|
68
|
+
|| k === 'dataSet'
|
|
69
|
+
|| k === 'data-set'
|
|
70
|
+
|| k === 'removeComponentElement'
|
|
71
|
+
|| k.match(/^(global-bind|bind|catch|capture-bind|capture-catch)([A-Za-z]+)$/)
|
|
72
|
+
|| k.match(/^data-([A-Za-z]+)$/)
|
|
73
|
+
) {
|
|
74
|
+
attributes[k] = jsxComponent.props[k];
|
|
75
|
+
delete jsxComponent.props[k];
|
|
76
|
+
}
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
return attributes;
|
|
80
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright 2024 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
|
+
const logDebug: (...args: any[]) => void = console.debug;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* @internal
|
|
8
|
+
*/
|
|
9
|
+
export { logDebug };
|