use tauri::plugin::Plugin; use tauri::{AppHandle, Builder, Invoke, Manager, Runtime}; use state::ApiState; use crate::tauri_plugin::state::{AppState, BufferState}; use std::thread; use std::time::Duration; pub(crate) mod commands; pub mod custom_schemes; pub mod error; mod settings; mod state; use commands::*; pub fn register_plugin(builder: Builder) -> Builder { let repo_plugin = MediarepoPlugin::new(); custom_schemes::register_custom_uri_schemes(builder.plugin(repo_plugin)) } pub struct MediarepoPlugin { invoke_handler: Box) + Send + Sync>, } impl MediarepoPlugin { pub fn new() -> Self { Self { invoke_handler: Box::new(tauri::generate_handler![ get_all_files, find_files, read_file_by_hash, get_file_thumbnails, read_thumbnail, get_repositories, get_all_tags, get_tags_for_file, get_active_repository, add_repository, select_repository ]), } } } impl Plugin for MediarepoPlugin { fn name(&self) -> &'static str { "mediarepo" } #[tracing::instrument(skip(self, app, _config))] fn initialize( &mut self, app: &AppHandle, _config: serde_json::value::Value, ) -> tauri::plugin::Result<()> { let api_state = ApiState::new(); app.manage(api_state); let buffer_state = BufferState::default(); app.manage(buffer_state.clone()); thread::spawn(move || loop { thread::sleep(Duration::from_secs(10)); buffer_state.clear_expired(); }); let repo_state = AppState::load()?; app.manage(repo_state); Ok(()) } fn extend_api(&mut self, message: Invoke) { (self.invoke_handler)(message) } }