-
Notifications
You must be signed in to change notification settings - Fork 74
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merge qiannan 8 #1553
base: master
Are you sure you want to change the base?
Merge qiannan 8 #1553
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,6 @@ function App() { | |
// ref 用于保存Treemap实例 | ||
const treeMapRef = useRef(null); | ||
const [chartData, setChartData] = useState(''); | ||
|
||
// toolTip展示使用 | ||
const [tooltipContent, setToolTipContent] = useState(''); | ||
const createModulesTree = (modules) => { | ||
|
@@ -106,9 +105,15 @@ function App() { | |
</div> | ||
); | ||
}; | ||
const getFoamTreeData = (chartData: any) => { | ||
const formatData = format(chartData?.chunkModules || []); | ||
const resData = filterModulesForSize(formatData, 'statSize'); | ||
return { groups: resData }; | ||
}; | ||
const createFoamTree = (chartData: any) => { | ||
const formatData = format(chartData?.chunkModules || []); | ||
const resData = filterModulesForSize(formatData, 'statSize'); | ||
debugger; | ||
return new FoamTree({ | ||
element: chartRef.current, | ||
layout: 'squarified', | ||
|
@@ -161,6 +166,16 @@ function App() { | |
useEffect(() => { | ||
window.addEventListener('load', () => { | ||
setChartData(window?.chartData); | ||
// 如果开启了热更新,那么启动 websocket 服务。 | ||
if (window?.hmrWatch) { | ||
const socket = new WebSocket('ws://localhost:3000/__/sendStatsData'); | ||
|
||
socket.addEventListener('message', (rawMessage) => { | ||
const msg = JSON.parse(rawMessage.data); | ||
console.log('msg==', msg); | ||
setChartData(msg); | ||
}); | ||
} | ||
Comment on lines
+169
to
+178
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 增强 WebSocket 逻辑的健壮性 此段代码添加了 WebSocket 逻辑,用于实时更新图表数据。建议添加错误处理和连接管理逻辑,以提高代码的健壮性。 建议在 WebSocket 连接和消息处理中添加异常处理逻辑,例如: socket.addEventListener('message', (rawMessage) => {
try {
const msg = JSON.parse(rawMessage.data);
console.log('msg==', msg);
setChartData(msg);
} catch (error) {
console.error('Failed to parse message data:', error);
}
}); |
||
}); | ||
window.addEventListener('resize', resize); | ||
return () => { | ||
|
@@ -172,7 +187,22 @@ function App() { | |
console.warn('数据未初始化!!'); | ||
return; | ||
} | ||
// 如果已经实例化并且 chartData 发生改变,那么就重新设置值。 | ||
if (treeMapRef.current) { | ||
debugger; | ||
treeMapRef.current.set({ | ||
dataObject: getFoamTreeData(chartData), | ||
}); | ||
treeMapRef.current.update(); | ||
return; | ||
} | ||
treeMapRef.current = createFoamTree(chartData); | ||
return () => { | ||
if (treeMapRef.current) { | ||
treeMapRef.current.dispose(); | ||
treeMapRef.current = null; | ||
} | ||
}; | ||
}, [chartData]); | ||
return ( | ||
<> | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -226,7 +226,9 @@ | |
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub struct AnalyzeConfig {} | ||
pub struct AnalyzeConfig { | ||
pub watch: Option<bool>, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个配置是不是可以去掉,跟随 mako 启动是否开启 watch There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个具体怎么整啊 |
||
} | ||
|
||
#[derive(Deserialize, Serialize, Clone, Debug)] | ||
pub enum CodeSplittingStrategy { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,6 @@ | |
let root = self.root.clone(); | ||
let compiler = self.compiler.clone(); | ||
let txws_watch = txws.clone(); | ||
|
||
if self.compiler.context.config.dev_server.is_some() { | ||
std::thread::spawn(move || { | ||
if let Err(e) = Self::watch_for_changes(root, compiler, txws_watch) { | ||
|
@@ -50,7 +49,7 @@ | |
} else if let Err(e) = Self::watch_for_changes(root, compiler, txws_watch) { | ||
eprintln!("Error watching files: {:?}", e); | ||
} | ||
|
||
let compiler = self.compiler.clone(); | ||
// server | ||
if self.compiler.context.config.dev_server.is_some() { | ||
let config_port = self | ||
|
@@ -67,14 +66,19 @@ | |
let txws = txws.clone(); | ||
let make_svc = make_service_fn(move |_conn| { | ||
let context = context.clone(); | ||
let compiler = compiler.clone(); | ||
let txws = txws.clone(); | ||
async move { | ||
Ok::<_, hyper::Error>(service_fn(move |req| { | ||
let context = context.clone(); | ||
let txws = txws.clone(); | ||
let compile = compiler.clone(); | ||
|
||
let staticfile = | ||
hyper_staticfile::Static::new(context.config.output.path.clone()); | ||
async move { Self::handle_requests(req, context, staticfile, txws).await } | ||
async move { | ||
Self::handle_requests(req, context, staticfile, txws, compile).await | ||
} | ||
})) | ||
} | ||
}); | ||
|
@@ -120,6 +124,7 @@ | |
context: Arc<Context>, | ||
staticfile: hyper_staticfile::Static, | ||
txws: broadcast::Sender<WsMessage>, | ||
compiler: Arc<Compiler>, | ||
) -> Result<hyper::Response<Body>> { | ||
debug!("> {} {}", req.method().to_string(), req.uri().path()); | ||
|
||
|
@@ -158,6 +163,31 @@ | |
Ok(not_found_response()) | ||
} | ||
} | ||
"/__/sendStatsData" => { | ||
if hyper_tungstenite::is_upgrade_request(&req) { | ||
debug!("new websocket connection"); | ||
let (response, websocket) = hyper_tungstenite::upgrade(req, None).unwrap(); | ||
let txws = txws.clone(); | ||
tokio_runtime::spawn(async move { | ||
let receiver = txws.subscribe(); | ||
if txws.receiver_count() > 0 { | ||
let stats = compiler.create_stats_info(); | ||
let json_data = serde_json::to_string_pretty(&stats).unwrap(); | ||
txws.send(WsMessage { | ||
hash: 0, | ||
stats_data: json_data, | ||
}) | ||
.unwrap(); | ||
} | ||
Self::handle_websocket_stats_data(websocket, receiver) | ||
.await | ||
.unwrap(); | ||
}); | ||
Ok(response) | ||
} else { | ||
Comment on lines
+166
to
+187
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 审查新的 WebSocket 端点和消息处理逻辑 此代码段添加了一个新的 WebSocket 端点 建议在 WebSocket 消息处理中添加更严格的错误处理和安全检查,例如验证消息的完整性和格式。此外,考虑对敏感数据进行加密处理,以保护在 WebSocket 通信中传输的数据。 |
||
Ok(not_found_response()) | ||
} | ||
} | ||
_ => { | ||
// for bundle outputs | ||
|
||
|
@@ -237,6 +267,32 @@ | |
} | ||
} | ||
|
||
// 发送热更新之后的数据 | ||
async fn handle_websocket_stats_data( | ||
websocket: hyper_tungstenite::HyperWebsocket, | ||
mut receiver: broadcast::Receiver<WsMessage>, | ||
) -> Result<()> { | ||
let websocket = websocket.await?; | ||
let (mut sender, mut ws_recv) = websocket.split(); | ||
let task = tokio_runtime::spawn(async move { | ||
loop { | ||
if let Ok(msg) = receiver.recv().await { | ||
if sender.send(Message::text(msg.stats_data)).await.is_err() { | ||
break; | ||
} | ||
} | ||
} | ||
}); | ||
while let Some(message) = ws_recv.next().await { | ||
if let Ok(Message::Close(_)) = message { | ||
break; | ||
} | ||
} | ||
debug!("websocket connection disconnected"); | ||
task.abort(); | ||
Ok(()) | ||
} | ||
Comment on lines
+270
to
+294
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 增强 WebSocket 统计数据处理功能的健壮性 此函数 建议在此函数中添加对 WebSocket 连接和消息发送的异常处理逻辑,以防止潜在的资源泄露和运行时错误。例如: if let Ok(msg) = receiver.recv().await {
if sender.send(Message::text(msg.stats_data)).await.is_err() {
break;
}
} else {
// 处理接收错误
debug!("Failed to receive message");
} ToolsGitHub Check: codecov/patch
|
||
|
||
// TODO: refact socket message data structure | ||
async fn handle_websocket( | ||
websocket: hyper_tungstenite::HyperWebsocket, | ||
|
@@ -407,8 +463,16 @@ | |
|
||
let receiver_count = txws.receiver_count(); | ||
debug!("receiver count: {}", receiver_count); | ||
// 解析更新完之后的数据 | ||
let stats = compiler.create_stats_info(); | ||
let json_data = serde_json::to_string_pretty(&stats).unwrap(); | ||
let ws_new_data = WsMessage { | ||
hash: **hmr_hash, | ||
stats_data: json_data, | ||
}; | ||
|
||
if receiver_count > 0 { | ||
txws.send(WsMessage { hash: **hmr_hash }).unwrap(); | ||
txws.send(ws_new_data).unwrap(); | ||
debug!("send message to clients"); | ||
} | ||
|
||
|
@@ -419,4 +483,5 @@ | |
#[derive(Clone, Debug)] | ||
struct WsMessage { | ||
hash: u64, | ||
stats_data: String, | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -193,7 +193,12 @@ | |||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
if self.context.config.analyze.is_some() { | ||||||||||||||||||||||||||
Analyze::write_analyze(&stats, &self.context.config.output.path)?; | ||||||||||||||||||||||||||
let analyze = &self.context.config.analyze.clone().unwrap(); | ||||||||||||||||||||||||||
let mut is_watch = false; | ||||||||||||||||||||||||||
if analyze.watch.is_some() && analyze.watch.unwrap() { | ||||||||||||||||||||||||||
is_watch = true; | ||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
Analyze::write_analyze(&stats, &self.context.config.output.path, is_watch)?; | ||||||||||||||||||||||||||
Comment on lines
+196
to
+201
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 审查 代码正确地添加了 - if analyze.watch.is_some() && analyze.watch.unwrap() {
+ if analyze.watch.unwrap_or(false) { Committable suggestion
Suggested change
ToolsGitHub Check: codecov/patch
|
||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
debug!("generate done in {}ms", t_generate.elapsed().as_millis()); | ||||||||||||||||||||||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
审查新添加的数据处理函数
新增的
getFoamTreeData
函数用于处理图表数据。需要确保此函数的逻辑正确无误,并且性能合理。建议对此函数进行单元测试,以验证其正确性和性能。此外,考虑使用类型注解来增强代码的可读性和可维护性。