improve speed when reloading the site
This commit is contained in:
1 parent
edc31dfc03
commit
8e33b8e027
2 files changed
+132
-79
No files matched your search
+66
-25
@@ -2,12 +2,13 @@ use axum::{
|
|||||||
extract::{ws::Message, Extension, TypedHeader, WebSocketUpgrade},
|
extract::{ws::Message, Extension, TypedHeader, WebSocketUpgrade},
|
||||||
response::IntoResponse,
|
response::IntoResponse,
|
||||||
};
|
};
|
||||||
|
use dioxus_rsx_interpreter::SetRsxMessage;
|
||||||
|
|
||||||
use std::{path::PathBuf, sync::Arc};
|
use std::{path::PathBuf, sync::Arc};
|
||||||
|
|
||||||
pub use crate::hot_reload::{find_rsx, DiffResult};
|
pub use crate::hot_reload::{find_rsx, DiffResult};
|
||||||
use crate::CrateConfig;
|
use crate::CrateConfig;
|
||||||
pub use dioxus_rsx_interpreter::{error::Error, CodeLocation, SetRsxMessage};
|
pub use dioxus_rsx_interpreter::{error::Error, CodeLocation, SetManyRsxMessage};
|
||||||
pub use proc_macro2::TokenStream;
|
pub use proc_macro2::TokenStream;
|
||||||
pub use std::collections::HashMap;
|
pub use std::collections::HashMap;
|
||||||
pub use std::sync::Mutex;
|
pub use std::sync::Mutex;
|
||||||
@@ -19,7 +20,7 @@ use syn::spanned::Spanned;
|
|||||||
use tokio::sync::broadcast;
|
use tokio::sync::broadcast;
|
||||||
|
|
||||||
pub struct HotReloadState {
|
pub struct HotReloadState {
|
||||||
pub messages: broadcast::Sender<SetRsxMessage>,
|
pub messages: broadcast::Sender<SetManyRsxMessage>,
|
||||||
pub update: broadcast::Sender<String>,
|
pub update: broadcast::Sender<String>,
|
||||||
pub last_file_rebuild: Arc<Mutex<FileMap>>,
|
pub last_file_rebuild: Arc<Mutex<FileMap>>,
|
||||||
pub watcher_config: CrateConfig,
|
pub watcher_config: CrateConfig,
|
||||||
@@ -32,32 +33,76 @@ pub struct FileMap {
|
|||||||
|
|
||||||
impl FileMap {
|
impl FileMap {
|
||||||
pub fn new(path: PathBuf) -> Self {
|
pub fn new(path: PathBuf) -> Self {
|
||||||
|
log::info!("Searching files for changes since last compile...");
|
||||||
fn find_rs_files(root: PathBuf) -> io::Result<HashMap<PathBuf, String>> {
|
fn find_rs_files(root: PathBuf) -> io::Result<HashMap<PathBuf, String>> {
|
||||||
let mut files = HashMap::new();
|
let mut files = HashMap::new();
|
||||||
if root.is_dir() {
|
if root.is_dir() {
|
||||||
let mut handles = Vec::new();
|
|
||||||
for entry in fs::read_dir(root)? {
|
for entry in fs::read_dir(root)? {
|
||||||
if let Ok(entry) = entry {
|
if let Ok(entry) = entry {
|
||||||
let path = entry.path();
|
let path = entry.path();
|
||||||
handles.push(std::thread::spawn(move || find_rs_files(path)));
|
files.extend(find_rs_files(path)?);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
for handle in handles {
|
|
||||||
files.extend(handle.join().unwrap()?);
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
if root.extension().map(|s| s.to_str()).flatten() == Some("rs") {
|
if root.extension().map(|s| s.to_str()).flatten() == Some("rs") {
|
||||||
let mut file = File::open(root.clone()).unwrap();
|
if let Ok(mut file) = File::open(root.clone()) {
|
||||||
let mut src = String::new();
|
let mut src = String::new();
|
||||||
file.read_to_string(&mut src).expect("Unable to read file");
|
file.read_to_string(&mut src).expect("Unable to read file");
|
||||||
files.insert(root, src);
|
files.insert(root, src);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Ok(files)
|
Ok(files)
|
||||||
}
|
}
|
||||||
Self {
|
|
||||||
last_updated_time: SystemTime::now(),
|
let last_updated_time = SystemTime::now();
|
||||||
|
let result = Self {
|
||||||
|
last_updated_time,
|
||||||
map: find_rs_files(path).unwrap(),
|
map: find_rs_files(path).unwrap(),
|
||||||
|
};
|
||||||
|
log::info!("Files updated");
|
||||||
|
result
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn update(&mut self, path: PathBuf) {
|
||||||
|
log::info!("Updating files that changed since last compile...");
|
||||||
|
self.last_updated_time = SystemTime::now();
|
||||||
|
self.update_inner(path);
|
||||||
|
log::info!("Files updated");
|
||||||
|
}
|
||||||
|
|
||||||
|
fn update_inner(&mut self, path: PathBuf) {
|
||||||
|
if path.is_dir() {
|
||||||
|
if let Ok(files) = fs::read_dir(path) {
|
||||||
|
for entry in files {
|
||||||
|
if let Ok(entry) = entry {
|
||||||
|
if let Ok(md) = entry.metadata() {
|
||||||
|
if let Ok(time) = md.modified() {
|
||||||
|
if time < self.last_updated_time {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let path = entry.path();
|
||||||
|
self.update(path);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if path.extension().map(|s| s.to_str()).flatten() == Some("rs") {
|
||||||
|
if let Ok(mut file) = File::open(path.clone()) {
|
||||||
|
if let Ok(md) = file.metadata() {
|
||||||
|
if let Ok(time) = md.modified() {
|
||||||
|
if time < self.last_updated_time {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let mut src = String::new();
|
||||||
|
file.read_to_string(&mut src).expect("Unable to read file");
|
||||||
|
self.map.insert(path, src);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -70,7 +115,6 @@ pub async fn hot_reload_handler(
|
|||||||
ws.on_upgrade(|mut socket| async move {
|
ws.on_upgrade(|mut socket| async move {
|
||||||
log::info!("🔥 Hot Reload WebSocket connected");
|
log::info!("🔥 Hot Reload WebSocket connected");
|
||||||
{
|
{
|
||||||
log::info!("Searching files for changes since last run...");
|
|
||||||
// update any files that changed before the websocket connected.
|
// update any files that changed before the websocket connected.
|
||||||
let mut messages = Vec::new();
|
let mut messages = Vec::new();
|
||||||
|
|
||||||
@@ -123,16 +167,14 @@ pub async fn hot_reload_handler(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for msg in &messages {
|
let msg = SetManyRsxMessage(messages);
|
||||||
if socket
|
if socket
|
||||||
.send(Message::Text(serde_json::to_string(msg).unwrap()))
|
.send(Message::Text(serde_json::to_string(&msg).unwrap()))
|
||||||
.await
|
.await
|
||||||
.is_err()
|
.is_err()
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
log::info!("Updated page");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut rx = state.messages.subscribe();
|
let mut rx = state.messages.subscribe();
|
||||||
@@ -149,8 +191,7 @@ pub async fn hot_reload_handler(
|
|||||||
Error::ParseError(parse_error) => {
|
Error::ParseError(parse_error) => {
|
||||||
log::error!("parse error:\n--> at {}:{}:{}\n\t{:?}", parse_error.location.file, parse_error.location.line, parse_error.location.column, parse_error.message);
|
log::error!("parse error:\n--> at {}:{}:{}\n\t{:?}", parse_error.location.file, parse_error.location.line, parse_error.location.column, parse_error.message);
|
||||||
},
|
},
|
||||||
Error::RecompileRequiredError(reason) => {
|
Error::RecompileRequiredError(_) => {
|
||||||
log::info!("{:?}", reason);
|
|
||||||
if state.update.send("reload".to_string()).is_err() {
|
if state.update.send("reload".to_string()).is_err() {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|||||||
+66
-54
@@ -6,6 +6,7 @@ use axum::{
|
|||||||
routing::{get, get_service},
|
routing::{get, get_service},
|
||||||
Router,
|
Router,
|
||||||
};
|
};
|
||||||
|
use dioxus_rsx_interpreter::SetRsxMessage;
|
||||||
use notify::{RecommendedWatcher, Watcher};
|
use notify::{RecommendedWatcher, Watcher};
|
||||||
use syn::spanned::Spanned;
|
use syn::spanned::Spanned;
|
||||||
|
|
||||||
@@ -67,67 +68,75 @@ pub async fn startup_hot_reload(config: CrateConfig) -> Result<()> {
|
|||||||
.unwrap_or_else(|| vec![PathBuf::from("src")]);
|
.unwrap_or_else(|| vec![PathBuf::from("src")]);
|
||||||
|
|
||||||
let mut watcher = RecommendedWatcher::new(move |evt: notify::Result<notify::Event>| {
|
let mut watcher = RecommendedWatcher::new(move |evt: notify::Result<notify::Event>| {
|
||||||
if let Ok(evt) = evt {
|
if chrono::Local::now().timestamp() > last_update_time {
|
||||||
for path in evt.paths {
|
if let Ok(evt) = evt {
|
||||||
let mut file = File::open(path.clone()).unwrap();
|
let mut messages = Vec::new();
|
||||||
if path.extension().map(|p| p.to_str()).flatten() != Some("rs") {
|
for path in evt.paths {
|
||||||
continue;
|
let mut file = File::open(path.clone()).unwrap();
|
||||||
}
|
if path.extension().map(|p| p.to_str()).flatten() != Some("rs") {
|
||||||
let mut src = String::new();
|
continue;
|
||||||
file.read_to_string(&mut src).expect("Unable to read file");
|
}
|
||||||
if src.is_empty() {
|
let mut src = String::new();
|
||||||
continue;
|
file.read_to_string(&mut src).expect("Unable to read file");
|
||||||
}
|
if src.is_empty() {
|
||||||
// find changes to the rsx in the file
|
continue;
|
||||||
if let Ok(syntax) = syn::parse_file(&src) {
|
}
|
||||||
let mut last_file_rebuild = last_file_rebuild.lock().unwrap();
|
// find changes to the rsx in the file
|
||||||
if let Some(old_str) = last_file_rebuild.map.get(&path) {
|
if let Ok(syntax) = syn::parse_file(&src) {
|
||||||
if let Ok(old) = syn::parse_file(&old_str) {
|
let mut last_file_rebuild = last_file_rebuild.lock().unwrap();
|
||||||
match find_rsx(&syntax, &old) {
|
if let Some(old_str) = last_file_rebuild.map.get(&path) {
|
||||||
DiffResult::CodeChanged => {
|
if let Ok(old) = syn::parse_file(&old_str) {
|
||||||
log::info!("reload required");
|
match find_rsx(&syntax, &old) {
|
||||||
if chrono::Local::now().timestamp() > last_update_time {
|
DiffResult::CodeChanged => {
|
||||||
let _ = reload_tx.send("reload".into());
|
log::info!("reload required");
|
||||||
last_update_time = chrono::Local::now().timestamp();
|
let _ = reload_tx.send("reload time".into());
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
}
|
DiffResult::RsxChanged(changed) => {
|
||||||
DiffResult::RsxChanged(changed) => {
|
log::info!("reloading rsx");
|
||||||
log::info!("reloading rsx");
|
for (old, new) in changed.into_iter() {
|
||||||
for (old, new) in changed.into_iter() {
|
let hr = get_location(
|
||||||
let hr = get_location(
|
&path
|
||||||
&path.strip_prefix(&crate_dir).unwrap().to_path_buf(),
|
.strip_prefix(&crate_dir)
|
||||||
old.to_token_stream(),
|
.unwrap()
|
||||||
);
|
.to_path_buf(),
|
||||||
// get the original source code to preserve whitespace
|
old.to_token_stream(),
|
||||||
let span = new.span();
|
);
|
||||||
let start = span.start();
|
// get the original source code to preserve whitespace
|
||||||
let end = span.end();
|
let span = new.span();
|
||||||
let mut lines: Vec<_> = src
|
let start = span.start();
|
||||||
.lines()
|
let end = span.end();
|
||||||
.skip(start.line - 1)
|
let mut lines: Vec<_> = src
|
||||||
.take(end.line - start.line + 1)
|
.lines()
|
||||||
.collect();
|
.skip(start.line - 1)
|
||||||
if let Some(first) = lines.first_mut() {
|
.take(end.line - start.line + 1)
|
||||||
*first = first.split_at(start.column).1;
|
.collect();
|
||||||
|
if let Some(first) = lines.first_mut() {
|
||||||
|
*first = first.split_at(start.column).1;
|
||||||
|
}
|
||||||
|
if let Some(last) = lines.last_mut() {
|
||||||
|
*last = last.split_at(end.column).0;
|
||||||
|
}
|
||||||
|
let rsx = lines.join("\n");
|
||||||
|
messages.push(SetRsxMessage {
|
||||||
|
location: hr,
|
||||||
|
new_text: rsx,
|
||||||
|
});
|
||||||
}
|
}
|
||||||
if let Some(last) = lines.last_mut() {
|
|
||||||
*last = last.split_at(end.column).0;
|
|
||||||
}
|
|
||||||
let rsx = lines.join("\n");
|
|
||||||
let _ = hot_reload_tx.send(SetRsxMessage {
|
|
||||||
location: hr,
|
|
||||||
new_text: rsx,
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
// if this is a new file, rebuild the project
|
||||||
|
*last_file_rebuild = FileMap::new(crate_dir.clone());
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
// if this is a new file, rebuild the project
|
|
||||||
*last_file_rebuild = FileMap::new(crate_dir.clone());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if !messages.is_empty() {
|
||||||
|
let _ = hot_reload_tx.send(SetManyRsxMessage(messages));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
last_update_time = chrono::Local::now().timestamp();
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.unwrap();
|
.unwrap();
|
||||||
@@ -313,7 +322,7 @@ async fn ws_handler(
|
|||||||
_: Option<TypedHeader<headers::UserAgent>>,
|
_: Option<TypedHeader<headers::UserAgent>>,
|
||||||
Extension(state): Extension<Arc<WsReloadState>>,
|
Extension(state): Extension<Arc<WsReloadState>>,
|
||||||
) -> impl IntoResponse {
|
) -> impl IntoResponse {
|
||||||
ws.on_upgrade(|mut socket| async move {
|
ws.max_send_queue(1).on_upgrade(|mut socket| async move {
|
||||||
let mut rx = state.update.subscribe();
|
let mut rx = state.update.subscribe();
|
||||||
let reload_watcher = tokio::spawn(async move {
|
let reload_watcher = tokio::spawn(async move {
|
||||||
loop {
|
loop {
|
||||||
@@ -335,7 +344,7 @@ async fn ws_handler(
|
|||||||
}
|
}
|
||||||
if let Some(file_map) = &state.last_file_rebuild {
|
if let Some(file_map) = &state.last_file_rebuild {
|
||||||
let mut write = file_map.lock().unwrap();
|
let mut write = file_map.lock().unwrap();
|
||||||
*write = FileMap::new(state.watcher_config.crate_dir.clone());
|
write.update(state.watcher_config.crate_dir.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// ignore the error
|
// ignore the error
|
||||||
@@ -346,6 +355,9 @@ async fn ws_handler(
|
|||||||
{
|
{
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// flush the errors after recompling
|
||||||
|
rx = rx.resubscribe();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|||||||
Reference in new issue
Block a user