make signal sync
This commit is contained in:
1 parent
647815fa6f
commit
1126c1d329
7 files changed
+113
-68
No files matched your search
@@ -1,7 +1,6 @@
|
||||
use crate::rt::CopyValue;
|
||||
use crate::signal::{ReadOnlySignal, Signal, Write};
|
||||
|
||||
use std::cell::{Ref, RefMut};
|
||||
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
|
||||
|
||||
use std::{
|
||||
fmt::{Debug, Display},
|
||||
@@ -38,8 +37,8 @@ macro_rules! read_impls {
|
||||
|
||||
impl<T: 'static> $ty<Vec<T>> {
|
||||
/// Read a value from the inner vector.
|
||||
pub fn get(&self, index: usize) -> Option<Ref<'_, T>> {
|
||||
Ref::filter_map(self.read(), |v| v.get(index)).ok()
|
||||
pub fn get(&self, index: usize) -> Option<MappedRwLockReadGuard<'static, T>> {
|
||||
MappedRwLockReadGuard::try_map(self.read(), |v| v.get(index)).ok()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,8 +52,8 @@ macro_rules! read_impls {
|
||||
}
|
||||
|
||||
/// Attemps to read the inner value of the Option.
|
||||
pub fn as_ref(&self) -> Option<Ref<'_, T>> {
|
||||
Ref::filter_map(self.read(), |v| v.as_ref()).ok()
|
||||
pub fn as_ref(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
|
||||
MappedRwLockReadGuard::try_map(self.read(), |v| v.as_ref()).ok()
|
||||
}
|
||||
}
|
||||
};
|
||||
@@ -182,19 +181,22 @@ macro_rules! write_impls {
|
||||
}
|
||||
|
||||
/// Gets the value out of the Option, or inserts the given value if the Option is empty.
|
||||
pub fn get_or_insert(&self, default: T) -> Ref<'_, T> {
|
||||
pub fn get_or_insert(&self, default: T) -> MappedRwLockReadGuard<'_, T> {
|
||||
self.get_or_insert_with(|| default)
|
||||
}
|
||||
|
||||
/// Gets the value out of the Option, or inserts the value returned by the given function if the Option is empty.
|
||||
pub fn get_or_insert_with(&self, default: impl FnOnce() -> T) -> Ref<'_, T> {
|
||||
pub fn get_or_insert_with(
|
||||
&self,
|
||||
default: impl FnOnce() -> T,
|
||||
) -> MappedRwLockReadGuard<'_, T> {
|
||||
let borrow = self.read();
|
||||
if borrow.is_none() {
|
||||
drop(borrow);
|
||||
self.with_mut(|v| *v = Some(default()));
|
||||
Ref::map(self.read(), |v| v.as_ref().unwrap())
|
||||
MappedRwLockReadGuard::map(self.read(), |v| v.as_ref().unwrap())
|
||||
} else {
|
||||
Ref::map(borrow, |v| v.as_ref().unwrap())
|
||||
MappedRwLockReadGuard::map(borrow, |v| v.as_ref().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -238,15 +240,15 @@ impl<T: Clone + 'static> IntoIterator for CopyValue<Vec<T>> {
|
||||
|
||||
impl<T: 'static> CopyValue<Vec<T>> {
|
||||
/// Write to an element in the inner vector.
|
||||
pub fn get_mut(&self, index: usize) -> Option<RefMut<'_, T>> {
|
||||
RefMut::filter_map(self.write(), |v| v.get_mut(index)).ok()
|
||||
pub fn get_mut(&self, index: usize) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
||||
MappedRwLockWriteGuard::try_map(self.write(), |v| v.get_mut(index)).ok()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> CopyValue<Option<T>> {
|
||||
/// Deref the inner value mutably.
|
||||
pub fn as_mut(&self) -> Option<RefMut<'_, T>> {
|
||||
RefMut::filter_map(self.write(), |v| v.as_mut()).ok()
|
||||
pub fn as_mut(&self) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
||||
MappedRwLockWriteGuard::try_map(self.write(), |v| v.as_mut()).ok()
|
||||
}
|
||||
}
|
||||
|
||||
@@ -281,14 +283,14 @@ impl<T: Clone + 'static> IntoIterator for Signal<Vec<T>> {
|
||||
|
||||
impl<T: 'static> Signal<Vec<T>> {
|
||||
/// Returns a reference to an element or `None` if out of bounds.
|
||||
pub fn get_mut(&self, index: usize) -> Option<Write<'_, T, Vec<T>>> {
|
||||
pub fn get_mut(&self, index: usize) -> Option<Write<T, Vec<T>>> {
|
||||
Write::filter_map(self.write(), |v| v.get_mut(index))
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: 'static> Signal<Option<T>> {
|
||||
/// Returns a reference to an element or `None` if out of bounds.
|
||||
pub fn as_mut(&self) -> Option<Write<'_, T, Option<T>>> {
|
||||
pub fn as_mut(&self) -> Option<Write<T, Option<T>>> {
|
||||
Write::filter_map(self.write(), |v| v.as_mut())
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,3 @@
|
||||
use std::cell::{Ref, RefMut};
|
||||
|
||||
use std::mem::MaybeUninit;
|
||||
use std::ops::Deref;
|
||||
use std::rc::Rc;
|
||||
@@ -8,9 +6,12 @@ use dioxus_core::prelude::*;
|
||||
use dioxus_core::ScopeId;
|
||||
|
||||
use generational_box::{GenerationalBox, Owner, Store};
|
||||
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
|
||||
|
||||
use crate::Effect;
|
||||
|
||||
static STORE: once_cell::sync::Lazy<Store> = once_cell::sync::Lazy::new(Store::default);
|
||||
|
||||
fn current_store() -> Store {
|
||||
match consume_context() {
|
||||
Some(rt) => rt,
|
||||
@@ -117,22 +118,22 @@ impl<T: 'static> CopyValue<T> {
|
||||
}
|
||||
|
||||
/// Try to read the value. If the value has been dropped, this will return None.
|
||||
pub fn try_read(&self) -> Option<Ref<'_, T>> {
|
||||
pub fn try_read(&self) -> Option<MappedRwLockReadGuard<'static, T>> {
|
||||
self.value.try_read()
|
||||
}
|
||||
|
||||
/// Read the value. If the value has been dropped, this will panic.
|
||||
pub fn read(&self) -> Ref<'static, T> {
|
||||
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> {
|
||||
self.value.read()
|
||||
}
|
||||
|
||||
/// Try to write the value. If the value has been dropped, this will return None.
|
||||
pub fn try_write(&self) -> Option<RefMut<'static, T>> {
|
||||
pub fn try_write(&self) -> Option<MappedRwLockWriteGuard<'static, T>> {
|
||||
self.value.try_write()
|
||||
}
|
||||
|
||||
/// Write the value. If the value has been dropped, this will panic.
|
||||
pub fn write(&self) -> RefMut<'static, T> {
|
||||
pub fn write(&self) -> MappedRwLockWriteGuard<'static, T> {
|
||||
self.value.write()
|
||||
}
|
||||
|
||||
@@ -168,7 +169,7 @@ impl<T: 'static> PartialEq for CopyValue<T> {
|
||||
}
|
||||
|
||||
impl<T> Deref for CopyValue<T> {
|
||||
type Target = dyn Fn() -> Ref<'static, T>;
|
||||
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// https://github.com/dtolnay/case-studies/tree/master/callable-types
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use std::{
|
||||
cell::{Ref, RefCell, RefMut},
|
||||
cell::RefCell,
|
||||
mem::MaybeUninit,
|
||||
ops::{Deref, DerefMut},
|
||||
rc::Rc,
|
||||
@@ -10,6 +10,7 @@ use dioxus_core::{
|
||||
prelude::{current_scope_id, has_context, provide_context, schedule_update_any},
|
||||
ScopeId, ScopeState,
|
||||
};
|
||||
use parking_lot::{MappedRwLockReadGuard, MappedRwLockWriteGuard};
|
||||
|
||||
use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
|
||||
|
||||
@@ -173,7 +174,7 @@ impl<T: 'static> Signal<T> {
|
||||
|
||||
/// Get the current value of the signal. This will subscribe the current scope to the signal.
|
||||
/// If the signal has been dropped, this will panic.
|
||||
pub fn read(&self) -> Ref<T> {
|
||||
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> {
|
||||
let inner = self.inner.read();
|
||||
if let Some(effect) = inner.effect_stack.current() {
|
||||
let mut effect_subscribers = inner.effect_subscribers.borrow_mut();
|
||||
@@ -197,14 +198,14 @@ impl<T: 'static> Signal<T> {
|
||||
}
|
||||
}
|
||||
}
|
||||
Ref::map(inner, |v| &v.value)
|
||||
MappedRwLockReadGuard::map(inner, |v| &v.value)
|
||||
}
|
||||
|
||||
/// Get a mutable reference to the signal's value.
|
||||
/// If the signal has been dropped, this will panic.
|
||||
pub fn write(&self) -> Write<'_, T> {
|
||||
pub fn write(&self) -> Write<T> {
|
||||
let inner = self.inner.write();
|
||||
let borrow = RefMut::map(inner, |v| &mut v.value);
|
||||
let borrow = MappedRwLockWriteGuard::map(inner, |v| &mut v.value);
|
||||
Write {
|
||||
write: borrow,
|
||||
signal: SignalSubscriberDrop { signal: *self },
|
||||
@@ -281,7 +282,7 @@ impl<T: 'static> PartialEq for Signal<T> {
|
||||
}
|
||||
|
||||
impl<T> Deref for Signal<T> {
|
||||
type Target = dyn Fn() -> Ref<'static, T>;
|
||||
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// https://github.com/dtolnay/case-studies/tree/master/callable-types
|
||||
@@ -324,17 +325,17 @@ impl<T: 'static> Drop for SignalSubscriberDrop<T> {
|
||||
}
|
||||
|
||||
/// A mutable reference to a signal's value.
|
||||
pub struct Write<'a, T: 'static, I: 'static = T> {
|
||||
write: RefMut<'a, T>,
|
||||
pub struct Write<T: 'static, I: 'static = T> {
|
||||
write: MappedRwLockWriteGuard<'static, T>,
|
||||
signal: SignalSubscriberDrop<I>,
|
||||
}
|
||||
|
||||
impl<'a, T: 'static, I: 'static> Write<'a, T, I> {
|
||||
impl<T: 'static, I: 'static> Write<T, I> {
|
||||
/// Map the mutable reference to the signal's value to a new type.
|
||||
pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<'a, O, I> {
|
||||
pub fn map<O>(myself: Self, f: impl FnOnce(&mut T) -> &mut O) -> Write<O, I> {
|
||||
let Self { write, signal } = myself;
|
||||
Write {
|
||||
write: RefMut::map(write, f),
|
||||
write: MappedRwLockWriteGuard::map(write, f),
|
||||
signal,
|
||||
}
|
||||
}
|
||||
@@ -343,14 +344,14 @@ impl<'a, T: 'static, I: 'static> Write<'a, T, I> {
|
||||
pub fn filter_map<O>(
|
||||
myself: Self,
|
||||
f: impl FnOnce(&mut T) -> Option<&mut O>,
|
||||
) -> Option<Write<'a, O, I>> {
|
||||
) -> Option<Write<O, I>> {
|
||||
let Self { write, signal } = myself;
|
||||
let write = RefMut::filter_map(write, f).ok();
|
||||
let write = MappedRwLockWriteGuard::try_map(write, f).ok();
|
||||
write.map(|write| Write { write, signal })
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, T: 'static, I: 'static> Deref for Write<'a, T, I> {
|
||||
impl<T: 'static, I: 'static> Deref for Write<T, I> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
@@ -358,7 +359,7 @@ impl<'a, T: 'static, I: 'static> Deref for Write<'a, T, I> {
|
||||
}
|
||||
}
|
||||
|
||||
impl<T, I> DerefMut for Write<'_, T, I> {
|
||||
impl<T, I> DerefMut for Write<T, I> {
|
||||
fn deref_mut(&mut self) -> &mut Self::Target {
|
||||
&mut self.write
|
||||
}
|
||||
@@ -381,7 +382,7 @@ impl<T: 'static> ReadOnlySignal<T> {
|
||||
}
|
||||
|
||||
/// Get the current value of the signal. This will subscribe the current scope to the signal.
|
||||
pub fn read(&self) -> Ref<T> {
|
||||
pub fn read(&self) -> MappedRwLockReadGuard<'static, T> {
|
||||
self.inner.read()
|
||||
}
|
||||
|
||||
@@ -405,7 +406,7 @@ impl<T: 'static> PartialEq for ReadOnlySignal<T> {
|
||||
}
|
||||
|
||||
impl<T> Deref for ReadOnlySignal<T> {
|
||||
type Target = dyn Fn() -> Ref<'static, T>;
|
||||
type Target = dyn Fn() -> MappedRwLockReadGuard<'static, T>;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
// https://github.com/dtolnay/case-studies/tree/master/callable-types
|
||||
|
||||
Reference in new issue
Block a user