2021-12-14 07:27:59 +00:00
|
|
|
use dioxus_core::{ScopeId, ScopeState};
|
2021-10-11 19:35:20 +00:00
|
|
|
use std::{
|
|
|
|
cell::{Cell, Ref, RefCell, RefMut},
|
|
|
|
collections::HashSet,
|
2022-02-23 19:00:01 +00:00
|
|
|
rc::Rc,
|
|
|
|
sync::Arc,
|
2021-10-11 19:35:20 +00:00
|
|
|
};
|
|
|
|
|
2022-03-02 22:57:57 +00:00
|
|
|
type ProvidedState<T> = Rc<RefCell<ProvidedStateInner<T>>>;
|
2021-10-11 19:35:20 +00:00
|
|
|
|
|
|
|
// Tracks all the subscribers to a shared State
|
2021-12-10 02:19:31 +00:00
|
|
|
pub struct ProvidedStateInner<T> {
|
2021-10-11 19:35:20 +00:00
|
|
|
value: Rc<RefCell<T>>,
|
2022-02-23 18:47:17 +00:00
|
|
|
notify_any: Arc<dyn Fn(ScopeId)>,
|
2021-10-11 19:35:20 +00:00
|
|
|
consumers: HashSet<ScopeId>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> ProvidedStateInner<T> {
|
|
|
|
pub(crate) fn notify_consumers(&mut self) {
|
|
|
|
for consumer in self.consumers.iter() {
|
|
|
|
(self.notify_any)(*consumer);
|
|
|
|
}
|
|
|
|
}
|
2021-12-10 02:19:31 +00:00
|
|
|
|
|
|
|
pub fn write(&self) -> RefMut<T> {
|
|
|
|
self.value.borrow_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read(&self) -> Ref<T> {
|
|
|
|
self.value.borrow()
|
|
|
|
}
|
2021-10-11 19:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// This hook provides some relatively light ergonomics around shared state.
|
|
|
|
///
|
|
|
|
/// It is not a substitute for a proper state management system, but it is capable enough to provide use_state - type
|
2022-01-29 16:43:10 +00:00
|
|
|
/// ergonomics in a pinch, with zero cost.
|
2021-10-11 19:35:20 +00:00
|
|
|
///
|
|
|
|
/// # Example
|
|
|
|
///
|
|
|
|
/// ## Provider
|
|
|
|
///
|
2022-01-03 06:12:39 +00:00
|
|
|
/// ```rust, ignore
|
2021-10-11 19:35:20 +00:00
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// ## Consumer
|
|
|
|
///
|
2022-01-03 06:12:39 +00:00
|
|
|
/// ```rust, ignore
|
2021-10-11 19:35:20 +00:00
|
|
|
///
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// # How it works
|
|
|
|
///
|
|
|
|
/// Any time a component calls `write`, every consumer of the state will be notified - excluding the provider.
|
|
|
|
///
|
|
|
|
/// Right now, there is not a distinction between read-only and write-only, so every consumer will be notified.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
2022-06-30 20:35:33 +00:00
|
|
|
pub fn use_context<T: 'static>(cx: &ScopeState) -> Option<UseSharedState<T>> {
|
2022-07-11 19:50:56 +00:00
|
|
|
let state = cx.use_hook(|| {
|
2022-01-02 07:15:04 +00:00
|
|
|
let scope_id = cx.scope_id();
|
2022-12-03 00:24:49 +00:00
|
|
|
let root = cx.consume_context::<ProvidedState<T>>().cloned();
|
2022-01-02 07:15:04 +00:00
|
|
|
|
|
|
|
if let Some(root) = root.as_ref() {
|
|
|
|
root.borrow_mut().consumers.insert(scope_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
let value = root.as_ref().map(|f| f.borrow().value.clone());
|
|
|
|
SharedStateInner {
|
|
|
|
root,
|
|
|
|
value,
|
|
|
|
scope_id,
|
|
|
|
needs_notification: Cell::new(false),
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
state.needs_notification.set(false);
|
|
|
|
match (&state.value, &state.root) {
|
|
|
|
(Some(value), Some(root)) => Some(UseSharedState {
|
|
|
|
cx,
|
|
|
|
value,
|
|
|
|
root,
|
|
|
|
needs_notification: &state.needs_notification,
|
|
|
|
}),
|
|
|
|
_ => None,
|
|
|
|
}
|
2021-10-11 19:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct SharedStateInner<T: 'static> {
|
2022-03-02 22:57:57 +00:00
|
|
|
root: Option<ProvidedState<T>>,
|
2021-10-11 19:35:20 +00:00
|
|
|
value: Option<Rc<RefCell<T>>>,
|
|
|
|
scope_id: ScopeId,
|
|
|
|
needs_notification: Cell<bool>,
|
|
|
|
}
|
2021-11-07 03:11:17 +00:00
|
|
|
impl<T> Drop for SharedStateInner<T> {
|
|
|
|
fn drop(&mut self) {
|
|
|
|
// we need to unsubscribe when our component is unounted
|
|
|
|
if let Some(root) = &self.root {
|
|
|
|
let mut root = root.borrow_mut();
|
|
|
|
root.consumers.remove(&self.scope_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-10-11 19:35:20 +00:00
|
|
|
|
|
|
|
pub struct UseSharedState<'a, T: 'static> {
|
2021-12-14 07:27:59 +00:00
|
|
|
pub(crate) cx: &'a ScopeState,
|
2021-10-11 19:35:20 +00:00
|
|
|
pub(crate) value: &'a Rc<RefCell<T>>,
|
|
|
|
pub(crate) root: &'a Rc<RefCell<ProvidedStateInner<T>>>,
|
|
|
|
pub(crate) needs_notification: &'a Cell<bool>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a, T: 'static> UseSharedState<'a, T> {
|
|
|
|
pub fn read(&self) -> Ref<'_, T> {
|
|
|
|
self.value.borrow()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn notify_consumers(self) {
|
2021-12-10 02:19:31 +00:00
|
|
|
if !self.needs_notification.get() {
|
|
|
|
self.root.borrow_mut().notify_consumers();
|
|
|
|
self.needs_notification.set(true);
|
|
|
|
}
|
2021-10-11 19:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn read_write(&self) -> (Ref<'_, T>, &Self) {
|
|
|
|
(self.read(), self)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Calling "write" will force the component to re-render
|
|
|
|
///
|
|
|
|
///
|
|
|
|
/// TODO: We prevent unncessary notifications only in the hook, but we should figure out some more global lock
|
2021-11-03 19:13:50 +00:00
|
|
|
pub fn write(&self) -> RefMut<'_, T> {
|
2021-10-11 19:35:20 +00:00
|
|
|
self.cx.needs_update();
|
|
|
|
self.notify_consumers();
|
|
|
|
self.value.borrow_mut()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Allows the ability to write the value without forcing a re-render
|
|
|
|
pub fn write_silent(&self) -> RefMut<'_, T> {
|
|
|
|
self.value.borrow_mut()
|
|
|
|
}
|
2021-12-10 02:19:31 +00:00
|
|
|
|
|
|
|
pub fn inner(&self) -> Rc<RefCell<ProvidedStateInner<T>>> {
|
|
|
|
self.root.clone()
|
|
|
|
}
|
2021-10-11 19:35:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> Copy for UseSharedState<'_, T> {}
|
|
|
|
impl<'a, T> Clone for UseSharedState<'a, T>
|
|
|
|
where
|
|
|
|
T: 'static,
|
|
|
|
{
|
|
|
|
fn clone(&self) -> Self {
|
|
|
|
UseSharedState {
|
|
|
|
cx: self.cx,
|
|
|
|
value: self.value,
|
|
|
|
root: self.root,
|
|
|
|
needs_notification: self.needs_notification,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Provide some state for components down the hierarchy to consume without having to drill props.
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
|
|
|
///
|
2022-01-08 14:32:53 +00:00
|
|
|
pub fn use_context_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) {
|
2022-07-11 19:50:56 +00:00
|
|
|
cx.use_hook(|| {
|
2022-03-02 22:57:57 +00:00
|
|
|
let state: ProvidedState<T> = Rc::new(RefCell::new(ProvidedStateInner {
|
2022-01-02 07:15:04 +00:00
|
|
|
value: Rc::new(RefCell::new(f())),
|
|
|
|
notify_any: cx.schedule_update_any(),
|
|
|
|
consumers: HashSet::new(),
|
2022-03-02 22:57:57 +00:00
|
|
|
}));
|
2022-12-03 00:24:49 +00:00
|
|
|
|
|
|
|
cx.provide_context(state);
|
2022-01-02 07:15:04 +00:00
|
|
|
});
|
2021-10-11 19:35:20 +00:00
|
|
|
}
|