Skip to content
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

refactor: share constants between backend and frontend #474

Merged
merged 2 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ tauri-sys = { git = "https://github.com/JonasKruckenberg/tauri-sys", branch = "v
thaw = { version = "0.3.4", features = ["csr"] }
thaw_utils = { version = "0.0.6", features = ["csr"] }
tokio = "1.40.0"
shared_constants = { path = "./shared_constants" }

[workspace]
members = ["src-tauri"]
members = ["shared_constants", "src-tauri"]
6 changes: 6 additions & 0 deletions shared_constants/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[package]
name = "shared_constants"
version = "0.1.0"
edition = "2021"

[dependencies]
6 changes: 6 additions & 0 deletions shared_constants/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
use std::time::Duration;
pub const MDNS_SD_META_SERVICE: &str = "_services._dns-sd._udp.local.";
pub const METRICS_CHECK_INTERVAL: Duration = Duration::from_secs(1);
pub const SPLASH_SCREEN_DURATION: Duration = Duration::from_secs(2);
pub const AUTO_COMPLETE_AUTO_FOCUS_DELAY: Duration = Duration::from_secs(5);
pub const SHOW_NO_UPDATE_DURATION: Duration = Duration::from_secs(3);
1 change: 1 addition & 0 deletions src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ serde_json = "1.0.128"
tauri = { version = "2.0.0", features = [] }
tauri-plugin-clipboard-manager = "2.0.0"
tauri-plugin-shell = "2"
shared_constants = { path = "../shared_constants" }

[lib]
name = "mdns_browser_lib"
Expand Down
19 changes: 9 additions & 10 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@ use clap::builder::TypedValueParser as _;
use clap::Parser;
use mdns_sd::{ServiceDaemon, ServiceEvent, ServiceInfo};
use serde::Serialize;

#[cfg(not(debug_assertions))]
use shared_constants::SPLASH_SCREEN_DURATION;
use shared_constants::{MDNS_SD_META_SERVICE, METRICS_CHECK_INTERVAL};
use std::{
collections::HashMap,
net::IpAddr,
sync::{Arc, Mutex},
time::{Duration, SystemTime},
time::SystemTime,
};

use tauri::Emitter;
use tauri::{AppHandle, Manager, State, Window};
use tauri_plugin_clipboard_manager::ClipboardExt;
Expand Down Expand Up @@ -144,15 +147,13 @@ type ServiceFoundEvent = ServiceRemovedEvent;
type ServiceTypeFoundEvent = SearchStartedEvent;
type ServiceTypeRemovedEvent = SearchStartedEvent;

const META_SERVICE: &str = "_services._dns-sd._udp.local.";

#[tauri::command]
fn browse_types(window: Window, state: State<ManagedState>) {
if let Ok(mdns) = state.daemon.lock() {
let mdns_for_thread = mdns.clone();
std::thread::spawn(move || {
let receiver = mdns_for_thread
.browse(META_SERVICE)
.browse(MDNS_SD_META_SERVICE)
.expect("Failed to browse");
while let Ok(event) = receiver.recv() {
match event {
Expand All @@ -179,7 +180,7 @@ fn browse_types(window: Window, state: State<ManagedState>) {
.expect("To emit");
}
ServiceEvent::SearchStopped(service_type) => {
if service_type == META_SERVICE {
if service_type == MDNS_SD_META_SERVICE {
break;
}
}
Expand Down Expand Up @@ -272,15 +273,13 @@ fn browse(service_type: String, window: Window, state: State<ManagedState>) {
}
}

const METRIC_CHECK_INTERVAL: Duration = Duration::from_secs(1);

#[tauri::command]
fn send_metrics(window: Window, state: State<ManagedState>) {
if let Ok(mdns) = state.daemon.lock() {
let mdns_for_thread = mdns.clone();
let mut old_metrics = HashMap::new();
std::thread::spawn(move || loop {
std::thread::sleep(METRIC_CHECK_INTERVAL);
std::thread::sleep(METRICS_CHECK_INTERVAL);
if let Ok(metrics_receiver) = mdns_for_thread.get_metrics() {
if let Ok(metrics) = metrics_receiver.recv() {
if old_metrics != metrics {
Expand Down Expand Up @@ -543,7 +542,7 @@ pub fn run() {
let main_window = app.get_webview_window("main").unwrap();
tauri::async_runtime::spawn(async move {
#[cfg(not(debug_assertions))]
tokio::time::sleep(Duration::from_millis(2000)).await;
tokio::time::sleep(SPLASH_SCREEN_DURATION).await;
splashscreen_window.close().unwrap();
main_window.show().unwrap();
#[cfg(debug_assertions)]
Expand Down
7 changes: 5 additions & 2 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ use leptos::*;
use leptos_meta::provide_meta_context;
use leptos_meta::Style;
use serde::{Deserialize, Serialize};
use shared_constants::{
AUTO_COMPLETE_AUTO_FOCUS_DELAY, SHOW_NO_UPDATE_DURATION, SPLASH_SCREEN_DURATION,
};
use std::{
collections::{HashMap, HashSet},
fmt::Display,
Expand Down Expand Up @@ -330,7 +333,7 @@ fn AutoCompleteServiceType(
comp.focus();
}
},
std::time::Duration::from_millis(5000),
SPLASH_SCREEN_DURATION + AUTO_COMPLETE_AUTO_FOCUS_DELAY,
);
});
});
Expand Down Expand Up @@ -833,7 +836,7 @@ pub fn About() -> impl IntoView {
move || {
show_no_update.set(false);
},
std::time::Duration::new(3, 0),
SHOW_NO_UPDATE_DURATION,
);
};

Expand Down