Stateful closing and io input

This commit is contained in:
2026-07-14 21:42:29 +02:00
parent 641805e1b9
commit 809e5d1529
3 changed files with 56 additions and 22 deletions
+11 -4
View File
@@ -3,25 +3,26 @@ use std::{any::Any, sync::Arc};
use tokio::sync::{Mutex, MutexGuard, mpsc::Sender};
pub struct Refresh;
pub struct Close;
#[derive(Debug, Clone)]
pub struct Context {
pub(crate) tx: Sender<Box<dyn Any>>,
pub(crate) tx: Sender<Box<dyn Any + Send + Sync>>,
}
pub struct State<T> {
pub value: Arc<Mutex<T>>,
tx: Sender<Box<dyn Any>>,
tx: Sender<Box<dyn Any + Send + Sync>>,
}
pub struct StateGuard<'a, T> {
is_mutated: bool,
value: MutexGuard<'a, T>,
tx: &'a Sender<Box<dyn Any>>,
tx: &'a Sender<Box<dyn Any + Send + Sync>>,
}
impl Context {
pub fn use_state<T>(&self, v: T) -> State<T> {
pub fn use_state<T: Send + Sync>(&self, v: T) -> State<T> {
State {
value: Arc::new(Mutex::new(v)),
tx: self.tx.clone(),
@@ -62,3 +63,9 @@ impl<'a, T> Drop for StateGuard<'a, T> {
}
}
}
impl Context {
pub async fn close(&self) {
self.tx.send(Box::new(Close)).await.unwrap()
}
}