Skip to content
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
17 changes: 17 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ tauri-plugin-listener2 = { path = "plugins/listener2" }
tauri-plugin-local-llm = { path = "plugins/local-llm" }
tauri-plugin-local-stt = { path = "plugins/local-stt" }
tauri-plugin-misc = { path = "plugins/misc" }
tauri-plugin-network = { path = "plugins/network" }
tauri-plugin-notification = { path = "plugins/notification" }
tauri-plugin-permissions = { path = "plugins/permissions" }
tauri-plugin-sfx = { path = "plugins/sfx" }
Expand Down
1 change: 1 addition & 0 deletions apps/desktop/src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ tauri-plugin-listener = { workspace = true }
tauri-plugin-listener2 = { workspace = true }
tauri-plugin-local-stt = { workspace = true }
tauri-plugin-misc = { workspace = true }
tauri-plugin-network = { workspace = true }
tauri-plugin-notification = { workspace = true }
tauri-plugin-opener = { workspace = true }
tauri-plugin-os = { workspace = true }
Expand Down
7 changes: 7 additions & 0 deletions apps/desktop/src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,13 @@ pub async fn main() {
.map(|ctx| ctx.supervisor.get_cell()),
},
))
.plugin(tauri_plugin_network::init(
tauri_plugin_network::InitOptions {
parent_supervisor: root_supervisor_ctx
.as_ref()
.map(|ctx| ctx.supervisor.get_cell()),
},
))
.plugin(tauri_plugin_autostart::init(
tauri_plugin_autostart::MacosLauncher::LaunchAgent,
Some(vec!["--background"]),
Expand Down
27 changes: 27 additions & 0 deletions plugins/network/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
[package]
name = "tauri-plugin-network"
version = "0.1.0"
authors = ["You"]
edition = "2021"
exclude = ["/js", "/node_modules"]
links = "tauri-plugin-network"
description = ""

[build-dependencies]
tauri-plugin = { workspace = true, features = ["build"] }

[dev-dependencies]
specta-typescript = { workspace = true }

[dependencies]
tauri = { workspace = true, features = ["specta"] }
tauri-specta = { workspace = true, features = ["derive", "typescript"] }

serde = { workspace = true }
specta = { workspace = true }

ractor = { workspace = true }
ractor-supervisor = { workspace = true }

reqwest = { workspace = true }
tracing = { workspace = true }
5 changes: 5 additions & 0 deletions plugins/network/build.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const COMMANDS: &[&str] = &[];

fn main() {
tauri_plugin::Builder::new(COMMANDS).build();
}
97 changes: 97 additions & 0 deletions plugins/network/src/actor.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
use ractor::concurrency::Duration;
use ractor::{Actor, ActorProcessingErr, ActorRef};
use tauri_specta::Event;

use crate::event::NetworkStatusEvent;

const CHECK_INTERVAL: Duration = Duration::from_secs(2);
const CHECK_URL: &str = "https://www.google.com/generate_204";
const REQUEST_TIMEOUT: Duration = Duration::from_secs(5);

pub const NETWORK_ACTOR_NAME: &str = "network_actor";

pub enum NetworkMsg {
Check,
}

pub struct NetworkArgs {
pub app: tauri::AppHandle,
}

pub struct NetworkState {
app: tauri::AppHandle,
is_online: bool,
}

pub struct NetworkActor;

impl NetworkActor {
pub fn name() -> ractor::ActorName {
NETWORK_ACTOR_NAME.into()
}
}

#[ractor::async_trait]
impl Actor for NetworkActor {
type Msg = NetworkMsg;
type State = NetworkState;
type Arguments = NetworkArgs;

async fn pre_start(
&self,
myself: ActorRef<Self::Msg>,
args: Self::Arguments,
) -> Result<Self::State, ActorProcessingErr> {
schedule_check(myself);

Ok(NetworkState {
app: args.app,
is_online: true,
})
}

async fn handle(
&self,
myself: ActorRef<Self::Msg>,
message: Self::Msg,
state: &mut Self::State,
) -> Result<(), ActorProcessingErr> {
match message {
NetworkMsg::Check => {
let is_online = check_network().await;

if is_online != state.is_online {
state.is_online = is_online;

let event = NetworkStatusEvent { is_online };
if let Err(e) = event.emit(&state.app) {
tracing::error!(?e, "failed_to_emit_network_status_event");
}
}

schedule_check(myself);
}
}
Ok(())
}
}

fn schedule_check(actor: ActorRef<NetworkMsg>) {
ractor::time::send_after(CHECK_INTERVAL, actor.get_cell(), || NetworkMsg::Check);
}

async fn check_network() -> bool {
let client = reqwest::Client::builder()
.timeout(REQUEST_TIMEOUT.into())
.build();

let client = match client {
Ok(c) => c,
Err(_) => return false,
};

match client.head(CHECK_URL).send().await {
Ok(response) => response.status().is_success() || response.status().as_u16() == 204,
Err(_) => false,
}
}
4 changes: 4 additions & 0 deletions plugins/network/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#[derive(serde::Serialize, Clone, specta::Type, tauri_specta::Event)]
pub struct NetworkStatusEvent {
pub is_online: bool,
}
76 changes: 76 additions & 0 deletions plugins/network/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
use ractor::{Actor, ActorCell};
use tauri::Manager;

mod actor;
pub mod event;

pub use actor::*;
pub use event::*;

const PLUGIN_NAME: &str = "network";

#[derive(Default)]
pub struct InitOptions {
pub parent_supervisor: Option<ActorCell>,
}

fn make_specta_builder<R: tauri::Runtime>() -> tauri_specta::Builder<R> {
tauri_specta::Builder::<R>::new()
.plugin_name(PLUGIN_NAME)
.events(tauri_specta::collect_events![NetworkStatusEvent])
.error_handling(tauri_specta::ErrorHandlingMode::Result)
}

pub fn init(options: InitOptions) -> tauri::plugin::TauriPlugin<tauri::Wry> {
let specta_builder = make_specta_builder();

tauri::plugin::Builder::new(PLUGIN_NAME)
.invoke_handler(specta_builder.invoke_handler())
.setup(move |app, _api| {
specta_builder.mount_events(app);

let app_handle = app.app_handle().clone();
let parent = options.parent_supervisor.clone();

tauri::async_runtime::spawn(async move {
match Actor::spawn(
Some(NetworkActor::name()),
NetworkActor,
NetworkArgs { app: app_handle },
)
.await
{
Ok((actor_ref, _)) => {
if let Some(parent_cell) = parent {
actor_ref.get_cell().link(parent_cell);
}
tracing::info!("network_actor_spawned");
}
Err(e) => {
tracing::error!(?e, "failed_to_spawn_network_actor");
}
}
});

Ok(())
})
.build()
}

#[cfg(test)]
mod test {
use super::*;

#[test]
fn export_types() {
make_specta_builder::<tauri::Wry>()
.export(
specta_typescript::Typescript::default()
.header("// @ts-nocheck\n\n")
.formatter(specta_typescript::formatter::prettier)
.bigint(specta_typescript::BigIntExportBehavior::Number),
"./js/bindings.gen.ts",
)
.unwrap()
}
}