-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to listen to node events from the frontend
- Loading branch information
1 parent
c558235
commit acc33bf
Showing
7 changed files
with
89 additions
and
1 deletion.
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 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 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 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,40 @@ | ||
use std::sync::{atomic::AtomicBool, Arc}; | ||
|
||
use radicle::{node::Handle, Node, Profile}; | ||
use tauri::{AppHandle, Emitter}; | ||
|
||
use crate::error::Error; | ||
|
||
pub async fn subscribe_events( | ||
handle: &AppHandle, | ||
profile: Profile, | ||
existing_events_thread: Arc<AtomicBool>, | ||
) -> Result<(), Error> { | ||
let event_handler = handle.clone(); | ||
let node = Node::new(profile.socket()); | ||
if !node.is_running() { | ||
log::debug!("node: not subscribing to events due to stopped node."); | ||
return Ok(()); | ||
}; | ||
|
||
if existing_events_thread.load(std::sync::atomic::Ordering::SeqCst) { | ||
log::debug!("node: not subscribing to events due to a running subscription."); | ||
return Ok(()); | ||
} else { | ||
let join_handle = tauri::async_runtime::spawn(async move { | ||
log::debug!("node: spawned node event subscription."); | ||
while let Ok(events) = node.subscribe(std::time::Duration::MAX) { | ||
existing_events_thread.store(true, std::sync::atomic::Ordering::SeqCst); | ||
for event in events.into_iter().flatten() { | ||
let _result = event_handler.emit("event", event); | ||
} | ||
} | ||
existing_events_thread.store(false, std::sync::atomic::Ordering::SeqCst); | ||
log::debug!("node: event subscription loop has exited."); | ||
}); | ||
|
||
join_handle.await?; | ||
} | ||
|
||
Ok(()) | ||
} |
This file contains 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 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 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,12 @@ | ||
import { emit } from "@tauri-apps/api/event"; | ||
|
||
let interval: NodeJS.Timeout | undefined = undefined; | ||
|
||
export function subscribeToNodeEvents() { | ||
if (interval === undefined) { | ||
interval = setInterval(() => { | ||
// In case there is a running subscription this won't launch a new one. | ||
void emit("subscribe_events"); | ||
}, 30_000); | ||
} | ||
} |