Implement effects using reactivecontext
This commit is contained in:
21 files changed
+287
-320
No files matched your search
@@ -1,24 +1,27 @@
|
||||
use crate::use_hook_did_run;
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_signals::{CopyValue, Effect, Writable};
|
||||
use dioxus_signals::{CopyValue, Effect, ReactiveContext, Writable};
|
||||
use futures_util::select;
|
||||
|
||||
/// 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.
|
||||
///
|
||||
/// If the use_effect call was skipped due to an early return, the effect will no longer activate.
|
||||
pub fn use_effect(mut callback: impl FnMut() + 'static) {
|
||||
let mut run_effect = use_hook(|| CopyValue::new(true));
|
||||
|
||||
use_hook_did_run(move |did_run| match did_run {
|
||||
true => run_effect.set(true),
|
||||
false => run_effect.set(false),
|
||||
});
|
||||
// let mut run_effect = use_hook(|| CopyValue::new(true));
|
||||
// use_hook_did_run(move |did_run| run_effect.set(did_run));
|
||||
|
||||
use_hook(|| {
|
||||
Effect::new(move || {
|
||||
if run_effect() {
|
||||
callback();
|
||||
spawn(async move {
|
||||
let rc = ReactiveContext::new(None);
|
||||
|
||||
loop {
|
||||
// Run the effect
|
||||
rc.run_in(|| callback());
|
||||
|
||||
// Wait for context to change
|
||||
rc.changed().await;
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
}
|
||||
@@ -21,11 +21,6 @@ where
|
||||
let mut callback = use_callback(move || {
|
||||
let fut = future();
|
||||
spawn(async move {
|
||||
// todo: not sure if we should flush_sync
|
||||
// It's fine for most cases but means that the future will always be started in the next frame
|
||||
// The point here is to not run use_future on the server... which like, shouldn't we?
|
||||
flush_sync().await;
|
||||
|
||||
state.set(UseFutureState::Pending);
|
||||
fut.await;
|
||||
state.set(UseFutureState::Complete);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use crate::dependency::Dependency;
|
||||
use crate::use_signal;
|
||||
use dioxus_core::prelude::*;
|
||||
use dioxus_signals::{ReadOnlySignal, Readable, Signal, SignalData};
|
||||
use dioxus_signals::{ReactiveContext, ReadOnlySignal, Readable, Signal, SignalData};
|
||||
use dioxus_signals::{Storage, Writable};
|
||||
|
||||
/// Creates a new unsync Selector. The selector will be run immediately and whenever any signal it reads changes.
|
||||
@@ -45,9 +45,33 @@ pub fn use_memo<R: PartialEq>(f: impl FnMut() -> R + 'static) -> ReadOnlySignal<
|
||||
/// ```
|
||||
#[track_caller]
|
||||
pub fn use_maybe_sync_memo<R: PartialEq, S: Storage<SignalData<R>>>(
|
||||
f: impl FnMut() -> R + 'static,
|
||||
mut f: impl FnMut() -> R + 'static,
|
||||
) -> ReadOnlySignal<R, S> {
|
||||
use_hook(|| Signal::maybe_sync_memo(f))
|
||||
use_hook(|| {
|
||||
// Get the current reactive context
|
||||
let rc = ReactiveContext::new(None);
|
||||
|
||||
// Create a new signal in that context, wiring up its dependencies and subscribers
|
||||
let mut state: Signal<R, S> = rc.run_in(|| Signal::new_maybe_sync(f()));
|
||||
|
||||
spawn(async move {
|
||||
loop {
|
||||
rc.changed().await;
|
||||
|
||||
println!("change triggered in memo");
|
||||
|
||||
let new = rc.run_in(|| f());
|
||||
|
||||
if new != *state.peek() {
|
||||
println!("change sett in memo");
|
||||
*state.write() = new;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
// And just return the readonly variant of that signal
|
||||
ReadOnlySignal::new_maybe_sync(state)
|
||||
})
|
||||
}
|
||||
|
||||
/// Creates a new unsync Selector with some local dependencies. The selector will be run immediately and whenever any signal it reads or any dependencies it tracks changes
|
||||
|
||||
@@ -27,20 +27,20 @@ where
|
||||
|
||||
// Spawn a wrapper task that polls the innner future and watch its dependencies
|
||||
let task = spawn(async move {
|
||||
// move the future here and pin it so we can poll it
|
||||
let fut = fut;
|
||||
pin_mut!(fut);
|
||||
// // move the future here and pin it so we can poll it
|
||||
// let fut = fut;
|
||||
// pin_mut!(fut);
|
||||
|
||||
let res = future::poll_fn(|cx| {
|
||||
// Set the effect stack properly
|
||||
// add any dependencies to the effect stack that we need to watch when restarting the future
|
||||
// Poll the inner future
|
||||
fut.poll_unpin(cx)
|
||||
})
|
||||
.await;
|
||||
// let res = future::poll_fn(|cx| {
|
||||
// // Set the effect stack properly
|
||||
// // add any dependencies to the effect stack that we need to watch when restarting the future
|
||||
// // Poll the inner future
|
||||
// fut.poll_unpin(cx)
|
||||
// })
|
||||
// .await;
|
||||
|
||||
// Set the value
|
||||
value.set(Some(Signal::new(res)));
|
||||
// // Set the value
|
||||
// value.set(Some(Signal::new(res)));
|
||||
});
|
||||
|
||||
Some(task)
|
||||
|
||||
@@ -96,7 +96,7 @@ fn use_maybe_signal_sync<T: 'static, U: Storage<SignalData<T>>>(
|
||||
|
||||
// By default, we want to unsubscribe the current component from the signal on every render
|
||||
// any calls to .read() in the body will re-subscribe the component to the signal
|
||||
use_before_render(move || signal.unsubscribe(current_scope_id().unwrap()));
|
||||
// use_before_render(move || signal.unsubscribe(current_scope_id().unwrap()));
|
||||
|
||||
signal
|
||||
}
|
||||
Reference in new issue
Block a user