Skip to content

Commit 4cda3a8

Browse files
committed
[Feature][PerformanceAPI] add LazyBundleEntry and ReloadBundleEntry demo
remove useless tti demo
1 parent f84fb4b commit 4cda3a8

File tree

8 files changed

+167
-50
lines changed

8 files changed

+167
-50
lines changed

examples/performance-api/lynx.config.mjs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,19 @@ import { pluginTypeCheck } from "@rsbuild/plugin-type-check";
1111
export default defineConfig({
1212
source: {
1313
entry: {
14-
fcp_entry: "./src/fcp_entry/index.tsx",
15-
tti_entry: "./src/tti_entry/index.tsx",
16-
actual_fmp_entry: "./src/actual_fmp_entry/index.tsx",
14+
metric_fcp_entry: "./src/fcp_entry/index.tsx",
15+
metric_actual_fmp_entry: "./src/actual_fmp_entry/index.tsx",
1716
pipeline_entry: "./src/pipeline_entry/index.tsx",
1817
load_bundle_entry: "./src/load_bundle_entry/index.tsx",
19-
InitLynxviewEntry: "./src/init_lynxview_entry/index.tsx",
20-
InitBackgroundRuntimeEntry: "./src/init_background_runtime_entry/index.tsx",
21-
InitContainerEntry: "./src/init_container_entry/index.tsx",
22-
CreateCustomPerformanceMetric: "./src/create_custom_performance_metric/index.tsx",
23-
PerformanceObserverObserve: "./src/performance_observer_observe/index.tsx",
24-
PerformanceObserverDisconnect: "./src/performance_observer_disconnect/index.tsx",
25-
SimpleObseve: "./src/simple_observe/index.tsx",
18+
init_lynxview_entry: "./src/init_lynxview_entry/index.tsx",
19+
init_background_runtime_entry: "./src/init_background_runtime_entry/index.tsx",
20+
init_container_entry: "./src/init_container_entry/index.tsx",
21+
create_custom_performance_metric: "./src/create_custom_performance_metric/index.tsx",
22+
performance_observer_observe: "./src/performance_observer_observe/index.tsx",
23+
performance_observer_disconnect: "./src/performance_observer_disconnect/index.tsx",
24+
simple_observe: "./src/simple_observe/index.tsx",
25+
reload_bundle_entry: "./src/reload_bundle_entry/index.tsx",
26+
lazy_bundle_entry: "./src/lazy_bundle_entry/index.tsx",
2627
},
2728
},
2829
plugins: [
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
import { useEffect, useMemo, useRef } from "@lynx-js/react";
2+
import type { PerformanceCallback, PerformanceObserver } from "@lynx-js/types";
3+
4+
export function useLynxPerformanceObserver(eventTypes: string[], callback: PerformanceCallback): void {
5+
"background only";
6+
7+
const previousArgsRef = useRef<[string[], PerformanceObserver]>();
8+
9+
useMemo(() => {
10+
if (previousArgsRef.current) {
11+
const [_, observer] = previousArgsRef.current;
12+
observer.disconnect();
13+
}
14+
const newObserver = lynx.performance.createObserver(callback);
15+
newObserver.observe(eventTypes);
16+
previousArgsRef.current = [eventTypes, newObserver];
17+
}, [eventTypes, callback]);
18+
19+
useEffect(() => {
20+
return () => {
21+
if (previousArgsRef.current) {
22+
const [_, observer] = previousArgsRef.current;
23+
observer.disconnect();
24+
}
25+
};
26+
}, []);
27+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import "./index.scss";
2+
3+
export default function MyLazyComponent() {
4+
return (
5+
<view style={{ flexDirection: "column" }} className="container">
6+
<text>Hello, This is a RL3 Lazy Component!</text>
7+
</view>
8+
);
9+
}

examples/performance-api/src/tti_entry/index.scss renamed to examples/performance-api/src/lazy_bundle_entry/index.scss

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,10 @@
77
}
88

99
.title {
10-
font-size: 58rpx;
11-
font-weight: 500;
12-
padding: 10rpx;
13-
color: #0095ff;
14-
color: linear-gradient(120deg, #0095ff 30%, #42d392);
10+
text-shadow: 0 2rpx 4rpx rgba(0, 149, 255, 0.3);
11+
font-weight: 700;
12+
border-bottom: 4rpx solid #0095ff;
13+
padding-bottom: 10rpx;
1514
}
1615

1716
.content {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import { lazy, root, Suspense, useCallback, useState } from "@lynx-js/react";
2+
import type { PerformanceEntry } from "@lynx-js/types";
3+
import { useLynxPerformanceObserver } from "../common/utils.jsx";
4+
import "./index.scss";
5+
6+
const MyLazyComponent = lazy(() => import("./MyLazyComponent.jsx"));
7+
8+
export default function LazyBundleEntryExample(this: any) {
9+
const [lazyBundleEntry, setLazyBundleEntry] = useState<string>("");
10+
11+
// 1. Create a performance observer callback.
12+
let callback = useCallback((entry: PerformanceEntry) => {
13+
console.log(entry.entryType, entry.name);
14+
if (entry.entryType == "resource" && entry.name == "lazyBundle") {
15+
// 3. Received "resource.lazyBundle" event.
16+
setLazyBundleEntry(JSON.stringify(entry, null, 4));
17+
}
18+
}, []);
19+
// 2. Register to listen to the "resource" event.
20+
useLynxPerformanceObserver(["pipeline", "resource"], callback);
21+
22+
return (
23+
<view className="container">
24+
<text className="title">Hello LazyBundleEntry~</text>
25+
26+
<view style={{ flexDirection: "column" }}>
27+
<text className="entry">{lazyBundleEntry}</text>
28+
</view>
29+
30+
<view style={{ flexDirection: "column" }}>
31+
<text>Hello, This is main page!</text>
32+
<Suspense fallback={<text>Loading...</text>}>
33+
<MyLazyComponent />
34+
</Suspense>
35+
</view>
36+
</view>
37+
);
38+
}
39+
40+
root.render(<LazyBundleEntryExample />);
41+
42+
if (import.meta.webpackHot) {
43+
import.meta.webpackHot.accept();
44+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
.container {
2+
display: flex;
3+
padding-top: 150rpx;
4+
height: 100%;
5+
flex-direction: column;
6+
align-items: center;
7+
}
8+
9+
.title {
10+
text-shadow: 0 2rpx 4rpx rgba(0, 149, 255, 0.3);
11+
font-weight: 700;
12+
border-bottom: 4rpx solid #0095ff;
13+
padding-bottom: 10rpx;
14+
}
15+
16+
.content {
17+
margin: 10px;
18+
}
19+
20+
.entry {
21+
margin: 15px;
22+
background-color: gainsboro;
23+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import { root, useCallback, useEffect, useState } from "@lynx-js/react";
2+
import type { PerformanceEntry } from "@lynx-js/types";
3+
import { useLynxPerformanceObserver } from "../common/utils.jsx";
4+
import "./index.scss";
5+
6+
let has_reload = false;
7+
export default function ReloadBundleEntryExample(this: any) {
8+
const [reloadBundleEntry, setReloadBundleEntry] = useState<string>("111");
9+
10+
// 1. Create a performance observer callback.
11+
let callback = useCallback((entry: PerformanceEntry) => {
12+
let entryId = entry.entryType + entry.name;
13+
console.log(entryId);
14+
if (
15+
entry.entryType == "pipeline"
16+
&& (entry.name == "reloadBundleFromNative" || entry.name == "reloadBundleFromBts")
17+
) {
18+
// 3. Received "pipeline.reloadBundleFromBts" event.
19+
setReloadBundleEntry(JSON.stringify(entry, null, 4));
20+
}
21+
}, []);
22+
// 2. Register to listen to the "pipeline" event.
23+
useLynxPerformanceObserver(["pipeline"], callback);
24+
25+
useEffect(() => {
26+
if (!has_reload) {
27+
setTimeout(() => {
28+
has_reload = true;
29+
// 3. reload from bts
30+
lynx.reload({ "test": "123" }, () => {});
31+
}, 2000);
32+
} else {
33+
has_reload = true;
34+
}
35+
}, []);
36+
37+
return (
38+
<view className="container">
39+
<text className="title">Hello ReloadBundleEntry~</text>
40+
<text className="entry">{reloadBundleEntry}</text>
41+
</view>
42+
);
43+
}
44+
45+
root.render(<ReloadBundleEntryExample />);
46+
47+
if (import.meta.webpackHot) {
48+
import.meta.webpackHot.accept();
49+
}

examples/performance-api/src/tti_entry/index.tsx

Lines changed: 0 additions & 35 deletions
This file was deleted.

0 commit comments

Comments
 (0)