tauri-plugin-sse 0.1.1

A simple Tauri plugin for Server-Sent Events (SSE), enabling real-time, one-way updates from server to your Tauri frontend.
Documentation
use tauri::{
  async_runtime::Mutex, plugin::{Builder, TauriPlugin}, Manager, Runtime
};

pub use models::*;

#[cfg(desktop)]
mod desktop;
#[cfg(mobile)]
mod mobile;

mod commands;
mod error;
mod models;
mod scope;

pub use error::{Error, Result};

#[cfg(desktop)]
use desktop::Sse;
#[cfg(mobile)]
use mobile::Sse;

/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the sse APIs.
pub trait SseExt<R: Runtime> {
  fn sse(&self) -> &Sse<R>;
}

impl<R: Runtime, T: Manager<R>> crate::SseExt<R> for T {
  fn sse(&self) -> &Sse<R> {
    self.state::<Sse<R>>().inner()
  }
}

/// Initializes the plugin.
pub fn init<R: Runtime>() -> TauriPlugin<R> {
  Builder::new("sse")
    .invoke_handler(tauri::generate_handler![commands::open_sse, 
      commands::close_sse,
      commands::add_on_message_sse,
      commands::add_on_error_sse,
      commands::add_event_listener_sse,
      commands::remove_event_listener_sse
    ])
    .setup(|app, api| {
      #[cfg(mobile)]
      let _sse = mobile::init(app, api)?;
      #[cfg(desktop)]
      let _sse = desktop::init(app, api)?;
      app.manage(Mutex::new(commands::AppState::default()));
      Ok(())
    })
    .build()
}