Skip to content

Commit

Permalink
Add ability to listen to node events from the frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinez committed Sep 10, 2024
1 parent c558235 commit acc33bf
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 1 deletion.
1 change: 1 addition & 0 deletions src-tauri/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 src-tauri/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ tauri-build = { version = "2.0.0-rc.0", features = ["isolation"] }

[dependencies]
anyhow = { version = "1.0.86" }
log = { version = "0.4.22" }
radicle = { version = "0.12.0" }
radicle-surf = { version = "0.21.0" }
serde = { version = "1.0", features = ["derive"] }
Expand Down
4 changes: 4 additions & 0 deletions src-tauri/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,10 @@ pub enum Error {
hint: &'static str,
},

/// Tauri error.
#[error(transparent)]
Tauri(#[from] tauri::Error),

/// Storage error.
#[error(transparent)]
Storage(#[from] radicle::storage::Error),
Expand Down
40 changes: 40 additions & 0 deletions src-tauri/src/events.rs
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(())
}
24 changes: 23 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
mod commands;
mod error;
mod events;
mod types;

use std::sync::atomic::AtomicBool;
use std::sync::Arc;

use serde_json::json;
use tauri::Listener;
use tauri::Manager;

use radicle::identity::doc::PayloadId;
Expand All @@ -15,6 +20,7 @@ use radicle::storage::git::Repository;
use radicle::storage::{ReadRepository, ReadStorage};

use commands::{auth, cobs, profile, repos};
use events::subscribe_events;
use types::repo::SupportedPayloads;

struct AppState {
Expand Down Expand Up @@ -97,7 +103,23 @@ pub fn run() {
}),
}?;

app.manage(AppState { profile });
app.manage(AppState {
profile: profile.clone(),
});

let existing_events_thread: Arc<AtomicBool> = Arc::new(AtomicBool::new(false));

let events_handler = app.handle().clone();
app.listen("subscribe_events", move |_| {
let profile = profile.clone();
let existing_events_thread = existing_events_thread.clone();

let events_handler = events_handler.to_owned();
tauri::async_runtime::spawn(async move {
let _result =
subscribe_events(&events_handler, profile, existing_events_thread).await;
});
});

Ok(())
})
Expand Down
8 changes: 8 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,24 @@
import { onMount } from "svelte";
import { invoke } from "@tauri-apps/api/core";
import { listen } from "@tauri-apps/api/event";
import * as router from "@app/lib/router";
import { theme } from "@app/components/ThemeSwitch.svelte";
import { subscribeToNodeEvents } from "@app/lib/events";
import { unreachable } from "@app/lib/utils";
import AuthenticationError from "@app/views/AuthenticationError.svelte";
import Home from "@app/views/Home.svelte";
import Issues from "@app/views/repo/Issues.svelte";
import Patches from "@app/views/repo/Patches.svelte";
subscribeToNodeEvents();
void listen("event", event => {
console.log(event.payload);
});
const activeRouteStore = router.activeRouteStore;
onMount(async () => {
Expand Down
12 changes: 12 additions & 0 deletions src/lib/events.ts
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);
}
}

0 comments on commit acc33bf

Please sign in to comment.