add as_mapped_ref
This commit is contained in:
1 parent
e45739ac04
commit
b9d179c494
6 files changed
+68
-20
No files matched your search
@@ -23,6 +23,6 @@ fn App(cx: Scope) -> Element {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[component]
|
#[component]
|
||||||
fn Child(cx: Scope, signal: SignalMap<String>) -> Element {
|
fn Child(cx: Scope, signal: MappedSignal<String>) -> Element {
|
||||||
render! {"{signal:?}"}
|
render! {"{signal:?}"}
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,39 @@
|
|||||||
|
use dioxus::prelude::*;
|
||||||
|
use dioxus_signals::*;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
dioxus_desktop::launch(App);
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn App(cx: Scope) -> Element {
|
||||||
|
let signal = use_signal(cx, || Some(String::from("Hello")));
|
||||||
|
|
||||||
|
render! {
|
||||||
|
button {
|
||||||
|
onclick: move |_| {
|
||||||
|
let new_value = (!signal.read().is_some()).then(|| String::from("Hello"));
|
||||||
|
signal.set(new_value);
|
||||||
|
},
|
||||||
|
"Swap"
|
||||||
|
}
|
||||||
|
button {
|
||||||
|
onclick: move |_| {
|
||||||
|
if let Some(value) = &mut *signal.write() {
|
||||||
|
value.push_str(" World");
|
||||||
|
}
|
||||||
|
},
|
||||||
|
"Change"
|
||||||
|
}
|
||||||
|
if let Some(item) = signal.as_mapped_ref(){
|
||||||
|
render! {
|
||||||
|
Child { signal: item }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[component]
|
||||||
|
fn Child(cx: Scope, signal: MappedSignal<String>) -> Element {
|
||||||
|
render! {"{signal:?}"}
|
||||||
|
}
|
||||||
@@ -1,6 +1,6 @@
|
|||||||
use crate::rt::CopyValue;
|
use crate::rt::CopyValue;
|
||||||
use crate::signal::{ReadOnlySignal, Signal, Write};
|
use crate::signal::{ReadOnlySignal, Signal, Write};
|
||||||
use crate::SignalMap;
|
use crate::MappedSignal;
|
||||||
use generational_box::GenerationalRef;
|
use generational_box::GenerationalRef;
|
||||||
use generational_box::GenerationalRefMut;
|
use generational_box::GenerationalRefMut;
|
||||||
|
|
||||||
@@ -280,7 +280,7 @@ impl<T: 'static> IntoIterator for Signal<Vec<T>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An iterator over items in a `Signal<Vec<T>>` that yields [`SignalMap`]s.
|
/// An iterator over items in a `Signal<Vec<T>>` that yields [`MappedSignal`]s.
|
||||||
pub struct MappedSignalIterator<T: 'static> {
|
pub struct MappedSignalIterator<T: 'static> {
|
||||||
index: usize,
|
index: usize,
|
||||||
length: usize,
|
length: usize,
|
||||||
@@ -288,7 +288,7 @@ pub struct MappedSignalIterator<T: 'static> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<T> Iterator for MappedSignalIterator<T> {
|
impl<T> Iterator for MappedSignalIterator<T> {
|
||||||
type Item = SignalMap<T>;
|
type Item = MappedSignal<T>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
let index = self.index;
|
let index = self.index;
|
||||||
@@ -303,7 +303,7 @@ impl<T: 'static> Signal<Vec<T>> {
|
|||||||
Write::filter_map(self.write(), |v| v.get_mut(index))
|
Write::filter_map(self.write(), |v| v.get_mut(index))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create an iterator of [`SignalMap`]s over the inner vector.
|
/// Create an iterator of [`MappedSignal`]s over the inner vector.
|
||||||
pub fn iter_signals(&self) -> MappedSignalIterator<T> {
|
pub fn iter_signals(&self) -> MappedSignalIterator<T> {
|
||||||
MappedSignalIterator {
|
MappedSignalIterator {
|
||||||
index: 0,
|
index: 0,
|
||||||
@@ -318,4 +318,13 @@ impl<T: 'static> Signal<Option<T>> {
|
|||||||
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())
|
Write::filter_map(self.write(), |v| v.as_mut())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Try to create a [`MappedSignal`] over the inner value.
|
||||||
|
pub fn as_mapped_ref(&self) -> Option<MappedSignal<T>> {
|
||||||
|
if self.read().is_some() {
|
||||||
|
Some(self.map(|v| v.as_ref().unwrap()))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
+11
-11
@@ -6,12 +6,12 @@ use std::fmt::Debug;
|
|||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
|
|
||||||
/// A read only signal that has been mapped to a new type.
|
/// A read only signal that has been mapped to a new type.
|
||||||
pub struct SignalMap<U: 'static + ?Sized> {
|
pub struct MappedSignal<U: 'static + ?Sized> {
|
||||||
origin_scope: ScopeId,
|
origin_scope: ScopeId,
|
||||||
mapping: CopyValue<Box<dyn Fn() -> GenerationalRef<U>>>,
|
mapping: CopyValue<Box<dyn Fn() -> GenerationalRef<U>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U: ?Sized> SignalMap<U> {
|
impl<U: ?Sized> MappedSignal<U> {
|
||||||
/// Create a new mapped signal.
|
/// Create a new mapped signal.
|
||||||
pub fn new<T: 'static>(signal: Signal<T>, mapping: impl Fn(&T) -> &U + 'static) -> Self {
|
pub fn new<T: 'static>(signal: Signal<T>, mapping: impl Fn(&T) -> &U + 'static) -> Self {
|
||||||
Self {
|
Self {
|
||||||
@@ -38,47 +38,47 @@ impl<U: ?Sized> SignalMap<U> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U: ?Sized + Clone> SignalMap<U> {
|
impl<U: ?Sized + Clone> MappedSignal<U> {
|
||||||
/// Get the current value of the signal. This will subscribe the current scope to the signal.
|
/// Get the current value of the signal. This will subscribe the current scope to the signal.
|
||||||
pub fn value(&self) -> U {
|
pub fn value(&self) -> U {
|
||||||
self.read().clone()
|
self.read().clone()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U: ?Sized> PartialEq for SignalMap<U> {
|
impl<U: ?Sized> PartialEq for MappedSignal<U> {
|
||||||
fn eq(&self, other: &Self) -> bool {
|
fn eq(&self, other: &Self) -> bool {
|
||||||
self.mapping == other.mapping
|
self.mapping == other.mapping
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U> std::clone::Clone for SignalMap<U> {
|
impl<U> std::clone::Clone for MappedSignal<U> {
|
||||||
fn clone(&self) -> Self {
|
fn clone(&self) -> Self {
|
||||||
*self
|
*self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U> Copy for SignalMap<U> {}
|
impl<U> Copy for MappedSignal<U> {}
|
||||||
|
|
||||||
impl<U: ?Sized + Display> Display for SignalMap<U> {
|
impl<U: ?Sized + Display> Display for MappedSignal<U> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.with(|v| Display::fmt(v, f))
|
self.with(|v| Display::fmt(v, f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U: ?Sized + Debug> Debug for SignalMap<U> {
|
impl<U: ?Sized + Debug> Debug for MappedSignal<U> {
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
self.with(|v| Debug::fmt(v, f))
|
self.with(|v| Debug::fmt(v, f))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U> SignalMap<Vec<U>> {
|
impl<U> MappedSignal<Vec<U>> {
|
||||||
/// Read a value from the inner vector.
|
/// Read a value from the inner vector.
|
||||||
pub fn get(&self, index: usize) -> Option<GenerationalRef<U>> {
|
pub fn get(&self, index: usize) -> Option<GenerationalRef<U>> {
|
||||||
GenerationalRef::filter_map(self.read(), |v| v.get(index))
|
GenerationalRef::filter_map(self.read(), |v| v.get(index))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<U: Clone + 'static> SignalMap<Option<U>> {
|
impl<U: Clone + 'static> MappedSignal<Option<U>> {
|
||||||
/// Unwraps the inner value and clones it.
|
/// Unwraps the inner value and clones it.
|
||||||
pub fn unwrap(&self) -> U
|
pub fn unwrap(&self) -> U
|
||||||
where
|
where
|
||||||
@@ -93,7 +93,7 @@ impl<U: Clone + 'static> SignalMap<Option<U>> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> std::ops::Deref for SignalMap<T> {
|
impl<T> std::ops::Deref for MappedSignal<T> {
|
||||||
type Target = dyn Fn() -> GenerationalRef<T>;
|
type Target = dyn Fn() -> GenerationalRef<T>;
|
||||||
|
|
||||||
fn deref(&self) -> &Self::Target {
|
fn deref(&self) -> &Self::Target {
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
use crate::SignalMap;
|
use crate::MappedSignal;
|
||||||
use std::{
|
use std::{
|
||||||
cell::RefCell,
|
cell::RefCell,
|
||||||
mem::MaybeUninit,
|
mem::MaybeUninit,
|
||||||
@@ -307,8 +307,8 @@ impl<T: 'static> Signal<T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/// Map the signal to a new type.
|
/// Map the signal to a new type.
|
||||||
pub fn map<O>(self, f: impl Fn(&T) -> &O + 'static) -> SignalMap<O> {
|
pub fn map<O>(self, f: impl Fn(&T) -> &O + 'static) -> MappedSignal<O> {
|
||||||
SignalMap::new(self, f)
|
MappedSignal::new(self, f)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ fn create_signals_global() {
|
|||||||
|
|
||||||
fn Child(cx: Scope) -> Element {
|
fn Child(cx: Scope) -> Element {
|
||||||
let signal = create_without_cx();
|
let signal = create_without_cx();
|
||||||
let mapped = SignalMap::new(signal, |v| v.as_bytes());
|
let mapped = MappedSignal::new(signal, |v| v.as_bytes());
|
||||||
|
|
||||||
render! {
|
render! {
|
||||||
"{signal:?}"
|
"{signal:?}"
|
||||||
|
|||||||
Reference in new issue
Block a user