hello world example working
This commit is contained in:
1 parent
3865f44081
commit
4013341b6c
22 files changed
+159
-176
No files matched your search
@@ -38,21 +38,20 @@ pub(crate) fn get_effect_stack() -> EffectStack {
|
||||
|
||||
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
|
||||
/// The signal will be owned by the current component and will be dropped when the component is dropped.
|
||||
pub fn use_effect(cx: &ScopeState, callback: impl FnMut() + 'static) {
|
||||
cx.use_hook(|| Effect::new(callback));
|
||||
pub fn use_effect(callback: impl FnMut() + 'static) {
|
||||
once(|| Effect::new(callback));
|
||||
}
|
||||
|
||||
/// Create a new effect. The effect will be run immediately and whenever any signal it reads changes.
|
||||
/// The signal will be owned by the current component and will be dropped when the component is dropped.
|
||||
pub fn use_effect_with_dependencies<D: Dependency>(
|
||||
cx: &ScopeState,
|
||||
dependencies: D,
|
||||
mut callback: impl FnMut(D::Out) + 'static,
|
||||
) where
|
||||
D::Out: 'static,
|
||||
{
|
||||
let dependencies_signal = use_signal(cx, || dependencies.out());
|
||||
cx.use_hook(|| {
|
||||
let dependencies_signal = use_signal(|| dependencies.out());
|
||||
once(|| {
|
||||
Effect::new(move || {
|
||||
let deref = &*dependencies_signal.read();
|
||||
callback(deref.clone());
|
||||
|
||||
@@ -44,7 +44,7 @@ fn owner_in_scope(scope: ScopeId) -> Rc<Owner> {
|
||||
Some(rt) => rt,
|
||||
None => {
|
||||
let owner = Rc::new(current_store().owner());
|
||||
provide_context_to_scope(scope, owner).expect("in a virtual dom")
|
||||
scope.provide_context(owner).expect("in a virtual dom")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -22,11 +22,8 @@ use crate::{get_effect_stack, signal::SignalData, CopyValue, Effect, ReadOnlySig
|
||||
/// }
|
||||
/// ```
|
||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||
pub fn use_selector<R: PartialEq>(
|
||||
cx: &ScopeState,
|
||||
f: impl FnMut() -> R + 'static,
|
||||
) -> ReadOnlySignal<R> {
|
||||
*cx.use_hook(|| selector(f))
|
||||
pub fn use_selector<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<R> {
|
||||
once(|| selector(f))
|
||||
}
|
||||
|
||||
/// Creates a new Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
|
||||
@@ -47,15 +44,14 @@ pub fn use_selector<R: PartialEq>(
|
||||
/// ```
|
||||
#[must_use = "Consider using `use_effect` to rerun a callback when dependencies change"]
|
||||
pub fn use_selector_with_dependencies<R: PartialEq, D: Dependency>(
|
||||
cx: &ScopeState,
|
||||
dependencies: D,
|
||||
mut f: impl FnMut(D::Out) -> R + 'static,
|
||||
) -> ReadOnlySignal<R>
|
||||
where
|
||||
D::Out: 'static,
|
||||
{
|
||||
let dependencies_signal = use_signal(cx, || dependencies.out());
|
||||
let selector = *cx.use_hook(|| {
|
||||
let dependencies_signal = use_signal(|| dependencies.out());
|
||||
let selector = once(|| {
|
||||
selector(move || {
|
||||
let deref = &*dependencies_signal.read();
|
||||
f(deref.clone())
|
||||
@@ -87,9 +83,9 @@ pub fn selector<R: PartialEq>(mut f: impl FnMut() -> R + 'static) -> ReadOnlySig
|
||||
state.inner.value.set(SignalData {
|
||||
subscribers: Default::default(),
|
||||
effect_subscribers: Default::default(),
|
||||
update_any: schedule_update_any().expect("in a virtual dom"),
|
||||
value: f(),
|
||||
effect_stack: get_effect_stack(),
|
||||
update_any: schedule_update_any(),
|
||||
});
|
||||
{
|
||||
get_effect_stack().effects.write().pop();
|
||||
|
||||
@@ -7,8 +7,9 @@ use std::{
|
||||
};
|
||||
|
||||
use dioxus_core::{
|
||||
once,
|
||||
prelude::{current_scope_id, has_context, provide_context, schedule_update_any},
|
||||
ScopeId, ScopeState,
|
||||
ScopeId,
|
||||
};
|
||||
use generational_box::{GenerationalRef, GenerationalRefMut};
|
||||
|
||||
@@ -47,11 +48,11 @@ use crate::{get_effect_stack, CopyValue, Effect, EffectStack};
|
||||
/// ```
|
||||
#[track_caller]
|
||||
#[must_use]
|
||||
pub fn use_signal<T: 'static>(cx: &ScopeState, f: impl FnOnce() -> T) -> Signal<T> {
|
||||
pub fn use_signal<T: 'static>(f: impl FnOnce() -> T) -> Signal<T> {
|
||||
#[cfg(debug_assertions)]
|
||||
let caller = std::panic::Location::caller();
|
||||
|
||||
*cx.use_hook(|| {
|
||||
once(|| {
|
||||
Signal::new_with_caller(
|
||||
f(),
|
||||
#[cfg(debug_assertions)]
|
||||
@@ -155,7 +156,7 @@ impl<T: 'static> Signal<T> {
|
||||
inner: CopyValue::new(SignalData {
|
||||
subscribers: Default::default(),
|
||||
effect_subscribers: Default::default(),
|
||||
update_any: schedule_update_any().expect("in a virtual dom"),
|
||||
update_any: schedule_update_any(),
|
||||
value,
|
||||
effect_stack: get_effect_stack(),
|
||||
}),
|
||||
@@ -172,7 +173,7 @@ impl<T: 'static> Signal<T> {
|
||||
SignalData {
|
||||
subscribers: Default::default(),
|
||||
effect_subscribers: Default::default(),
|
||||
update_any: schedule_update_any().expect("in a virtual dom"),
|
||||
update_any: schedule_update_any(),
|
||||
value,
|
||||
effect_stack: get_effect_stack(),
|
||||
},
|
||||
@@ -189,7 +190,7 @@ impl<T: 'static> Signal<T> {
|
||||
SignalData {
|
||||
subscribers: Default::default(),
|
||||
effect_subscribers: Default::default(),
|
||||
update_any: schedule_update_any().expect("in a virtual dom"),
|
||||
update_any: schedule_update_any(),
|
||||
value,
|
||||
effect_stack: get_effect_stack(),
|
||||
},
|
||||
|
||||
Reference in new issue
Block a user