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 5, 2024
1 parent 169866e commit bff0ece
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
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
22 changes: 22 additions & 0 deletions src-tauri/src/events.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use radicle::Profile;
use radicle::{node::Handle, Node};
use tauri::{AppHandle, Emitter};

use crate::error::Error;

pub async fn subscribe_events(handle: &AppHandle, profile: Profile) -> Result<(), Error> {
let node = Node::new(profile.socket());

let event_handler = handle.clone();
let join_handle = tauri::async_runtime::spawn(async move {
while let Ok(events) = node.subscribe(std::time::Duration::MAX) {
for event in events.into_iter().flatten() {
let _result = event_handler.emit("event", event);
}
}
});

join_handle.await?;

Ok(())
}
17 changes: 16 additions & 1 deletion src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
mod commands;
mod error;
mod events;
mod json;
mod types;

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

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

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

struct AppState {
Expand Down Expand Up @@ -93,7 +96,19 @@ pub fn run() {
}),
}?;

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

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

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

Ok(())
})
Expand Down
6 changes: 6 additions & 0 deletions src/App.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { onMount } from "svelte";
import { invoke } from "@tauri-apps/api/core";
import { emit, listen } from "@tauri-apps/api/event";
import * as router from "@app/lib/router";
import { theme } from "@app/components/ThemeSwitch.svelte";
Expand All @@ -11,6 +12,11 @@
import DesignSystem from "@app/views/DesignSystem.svelte";
import Home from "@app/views/Home.svelte";
void emit("subscribe_events");
void listen("event", event => {
console.log(event.payload);
});
const activeRouteStore = router.activeRouteStore;
void router.loadFromLocation();
Expand Down

0 comments on commit bff0ece

Please sign in to comment.