mirror of
https://github.com/DioxusLabs/dioxus
synced 2025-01-05 01:08:42 +00:00
36 lines
1 KiB
Rust
36 lines
1 KiB
Rust
|
use std::sync::{Arc, PoisonError, RwLock, RwLockWriteGuard};
|
||
|
|
||
|
use anymap::{any::Any, Map};
|
||
|
|
||
|
type SendSyncAnyMap = Map<dyn Any + Send + Sync + 'static>;
|
||
|
|
||
|
/// A shared context for server functions. This allows you to pass data between your server and the server functions like authentication session data.
|
||
|
#[derive(Clone)]
|
||
|
pub struct DioxusServerContext {
|
||
|
shared_context: Arc<RwLock<SendSyncAnyMap>>,
|
||
|
}
|
||
|
|
||
|
impl Default for DioxusServerContext {
|
||
|
fn default() -> Self {
|
||
|
Self {
|
||
|
shared_context: Arc::new(RwLock::new(SendSyncAnyMap::new())),
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
impl DioxusServerContext {
|
||
|
pub fn get<T: Any + Send + Sync + Clone + 'static>(&self) -> Option<T> {
|
||
|
self.shared_context.read().ok()?.get::<T>().cloned()
|
||
|
}
|
||
|
|
||
|
pub fn insert<T: Any + Send + Sync + 'static>(
|
||
|
&mut self,
|
||
|
value: T,
|
||
|
) -> Result<(), PoisonError<RwLockWriteGuard<'_, SendSyncAnyMap>>> {
|
||
|
self.shared_context
|
||
|
.write()
|
||
|
.map(|mut map| map.insert(value))
|
||
|
.map(|_| ())
|
||
|
}
|
||
|
}
|