-
Notifications
You must be signed in to change notification settings - Fork 536
feat: add tauri-plugin-network for network availability monitoring #2069
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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, | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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() | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.