Stateful closing and io input
This commit is contained in:
+11
-4
@@ -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()
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user