Revert "improved documentation and refactored naming to be more self-explanatory"

This reverts commit adfbb6c380.
This commit is contained in:
Pascal committed 2023-12-07 17:11:56 +01:00
1 parent adfbb6c380
commit 906c466e78
91 files changed
+1553 -1663

No files matched your search

+5 -6
View File
@@ -129,12 +129,11 @@ mod tests {
use futures_util::StreamExt;
fn app(cx: Scope, name: String) -> Element {
let task =
use_coroutine(cx, |mut rx: UnboundedReceiver<i32>| async move {
while let Some(msg) = rx.next().await {
println!("got message: {msg}");
}
});
let task = use_coroutine(cx, |mut rx: UnboundedReceiver<i32>| async move {
while let Some(msg) = rx.next().await {
println!("got message: {msg}");
}
});
let task2 = use_coroutine(cx, view_task);
+18 -25
View File
@@ -226,12 +226,10 @@ impl<T> UseSharedState<T> {
pub fn read(&self) -> Ref<'_, T> {
match self.try_read() {
Ok(value) => value,
Err(message) => {
panic!(
"Reading the shared state failed: {}\n({:?})",
message, message
)
}
Err(message) => panic!(
"Reading the shared state failed: {}\n({:?})",
message, message
),
}
}
@@ -260,12 +258,10 @@ impl<T> UseSharedState<T> {
pub fn write(&self) -> RefMut<'_, T> {
match self.try_write() {
Ok(value) => value,
Err(message) => {
panic!(
"Writing to shared state failed: {}\n({:?})",
message, message
)
}
Err(message) => panic!(
"Writing to shared state failed: {}\n({:?})",
message, message
),
}
}
@@ -288,12 +284,10 @@ impl<T> UseSharedState<T> {
pub fn write_silent(&self) -> RefMut<'_, T> {
match self.try_write_silent() {
Ok(value) => value,
Err(message) => {
panic!(
"Writing to shared state silently failed: {}\n({:?})",
message, message
)
}
Err(message) => panic!(
"Writing to shared state silently failed: {}\n({:?})",
message, message
),
}
}
@@ -369,13 +363,12 @@ impl<T> PartialEq for UseSharedState<T> {
/// ```
pub fn use_shared_state_provider<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) {
cx.use_hook(|| {
let state: ProvidedState<T> =
Rc::new(RefCell::new(ProvidedStateInner {
value: f(),
notify_any: cx.schedule_update_any(),
consumers: HashSet::new(),
gen: 0,
}));
let state: ProvidedState<T> = Rc::new(RefCell::new(ProvidedStateInner {
value: f(),
notify_any: cx.schedule_update_any(),
consumers: HashSet::new(),
gen: 0,
}));
cx.provide_context(state);
});
+24 -25
View File
@@ -35,37 +35,36 @@ pub fn use_state<T: 'static>(
cx: &ScopeState,
initial_state_fn: impl FnOnce() -> T,
) -> &UseState<T> {
let hook =
cx.use_hook(move || {
let current_val = Rc::new(initial_state_fn());
let update_callback = cx.schedule_update();
let slot = Rc::new(RefCell::new(current_val.clone()));
let setter = Rc::new({
to_owned![update_callback, slot];
move |new| {
{
let mut slot = slot.borrow_mut();
let hook = cx.use_hook(move || {
let current_val = Rc::new(initial_state_fn());
let update_callback = cx.schedule_update();
let slot = Rc::new(RefCell::new(current_val.clone()));
let setter = Rc::new({
to_owned![update_callback, slot];
move |new| {
{
let mut slot = slot.borrow_mut();
// if there's only one reference (weak or otherwise), we can just swap the values
// Typically happens when the state is set multiple times - we don't want to create a new Rc for each new value
if let Some(val) = Rc::get_mut(&mut slot) {
*val = new;
} else {
*slot = Rc::new(new);
}
// if there's only one reference (weak or otherwise), we can just swap the values
// Typically happens when the state is set multiple times - we don't want to create a new Rc for each new value
if let Some(val) = Rc::get_mut(&mut slot) {
*val = new;
} else {
*slot = Rc::new(new);
}
update_callback();
}
});
UseState {
current_val,
update_callback,
setter,
slot,
update_callback();
}
});
UseState {
current_val,
update_callback,
setter,
slot,
}
});
hook.current_val = hook.slot.borrow().clone();
hook