From: Olaf Wintermann Date: Thu, 14 May 2026 18:19:50 +0000 (+0200) Subject: add backend thread X-Git-Url: https://uap-core.de/gitweb/?a=commitdiff_plain;h=20a138dbc5c632a87071950dd6f12246a3bd8fff;p=note.git add backend thread --- diff --git a/application/src/backend.rs b/application/src/backend.rs index 19c3d61..a1fc41c 100644 --- a/application/src/backend.rs +++ b/application/src/backend.rs @@ -1,6 +1,10 @@ +use std::future::Future; +use std::pin::Pin; use sea_orm::{ActiveModelTrait, Database, DatabaseConnection, DbErr, EntityTrait, QueryFilter, ColumnTrait, Set}; use tokio::runtime::Runtime; use std::sync::{Arc}; +use tokio::sync::mpsc; +use tokio::sync::mpsc::UnboundedSender; use migration::{Migrator, MigratorTrait}; use ui_rs::ui; @@ -14,6 +18,19 @@ pub struct Backend { pub current_profile: Option, } +type CmdFuture = Pin + Send>>; +pub trait Cmd: Send { + fn run(self: Box, backend: Arc) -> CmdFuture; +} + +pub type DynCmd = Box; + +pub struct BackendHandle { + tx: UnboundedSender, +} + + + impl Backend { pub fn new() -> Result { let rt = Arc::new( @@ -97,4 +114,26 @@ impl Backend { Ok(profile) } + + pub fn start(self) -> BackendHandle { + let backend = Arc::new(self); + + let (tx, mut rx) = mpsc::unbounded_channel::(); + + let rt = backend.rt.clone(); + + std::thread::spawn(move || { + rt.block_on(async move { + while let Some(cmd) = rx.recv().await { + let backend = backend.clone(); + + tokio::spawn(async move { + cmd.run(backend).await; + }); + } + }); + }); + + BackendHandle { tx } + } } diff --git a/application/src/main.rs b/application/src/main.rs index c055206..fe0c235 100644 --- a/application/src/main.rs +++ b/application/src/main.rs @@ -30,9 +30,10 @@ mod window; mod backend; use std::env; +use tokio::sync::mpsc; use ui_rs::{ui}; use ui_rs::ui::*; -use crate::backend::Backend; +use crate::backend::{Backend, BackendHandle, DynCmd}; use crate::window::*; fn main() { @@ -46,7 +47,9 @@ fn main() { } }; - let mut app = App { backend: backend }; + let handle = backend.start(); + + let mut app = App { backend: handle }; ui::app_run::(&mut app); } @@ -97,7 +100,7 @@ fn init_backend() -> Result { } struct App { - backend: Backend, + backend: BackendHandle, }