@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,117 @@
|
|
|
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 { createSnapshot, SnapshotInstance, snapshotInstanceManager, snapshotManager } from './snapshot.js';
|
|
5
|
+
import type { SnapshotPatch } from './snapshotPatch.js';
|
|
6
|
+
import { SnapshotOperation } from './snapshotPatch.js';
|
|
7
|
+
|
|
8
|
+
function reportCtxNotFound(): void {
|
|
9
|
+
lynx.reportError(new Error(`snapshotPatchApply failed: ctx not found`));
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function snapshotPatchApply(snapshotPatch: SnapshotPatch): void {
|
|
13
|
+
const length = snapshotPatch.length;
|
|
14
|
+
for (let i = 0; i < length; ++i) {
|
|
15
|
+
switch (snapshotPatch[i]) {
|
|
16
|
+
case SnapshotOperation.CreateElement: {
|
|
17
|
+
const type = snapshotPatch[++i];
|
|
18
|
+
const id = snapshotPatch[++i];
|
|
19
|
+
new SnapshotInstance(type, id);
|
|
20
|
+
break;
|
|
21
|
+
}
|
|
22
|
+
case SnapshotOperation.InsertBefore: {
|
|
23
|
+
const parentId = snapshotPatch[++i];
|
|
24
|
+
const childId = snapshotPatch[++i];
|
|
25
|
+
const beforeId = snapshotPatch[++i];
|
|
26
|
+
const parent = snapshotInstanceManager.values.get(parentId);
|
|
27
|
+
const child = snapshotInstanceManager.values.get(childId);
|
|
28
|
+
const existingNode = snapshotInstanceManager.values.get(beforeId!);
|
|
29
|
+
if (!parent || !child) {
|
|
30
|
+
reportCtxNotFound();
|
|
31
|
+
} else {
|
|
32
|
+
parent.insertBefore(child, existingNode);
|
|
33
|
+
}
|
|
34
|
+
break;
|
|
35
|
+
}
|
|
36
|
+
case SnapshotOperation.RemoveChild: {
|
|
37
|
+
const parentId = snapshotPatch[++i];
|
|
38
|
+
const childId = snapshotPatch[++i];
|
|
39
|
+
const parent = snapshotInstanceManager.values.get(parentId);
|
|
40
|
+
const child = snapshotInstanceManager.values.get(childId);
|
|
41
|
+
if (!parent || !child) {
|
|
42
|
+
reportCtxNotFound();
|
|
43
|
+
} else {
|
|
44
|
+
parent.removeChild(child);
|
|
45
|
+
}
|
|
46
|
+
break;
|
|
47
|
+
}
|
|
48
|
+
case SnapshotOperation.SetAttribute: {
|
|
49
|
+
const id = snapshotPatch[++i];
|
|
50
|
+
const dynamicPartIndex = snapshotPatch[++i];
|
|
51
|
+
const value = snapshotPatch[++i];
|
|
52
|
+
const si = snapshotInstanceManager.values.get(id);
|
|
53
|
+
if (si) {
|
|
54
|
+
si.setAttribute(dynamicPartIndex, value);
|
|
55
|
+
} else {
|
|
56
|
+
reportCtxNotFound();
|
|
57
|
+
}
|
|
58
|
+
break;
|
|
59
|
+
}
|
|
60
|
+
case SnapshotOperation.SetAttributes: {
|
|
61
|
+
const id = snapshotPatch[++i];
|
|
62
|
+
const values = snapshotPatch[++i];
|
|
63
|
+
const si = snapshotInstanceManager.values.get(id);
|
|
64
|
+
if (si) {
|
|
65
|
+
si.setAttribute('values', values);
|
|
66
|
+
} else {
|
|
67
|
+
reportCtxNotFound();
|
|
68
|
+
}
|
|
69
|
+
break;
|
|
70
|
+
}
|
|
71
|
+
case SnapshotOperation.DEV_ONLY_AddSnapshot: {
|
|
72
|
+
if (__DEV__) {
|
|
73
|
+
const uniqID: string = snapshotPatch[++i];
|
|
74
|
+
const create: string = snapshotPatch[++i];
|
|
75
|
+
const update: string[] = snapshotPatch[++i];
|
|
76
|
+
const slot = snapshotPatch[++i];
|
|
77
|
+
const cssId: number = snapshotPatch[++i] ?? 0;
|
|
78
|
+
const entryName: string | undefined = snapshotPatch[++i];
|
|
79
|
+
|
|
80
|
+
if (!snapshotManager.values.has(uniqID)) {
|
|
81
|
+
// HMR-related
|
|
82
|
+
// Update the evaluated snapshots from JS.
|
|
83
|
+
createSnapshot(
|
|
84
|
+
uniqID,
|
|
85
|
+
evaluate<(ctx: SnapshotInstance) => FiberElement[]>(create),
|
|
86
|
+
update.map<(ctx: SnapshotInstance, index: number, oldValue: any) => void>(evaluate),
|
|
87
|
+
slot,
|
|
88
|
+
cssId,
|
|
89
|
+
entryName,
|
|
90
|
+
);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
case SnapshotOperation.DEV_ONLY_RegisterWorklet: {
|
|
96
|
+
// HMR-related
|
|
97
|
+
if (__DEV__) {
|
|
98
|
+
const hash: string = snapshotPatch[++i];
|
|
99
|
+
const fnStr: string = snapshotPatch[++i];
|
|
100
|
+
const fn = evaluate<(ctx: SnapshotInstance) => FiberElement[]>(fnStr);
|
|
101
|
+
registerWorklet('main-thread', hash, fn);
|
|
102
|
+
}
|
|
103
|
+
break;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Given an expression string, return the evaluated result with ReactLynx runtime injected.
|
|
111
|
+
*
|
|
112
|
+
* @param code - The code to be evaluated
|
|
113
|
+
* @returns the evaluated expression
|
|
114
|
+
*/
|
|
115
|
+
function evaluate<T>(code: string): T {
|
|
116
|
+
return new Function(`return ${code}`)();
|
|
117
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
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
|
+
export function isDirectOrDeepEqual(a: any, b: any): boolean {
|
|
5
|
+
if (a === b) {
|
|
6
|
+
return true;
|
|
7
|
+
}
|
|
8
|
+
if (
|
|
9
|
+
typeof a == 'object' && a !== null && typeof b == 'object' && b !== null && JSON.stringify(a) === JSON.stringify(b)
|
|
10
|
+
) {
|
|
11
|
+
return true;
|
|
12
|
+
}
|
|
13
|
+
return false;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isEmptyObject(obj?: object): obj is Record<string, never> {
|
|
17
|
+
for (var _ in obj) return false;
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
export function isSdkVersionGt(major: number, minor: number): boolean {
|
|
22
|
+
const lynxSdkVersion: string = SystemInfo.lynxSdkVersion || '1.0';
|
|
23
|
+
const version = lynxSdkVersion.split('.');
|
|
24
|
+
return Number(version[0]) > major || (Number(version[0]) == major && Number(version[1]) > minor);
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export function pick<T extends object, K extends keyof T>(obj: T, keys: Iterable<K>): Pick<T, K> {
|
|
28
|
+
const result: any = {};
|
|
29
|
+
for (const key of keys) {
|
|
30
|
+
if (key in obj) {
|
|
31
|
+
result[key] = obj[key];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return result;
|
|
35
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
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 { lynxWorkletJsImpl } from './jsImpl.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
export function onPostWorkletCtx(afterValue: Worklet | null): Worklet | null {
|
|
12
|
+
const impl = lynxWorkletJsImpl();
|
|
13
|
+
if (!impl && afterValue) {
|
|
14
|
+
lynx.reportError(new Error('Main thread script requires Lynx sdk version 2.14'));
|
|
15
|
+
return null;
|
|
16
|
+
}
|
|
17
|
+
if (!afterValue) {
|
|
18
|
+
return afterValue;
|
|
19
|
+
}
|
|
20
|
+
impl?._workletExecIdMap?.add(afterValue);
|
|
21
|
+
return afterValue;
|
|
22
|
+
}
|
|
@@ -0,0 +1,50 @@
|
|
|
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 { JsFnHandle, Worklet } from '@lynx-js/react/worklet-runtime/bindings';
|
|
5
|
+
|
|
6
|
+
import { IndexMap } from './indexMap.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* `WorkletExecIdMap` ensures the worklet object is not released in js context when the main thread is still holding the
|
|
10
|
+
* worklet object (or any of its jsFnHandles). The worklet object holds the js functions which might be called by `runOnBackground()`.
|
|
11
|
+
* If the worklet object is incorrectly released earlier, the `runOnBackground()` would result in failure.
|
|
12
|
+
*
|
|
13
|
+
* Every time a worklet object is sent to the main thread, an `execId` is distributed and will be sent to element
|
|
14
|
+
* context with the worklet object. This relationship is recorded in this class. When all the references to the
|
|
15
|
+
* worklet object are released in the main thread, a message will be sent back to remove the record here.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export class WorkletExecIdMap extends IndexMap<Worklet> {
|
|
20
|
+
public override add(worklet: Worklet): number {
|
|
21
|
+
const execId = super.add(worklet);
|
|
22
|
+
worklet._execId = execId;
|
|
23
|
+
return execId;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public findJsFnHandle(execId: number, fnId: number): JsFnHandle | undefined {
|
|
27
|
+
const worklet = this.get(execId);
|
|
28
|
+
if (!worklet) {
|
|
29
|
+
return undefined;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const f = (obj: any): JsFnHandle | undefined => {
|
|
33
|
+
if (obj === null || typeof obj !== 'object') {
|
|
34
|
+
return undefined;
|
|
35
|
+
}
|
|
36
|
+
if ('_jsFnId' in obj && obj._jsFnId === fnId) {
|
|
37
|
+
return obj as JsFnHandle;
|
|
38
|
+
}
|
|
39
|
+
for (const i in obj) {
|
|
40
|
+
const result = f(obj[i]);
|
|
41
|
+
if (result) {
|
|
42
|
+
return result;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return f(worklet);
|
|
49
|
+
}
|
|
50
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
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 { isSdkVersionGt } from '../utils.js';
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* @internal
|
|
9
|
+
*/
|
|
10
|
+
export function enableRunOnBackground(): boolean {
|
|
11
|
+
return isSdkVersionGt(2, 15);
|
|
12
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
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 { SnapshotOperation, __globalSnapshotPatch } from '../snapshotPatch.js';
|
|
5
|
+
|
|
6
|
+
const workletHashSet: Set<string> = /* @__PURE__ */ new Set();
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @internal
|
|
10
|
+
*/
|
|
11
|
+
function registerWorkletOnBackground(_type: string, hash: string, fn: Function) {
|
|
12
|
+
if (workletHashSet.has(hash)) {
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
workletHashSet.add(hash);
|
|
16
|
+
if (__globalSnapshotPatch) {
|
|
17
|
+
__globalSnapshotPatch.push(
|
|
18
|
+
SnapshotOperation.DEV_ONLY_RegisterWorklet,
|
|
19
|
+
hash,
|
|
20
|
+
// We use `Function.prototype.toString` to serialize the function for Lepus.
|
|
21
|
+
fn.toString(),
|
|
22
|
+
);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export { registerWorkletOnBackground };
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
class IndexMap<T> {
|
|
2
|
+
protected lastIndex = 0;
|
|
3
|
+
protected indexMap: Map<number, T> = new Map();
|
|
4
|
+
|
|
5
|
+
public add(value: T): number {
|
|
6
|
+
const index = ++this.lastIndex;
|
|
7
|
+
this.indexMap.set(index, value);
|
|
8
|
+
return index;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
public get(index: number): T | undefined {
|
|
12
|
+
return this.indexMap.get(index);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
public remove(index: number): void {
|
|
16
|
+
this.indexMap.delete(index);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export { IndexMap };
|
|
@@ -0,0 +1,86 @@
|
|
|
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 { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
|
|
5
|
+
|
|
6
|
+
import { WorkletExecIdMap } from './execMap.js';
|
|
7
|
+
import { enableRunOnBackground } from './functionality.js';
|
|
8
|
+
|
|
9
|
+
interface LynxWorkletJsImpl {
|
|
10
|
+
_workletExecIdMap?: WorkletExecIdMap;
|
|
11
|
+
_workletJsFnLastId: number;
|
|
12
|
+
_workletRefLastId: number;
|
|
13
|
+
_workletRefInitValueSet: Set<number>;
|
|
14
|
+
_workletRefInitValuePatch: [number, unknown][];
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* @internal
|
|
19
|
+
*/
|
|
20
|
+
export function runJSFunction(event: RuntimeProxy.Event): void {
|
|
21
|
+
const impl = lynxWorkletJsImpl();
|
|
22
|
+
if (!impl || !impl._workletExecIdMap) {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
const data = JSON.parse(event.data);
|
|
26
|
+
const obj = impl._workletExecIdMap.findJsFnHandle(data.obj._execId, data.obj._jsFnId);
|
|
27
|
+
const f = obj?._fn;
|
|
28
|
+
if (!f) {
|
|
29
|
+
throw new Error('runOnBackground: JS function not found: ' + JSON.stringify(data.obj));
|
|
30
|
+
}
|
|
31
|
+
f(...data.params);
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* @internal
|
|
36
|
+
*/
|
|
37
|
+
export function removeJsWorklets(event: RuntimeProxy.Event): void {
|
|
38
|
+
const impl = lynxWorkletJsImpl();
|
|
39
|
+
if (!impl?._workletExecIdMap) {
|
|
40
|
+
return;
|
|
41
|
+
}
|
|
42
|
+
for (const id of event.data) {
|
|
43
|
+
impl._workletExecIdMap.remove(id);
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function initWorklet(): boolean {
|
|
48
|
+
if (lynx.getCoreContext === undefined) {
|
|
49
|
+
return false;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
lynx.lynxWorkletJsImpl = {
|
|
53
|
+
_workletJsFnLastId: 0,
|
|
54
|
+
_workletRefLastId: 0,
|
|
55
|
+
_workletRefInitValueSet: new Set<number>(),
|
|
56
|
+
_workletRefInitValuePatch: [],
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
if (enableRunOnBackground()) {
|
|
60
|
+
lynx.lynxWorkletJsImpl._workletExecIdMap = new WorkletExecIdMap();
|
|
61
|
+
lynx.getCoreContext().addEventListener(WorkletEvents.runOnBackground, runJSFunction);
|
|
62
|
+
lynx.getCoreContext().addEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
return true;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
export function destroyWorklet(): void {
|
|
69
|
+
if (!lynx.lynxWorkletJsImpl) {
|
|
70
|
+
return;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
lynx.lynxWorkletJsImpl = undefined;
|
|
74
|
+
|
|
75
|
+
if (enableRunOnBackground()) {
|
|
76
|
+
lynx.getCoreContext?.().removeEventListener(WorkletEvents.runOnBackground, runJSFunction);
|
|
77
|
+
lynx.getCoreContext?.().removeEventListener(WorkletEvents.releaseBackgroundWorkletCtx, removeJsWorklets);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export function lynxWorkletJsImpl(shouldInit: boolean = true): LynxWorkletJsImpl | undefined {
|
|
82
|
+
if (lynx.lynxWorkletJsImpl || (shouldInit && initWorklet())) {
|
|
83
|
+
return lynx.lynxWorkletJsImpl;
|
|
84
|
+
}
|
|
85
|
+
return undefined;
|
|
86
|
+
}
|
|
@@ -0,0 +1,88 @@
|
|
|
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 { JsFnHandle, Worklet } from '@lynx-js/react/worklet-runtime/bindings';
|
|
5
|
+
import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
|
|
6
|
+
|
|
7
|
+
import { onPostWorkletCtx } from './ctx.js';
|
|
8
|
+
import { enableRunOnBackground } from './functionality.js';
|
|
9
|
+
import { lynxWorkletJsImpl } from './jsImpl.js';
|
|
10
|
+
/**
|
|
11
|
+
* transform args of `runOnJS()`.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export function transformToWorklet(obj: Function): JsFnHandle {
|
|
16
|
+
const impl = lynxWorkletJsImpl();
|
|
17
|
+
const id = impl ? ++impl._workletJsFnLastId : 0;
|
|
18
|
+
if (typeof obj !== 'function') {
|
|
19
|
+
// We save the error message in the object, so that we can throw it later when the function is called on the main thread.
|
|
20
|
+
return {
|
|
21
|
+
_jsFnId: id,
|
|
22
|
+
_error: `Argument of runOnBackground should be a function, but got [${typeof obj}] instead`,
|
|
23
|
+
};
|
|
24
|
+
}
|
|
25
|
+
return {
|
|
26
|
+
_jsFnId: id,
|
|
27
|
+
_fn: obj,
|
|
28
|
+
};
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* `runOnMainThread` allows triggering main thread functions on the main thread asynchronously.
|
|
33
|
+
* @param fn - The main thread functions to be called.
|
|
34
|
+
* @returns A function. Calling which with the arguments to be passed to the main thread function to trigger it on the main thread.
|
|
35
|
+
* @public
|
|
36
|
+
*/
|
|
37
|
+
export function runOnMainThread<Fn extends (...args: any[]) => any>(fn: Fn): (...args: Parameters<Fn>) => void {
|
|
38
|
+
if (__LEPUS__) {
|
|
39
|
+
throw new Error('runOnMainThread can only be used on the background thread.');
|
|
40
|
+
}
|
|
41
|
+
const impl = lynxWorkletJsImpl();
|
|
42
|
+
if (!impl) {
|
|
43
|
+
throw new Error('runOnMainThread requires Lynx sdk version 2.14.');
|
|
44
|
+
}
|
|
45
|
+
return (...params: any[]): void => {
|
|
46
|
+
onPostWorkletCtx(fn as any as Worklet);
|
|
47
|
+
lynx.getCoreContext!().dispatchEvent({
|
|
48
|
+
type: WorkletEvents.runWorkletCtx,
|
|
49
|
+
data: JSON.stringify({
|
|
50
|
+
worklet: fn,
|
|
51
|
+
params,
|
|
52
|
+
}),
|
|
53
|
+
});
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* `runOnBackground` allows triggering js functions on the js context asynchronously.
|
|
59
|
+
* @param f - The js function to be called.
|
|
60
|
+
* @returns A function. Calling which with the arguments to be passed to the js function to trigger it on the js context.
|
|
61
|
+
* @public
|
|
62
|
+
*/
|
|
63
|
+
export function runOnBackground<Fn extends (...args: any[]) => any>(f: Fn): (...args: Parameters<Fn>) => void {
|
|
64
|
+
if (!enableRunOnBackground()) {
|
|
65
|
+
throw new Error('runOnBackground requires Lynx sdk version 2.16.');
|
|
66
|
+
}
|
|
67
|
+
if (__JS__) {
|
|
68
|
+
throw new Error('runOnBackground can not be used on the main thread.');
|
|
69
|
+
}
|
|
70
|
+
const obj = f as any as JsFnHandle;
|
|
71
|
+
if (obj._error) {
|
|
72
|
+
throw new Error(obj._error);
|
|
73
|
+
}
|
|
74
|
+
return (...params: any[]): void => {
|
|
75
|
+
if (lynx.getJSContext) {
|
|
76
|
+
lynx.getJSContext().dispatchEvent({
|
|
77
|
+
type: WorkletEvents.runOnBackground,
|
|
78
|
+
data: JSON.stringify({
|
|
79
|
+
obj: {
|
|
80
|
+
_jsFnId: obj._jsFnId,
|
|
81
|
+
_execId: obj._execId!,
|
|
82
|
+
},
|
|
83
|
+
params,
|
|
84
|
+
}),
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
};
|
|
88
|
+
}
|
|
@@ -0,0 +1,229 @@
|
|
|
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 { RefObject } from 'react';
|
|
5
|
+
|
|
6
|
+
import type { WorkletRefImpl } from '@lynx-js/react/worklet-runtime/bindings';
|
|
7
|
+
import { WorkletEvents } from '@lynx-js/react/worklet-runtime/bindings';
|
|
8
|
+
|
|
9
|
+
import { lynxWorkletJsImpl } from './jsImpl.js';
|
|
10
|
+
import { addWorkletRefInitValue } from './workletRefPool.js';
|
|
11
|
+
import { useMemo } from '../hooks/react.js';
|
|
12
|
+
|
|
13
|
+
abstract class WorkletRef<T> {
|
|
14
|
+
/**
|
|
15
|
+
* @internal
|
|
16
|
+
*/
|
|
17
|
+
protected _id: number;
|
|
18
|
+
/**
|
|
19
|
+
* @internal
|
|
20
|
+
*/
|
|
21
|
+
protected _initValue: T | undefined;
|
|
22
|
+
/**
|
|
23
|
+
* @internal
|
|
24
|
+
*/
|
|
25
|
+
protected _type: string;
|
|
26
|
+
/**
|
|
27
|
+
* @internal
|
|
28
|
+
*/
|
|
29
|
+
protected _lifecycleObserver: unknown;
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
protected constructor(initValue: T, type: string) {
|
|
35
|
+
if (__JS__) {
|
|
36
|
+
const impl = lynxWorkletJsImpl();
|
|
37
|
+
this._id = impl ? ++impl._workletRefLastId : 0;
|
|
38
|
+
this._initValue = initValue;
|
|
39
|
+
this._type = type;
|
|
40
|
+
addWorkletRefInitValue(this._id, initValue);
|
|
41
|
+
} else {
|
|
42
|
+
// Out of the js thread, the `WorkletRef` class here is just a placeholder and should not be accessed directly.
|
|
43
|
+
// The real WorkletRef will be generated by the worklet runtime.
|
|
44
|
+
this._id = -1;
|
|
45
|
+
this._type = '__LEPUS__';
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
get current(): T {
|
|
50
|
+
if (__JS__ && __DEV__) {
|
|
51
|
+
throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed in the background thread.');
|
|
52
|
+
}
|
|
53
|
+
if (__LEPUS__ && __DEV__) {
|
|
54
|
+
/* v8 ignore next */
|
|
55
|
+
throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed outside of main thread script.');
|
|
56
|
+
}
|
|
57
|
+
/* v8 ignore next */
|
|
58
|
+
return undefined as T;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
set current(_: T) {
|
|
62
|
+
if (__JS__ && __DEV__) {
|
|
63
|
+
throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed in the background thread.');
|
|
64
|
+
}
|
|
65
|
+
if (__LEPUS__ && __DEV__) {
|
|
66
|
+
throw new Error('MainThreadRef: value of a MainThreadRef cannot be accessed outside of main thread script.');
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* @internal
|
|
72
|
+
*/
|
|
73
|
+
toJSON(): { _wvid: WorkletRefImpl<T>['_wvid'] } {
|
|
74
|
+
return {
|
|
75
|
+
_wvid: this._id,
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
|
|
82
|
+
* states between main thread function calls.
|
|
83
|
+
* The data saved in `current` property of the `MainThreadRef` can be read and written in
|
|
84
|
+
* multiple main thread functions.
|
|
85
|
+
* @public
|
|
86
|
+
*/
|
|
87
|
+
export class MainThreadRef<T> extends WorkletRef<T> {
|
|
88
|
+
constructor(initValue: T) {
|
|
89
|
+
super(initValue, 'main-thread');
|
|
90
|
+
if (__JS__) {
|
|
91
|
+
const id = this._id;
|
|
92
|
+
this._lifecycleObserver = lynx.getNativeApp().createJSObjectDestructionObserver(() => {
|
|
93
|
+
lynx.getCoreContext?.().dispatchEvent({
|
|
94
|
+
type: WorkletEvents.releaseWorkletRef,
|
|
95
|
+
data: {
|
|
96
|
+
id,
|
|
97
|
+
},
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* Create A `MainThreadRef`.
|
|
106
|
+
*
|
|
107
|
+
* A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
|
|
108
|
+
* states between main thread function calls.
|
|
109
|
+
* The data saved in `current` property of the `MainThreadRef` can be read and written in
|
|
110
|
+
* multiple main thread functions.
|
|
111
|
+
*
|
|
112
|
+
* It is a hook and it should only be called at the top level of your component.
|
|
113
|
+
*
|
|
114
|
+
* @param initValue - The init value of the `MainThreadRef`.
|
|
115
|
+
*
|
|
116
|
+
* @example
|
|
117
|
+
*
|
|
118
|
+
* ```ts
|
|
119
|
+
* import { useMainThreadRef } from '@lynx-js/react'
|
|
120
|
+
* import type { MainThread } from '@lynx-js/types'
|
|
121
|
+
*
|
|
122
|
+
* export function App() {
|
|
123
|
+
* const ref = useMainThreadRef<MainThread.Element>(null)
|
|
124
|
+
*
|
|
125
|
+
* const handleTap = () => {
|
|
126
|
+
* 'main thread'
|
|
127
|
+
* ref.current?.setStyleProperty('background-color', 'blue')
|
|
128
|
+
* }
|
|
129
|
+
*
|
|
130
|
+
* return (
|
|
131
|
+
* <view
|
|
132
|
+
* main-thread:ref={ref}
|
|
133
|
+
* main-thread:bindtap={handleTap}
|
|
134
|
+
* style={{ width: '300px', height: '300px' }}
|
|
135
|
+
* />
|
|
136
|
+
* )
|
|
137
|
+
* }
|
|
138
|
+
* ```
|
|
139
|
+
*
|
|
140
|
+
* @public
|
|
141
|
+
*/
|
|
142
|
+
export function useMainThreadRef<T>(initValue: T): MainThreadRef<T>;
|
|
143
|
+
|
|
144
|
+
// convenience overload for refs given as a ref prop as they typically start with a null value
|
|
145
|
+
/**
|
|
146
|
+
* Create A `MainThreadRef`.
|
|
147
|
+
*
|
|
148
|
+
* A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
|
|
149
|
+
* states between main thread function calls.
|
|
150
|
+
* The data saved in `current` property of the `MainThreadRef` can be read and written in
|
|
151
|
+
* multiple main thread functions.
|
|
152
|
+
*
|
|
153
|
+
* It is a hook and it should only be called at the top level of your component.
|
|
154
|
+
*
|
|
155
|
+
* @param initValue - The init value of the `MainThreadRef`.
|
|
156
|
+
*
|
|
157
|
+
* @example
|
|
158
|
+
*
|
|
159
|
+
* ```ts
|
|
160
|
+
* import { useMainThreadRef } from '@lynx-js/react'
|
|
161
|
+
* import type { MainThread } from '@lynx-js/types'
|
|
162
|
+
*
|
|
163
|
+
* export function App() {
|
|
164
|
+
* const ref = useMainThreadRef<MainThread.Element>(null)
|
|
165
|
+
*
|
|
166
|
+
* const handleTap = () => {
|
|
167
|
+
* 'main thread'
|
|
168
|
+
* ref.current?.setStyleProperty('background-color', 'blue')
|
|
169
|
+
* }
|
|
170
|
+
*
|
|
171
|
+
* return (
|
|
172
|
+
* <view
|
|
173
|
+
* main-thread:ref={ref}
|
|
174
|
+
* main-thread:bindtap={handleTap}
|
|
175
|
+
* style={{ width: '300px', height: '300px' }}
|
|
176
|
+
* />
|
|
177
|
+
* )
|
|
178
|
+
* }
|
|
179
|
+
* ```
|
|
180
|
+
*
|
|
181
|
+
* @public
|
|
182
|
+
*/
|
|
183
|
+
export function useMainThreadRef<T>(initValue: T | null): RefObject<T>;
|
|
184
|
+
|
|
185
|
+
// convenience overload for potentially undefined initialValue / call with 0 arguments
|
|
186
|
+
// has a default to stop it from defaulting to {} instead
|
|
187
|
+
/**
|
|
188
|
+
* Create A `MainThreadRef`.
|
|
189
|
+
*
|
|
190
|
+
* A `MainThreadRef` is a ref that can only be accessed on the main thread. It is used to preserve
|
|
191
|
+
* states between main thread function calls.
|
|
192
|
+
* The data saved in `current` property of the `MainThreadRef` can be read and written in
|
|
193
|
+
* multiple main thread functions.
|
|
194
|
+
*
|
|
195
|
+
* It is a hook and it should only be called at the top level of your component.
|
|
196
|
+
*
|
|
197
|
+
* @example
|
|
198
|
+
*
|
|
199
|
+
* ```ts
|
|
200
|
+
* import { useMainThreadRef } from '@lynx-js/react'
|
|
201
|
+
* import type { MainThread } from '@lynx-js/types'
|
|
202
|
+
*
|
|
203
|
+
* export function App() {
|
|
204
|
+
* const ref = useMainThreadRef<MainThread.Element>(null)
|
|
205
|
+
*
|
|
206
|
+
* const handleTap = () => {
|
|
207
|
+
* 'main thread'
|
|
208
|
+
* ref.current?.setStyleProperty('background-color', 'blue')
|
|
209
|
+
* }
|
|
210
|
+
*
|
|
211
|
+
* return (
|
|
212
|
+
* <view
|
|
213
|
+
* main-thread:ref={ref}
|
|
214
|
+
* main-thread:bindtap={handleTap}
|
|
215
|
+
* style={{ width: '300px', height: '300px' }}
|
|
216
|
+
* />
|
|
217
|
+
* )
|
|
218
|
+
* }
|
|
219
|
+
* ```
|
|
220
|
+
*
|
|
221
|
+
* @public
|
|
222
|
+
*/
|
|
223
|
+
export function useMainThreadRef<T = undefined>(): MainThreadRef<T | undefined>;
|
|
224
|
+
|
|
225
|
+
export function useMainThreadRef<T>(initValue?: T): MainThreadRef<T | undefined> {
|
|
226
|
+
return useMemo(() => {
|
|
227
|
+
return new MainThreadRef(initValue);
|
|
228
|
+
}, []);
|
|
229
|
+
}
|