tauri_plugin_sse/
lib.rs

1use tauri::{
2  async_runtime::Mutex, plugin::{Builder, TauriPlugin}, Manager, Runtime
3};
4
5pub use models::*;
6
7#[cfg(desktop)]
8mod desktop;
9#[cfg(mobile)]
10mod mobile;
11
12mod commands;
13mod error;
14mod models;
15mod scope;
16
17pub use error::{Error, Result};
18
19#[cfg(desktop)]
20use desktop::Sse;
21#[cfg(mobile)]
22use mobile::Sse;
23
24/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the sse APIs.
25pub trait SseExt<R: Runtime> {
26  fn sse(&self) -> &Sse<R>;
27}
28
29impl<R: Runtime, T: Manager<R>> crate::SseExt<R> for T {
30  fn sse(&self) -> &Sse<R> {
31    self.state::<Sse<R>>().inner()
32  }
33}
34
35/// Initializes the plugin.
36pub fn init<R: Runtime>() -> TauriPlugin<R> {
37  Builder::new("sse")
38    .invoke_handler(tauri::generate_handler![commands::open_sse, 
39      commands::close_sse,
40      commands::add_on_message_sse,
41      commands::add_on_error_sse,
42      commands::add_event_listener_sse,
43      commands::remove_event_listener_sse
44    ])
45    .setup(|app, api| {
46      #[cfg(mobile)]
47      let _sse = mobile::init(app, api)?;
48      #[cfg(desktop)]
49      let _sse = desktop::init(app, api)?;
50      app.manage(Mutex::new(commands::AppState::default()));
51      Ok(())
52    })
53    .build()
54}