Skip to content

Commit e11a570

Browse files
committed
perf(tauri): modularize native desktop event transport
fix(tauri): stop desktop event transport on window close
1 parent 0af7900 commit e11a570

23 files changed

Lines changed: 3918 additions & 142 deletions

packages/tauri-app/Cargo.lock

Lines changed: 263 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/tauri-app/Cargo.toml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
11
[workspace]
22
members = ["src-tauri"]
33
resolver = "2"
4+
5+
[profile.release]
6+
panic = "abort"
7+
lto = true
8+
codegen-units = 1
9+
opt-level = "s"
10+
strip = true

packages/tauri-app/src-tauri/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ tauri-plugin-opener = "2"
2626
tauri-plugin-global-shortcut = "2"
2727
url = "2"
2828
tauri-plugin-notification = "2"
29+
reqwest = { version = "0.12", default-features = false, features = ["blocking", "json", "rustls-tls"] }
2930

3031
[target.'cfg(windows)'.dependencies]
3132
windows-sys = { version = "0.59", features = ["Win32_UI_Shell"] }

packages/tauri-app/src-tauri/src/cli_manager.rs

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use crate::desktop_event_transport::DesktopEventStreamConfig;
12
use dirs::home_dir;
23
use parking_lot::Mutex;
34
use regex::Regex;
@@ -346,6 +347,7 @@ pub struct CliProcessManager {
346347
child: Arc<Mutex<Option<Child>>>,
347348
ready: Arc<AtomicBool>,
348349
bootstrap_token: Arc<Mutex<Option<String>>>,
350+
session_cookie: Arc<Mutex<Option<String>>>,
349351
}
350352

351353
impl CliProcessManager {
@@ -355,6 +357,7 @@ impl CliProcessManager {
355357
child: Arc::new(Mutex::new(None)),
356358
ready: Arc::new(AtomicBool::new(false)),
357359
bootstrap_token: Arc::new(Mutex::new(None)),
360+
session_cookie: Arc::new(Mutex::new(None)),
358361
}
359362
}
360363

@@ -363,6 +366,7 @@ impl CliProcessManager {
363366
self.stop()?;
364367
self.ready.store(false, Ordering::SeqCst);
365368
*self.bootstrap_token.lock() = None;
369+
*self.session_cookie.lock() = None;
366370
{
367371
let mut status = self.status.lock();
368372
status.state = CliState::Starting;
@@ -377,13 +381,15 @@ impl CliProcessManager {
377381
let child_arc = self.child.clone();
378382
let ready_flag = self.ready.clone();
379383
let token_arc = self.bootstrap_token.clone();
384+
let session_cookie_arc = self.session_cookie.clone();
380385
thread::spawn(move || {
381386
if let Err(err) = Self::spawn_cli(
382387
app.clone(),
383388
status_arc.clone(),
384389
child_arc,
385390
ready_flag,
386391
token_arc,
392+
session_cookie_arc,
387393
dev,
388394
) {
389395
log_line(&format!("cli spawn failed: {err}"));
@@ -480,6 +486,7 @@ impl CliProcessManager {
480486
status.port = None;
481487
status.url = None;
482488
status.error = None;
489+
*self.session_cookie.lock() = None;
483490

484491
Ok(())
485492
}
@@ -488,12 +495,27 @@ impl CliProcessManager {
488495
self.status.lock().clone()
489496
}
490497

498+
pub fn desktop_event_stream_config(&self) -> Option<DesktopEventStreamConfig> {
499+
let base_url = self.status.lock().url.clone()?;
500+
let events_url = format!("{}/api/events", base_url.trim_end_matches('/'));
501+
let client_id = format!("tauri-{}", std::process::id());
502+
503+
Some(DesktopEventStreamConfig {
504+
base_url,
505+
events_url,
506+
client_id,
507+
cookie_name: SESSION_COOKIE_NAME.to_string(),
508+
session_cookie: self.session_cookie.lock().clone(),
509+
})
510+
}
511+
491512
fn spawn_cli(
492513
app: AppHandle,
493514
status: Arc<Mutex<CliStatus>>,
494515
child_holder: Arc<Mutex<Option<Child>>>,
495516
ready: Arc<AtomicBool>,
496517
bootstrap_token: Arc<Mutex<Option<String>>>,
518+
session_cookie: Arc<Mutex<Option<String>>>,
497519
dev: bool,
498520
) -> anyhow::Result<()> {
499521
log_line("resolving CLI entry");
@@ -584,6 +606,7 @@ impl CliProcessManager {
584606
let app_clone = app.clone();
585607
let ready_clone = ready.clone();
586608
let token_clone = bootstrap_token.clone();
609+
let session_cookie_clone = session_cookie.clone();
587610

588611
thread::spawn(move || {
589612
let stdout = child_clone
@@ -605,6 +628,7 @@ impl CliProcessManager {
605628
&status_clone,
606629
&ready_clone,
607630
&token_clone,
631+
&session_cookie_clone,
608632
);
609633
}
610634
if let Some(reader) = stderr {
@@ -615,6 +639,7 @@ impl CliProcessManager {
615639
&status_clone,
616640
&ready_clone,
617641
&token_clone,
642+
&session_cookie_clone,
618643
);
619644
}
620645
});
@@ -731,6 +756,7 @@ impl CliProcessManager {
731756
status: &Arc<Mutex<CliStatus>>,
732757
ready: &Arc<AtomicBool>,
733758
bootstrap_token: &Arc<Mutex<Option<String>>>,
759+
session_cookie: &Arc<Mutex<Option<String>>>,
734760
) {
735761
let mut buffer = String::new();
736762
let local_url_regex = Regex::new(r"^Local\s+Connection\s+URL\s*:\s*(https?://\S+)").ok();
@@ -766,7 +792,14 @@ impl CliProcessManager {
766792
.and_then(|re| re.captures(line).and_then(|c| c.get(1)))
767793
.map(|m| m.as_str().to_string())
768794
{
769-
Self::mark_ready(app, status, ready, bootstrap_token, url);
795+
Self::mark_ready(
796+
app,
797+
status,
798+
ready,
799+
bootstrap_token,
800+
&session_cookie,
801+
url,
802+
);
770803
continue;
771804
}
772805

@@ -781,6 +814,7 @@ impl CliProcessManager {
781814
status,
782815
ready,
783816
bootstrap_token,
817+
&session_cookie,
784818
format!("http://localhost:{port}"),
785819
);
786820
continue;
@@ -793,6 +827,7 @@ impl CliProcessManager {
793827
status,
794828
ready,
795829
bootstrap_token,
830+
&session_cookie,
796831
format!("http://localhost:{}", port),
797832
);
798833
continue;
@@ -811,6 +846,7 @@ impl CliProcessManager {
811846
status: &Arc<Mutex<CliStatus>>,
812847
ready: &Arc<AtomicBool>,
813848
bootstrap_token: &Arc<Mutex<Option<String>>>,
849+
session_cookie: &Arc<Mutex<Option<String>>>,
814850
base_url: String,
815851
) {
816852
ready.store(true, Ordering::SeqCst);
@@ -840,6 +876,7 @@ impl CliProcessManager {
840876
log_line(&format!("failed to set session cookie: {err}"));
841877
navigate_main(app, &format!("{base_url}/login"));
842878
} else {
879+
*session_cookie.lock() = Some(session_id.clone());
843880
navigate_main(app, &base_url);
844881
}
845882
}

0 commit comments

Comments
 (0)