use async_trait::async_trait;
use tauri::Runtime;
use crate::error::ServiceError;
use crate::models::ServiceContext;
#[async_trait]
pub trait BackgroundService<R: Runtime>: Send + 'static {
async fn init(&mut self, ctx: &ServiceContext<R>) -> Result<(), ServiceError>;
async fn run(&mut self, ctx: &ServiceContext<R>) -> Result<(), ServiceError>;
}
#[cfg(test)]
mod tests {
use super::*;
use tauri::AppHandle;
use tokio_util::sync::CancellationToken;
use crate::notifier::Notifier;
#[allow(dead_code)]
struct DummyService;
#[async_trait]
impl BackgroundService<tauri::Wry> for DummyService {
async fn init(&mut self, _ctx: &ServiceContext<tauri::Wry>) -> Result<(), ServiceError> {
Ok(())
}
async fn run(&mut self, _ctx: &ServiceContext<tauri::Wry>) -> Result<(), ServiceError> {
Ok(())
}
}
#[allow(dead_code)]
fn box_dyn_compiles() {
let _boxed: Box<dyn BackgroundService<tauri::Wry>> = Box::new(DummyService);
}
#[allow(dead_code)]
fn service_context_constructs<R: Runtime>(app: AppHandle<R>) {
let _ctx = ServiceContext {
notifier: Notifier { app: app.clone() },
app,
shutdown: CancellationToken::new(),
#[cfg(mobile)]
service_label: "test".into(),
#[cfg(mobile)]
foreground_service_type: "dataSync".into(),
};
}
}