wip: reactive context instead of effect
This commit is contained in:
9 files changed
+177
-115
No files matched your search
@@ -252,34 +252,10 @@ pub fn after_render(f: impl FnMut() + 'static) {
|
||||
/// Effects rely on this to ensure that they only run effects after the DOM has been updated. Without flush_sync effects
|
||||
/// are run immediately before diffing the DOM, which causes all sorts of out-of-sync weirdness.
|
||||
pub async fn flush_sync() {
|
||||
let mut polled = false;
|
||||
|
||||
let _task =
|
||||
FlushKey(Runtime::with(|rt| rt.add_to_flush_table()).expect("to be in a dioxus runtime"));
|
||||
|
||||
// Poll without giving the waker to anyone
|
||||
// The runtime will manually wake this task up when it's ready
|
||||
poll_fn(|_| {
|
||||
if !polled {
|
||||
polled = true;
|
||||
futures_util::task::Poll::Pending
|
||||
} else {
|
||||
futures_util::task::Poll::Ready(())
|
||||
}
|
||||
})
|
||||
.await;
|
||||
|
||||
// If the the future got polled, then we don't need to prevent it from being dropped
|
||||
// If we had generational indicies on tasks we could simply let the task remain in the queue and just be a no-op
|
||||
// when it's run
|
||||
std::mem::forget(_task);
|
||||
|
||||
struct FlushKey(Task);
|
||||
impl Drop for FlushKey {
|
||||
fn drop(&mut self) {
|
||||
Runtime::with(|rt| rt.flush_table.borrow_mut().remove(&self.0));
|
||||
}
|
||||
}
|
||||
_ = Runtime::with(|rt| rt.flush.clone())
|
||||
.unwrap()
|
||||
.recv_async()
|
||||
.await;
|
||||
}
|
||||
|
||||
/// Use a hook with a cleanup function
|
||||
|
||||
@@ -33,19 +33,22 @@ pub struct Runtime {
|
||||
pub(crate) sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
||||
|
||||
// Tasks waiting to be manually resumed when we call wait_for_work
|
||||
pub(crate) flush_table: RefCell<FxHashSet<Task>>,
|
||||
pub(crate) flush: flume::Receiver<()>,
|
||||
}
|
||||
|
||||
impl Runtime {
|
||||
pub(crate) fn new(sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>) -> Rc<Self> {
|
||||
pub(crate) fn new(
|
||||
sender: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
||||
flush: flume::Receiver<()>,
|
||||
) -> Rc<Self> {
|
||||
Rc::new(Self {
|
||||
sender,
|
||||
flush,
|
||||
rendering: Cell::new(true),
|
||||
scope_states: Default::default(),
|
||||
scope_stack: Default::default(),
|
||||
current_task: Default::default(),
|
||||
tasks: Default::default(),
|
||||
flush_table: Default::default(),
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -122,13 +122,6 @@ impl Runtime {
|
||||
self.tasks.borrow().get(task.0)?.parent
|
||||
}
|
||||
|
||||
/// Add this task to the queue of tasks that will manually get poked when the scheduler is flushed
|
||||
pub(crate) fn add_to_flush_table(&self) -> Task {
|
||||
let value = self.current_task().unwrap();
|
||||
self.flush_table.borrow_mut().insert(value);
|
||||
value
|
||||
}
|
||||
|
||||
pub(crate) fn handle_task_wakeup(&self, id: Task) {
|
||||
debug_assert!(Runtime::current().is_some(), "Must be in a dioxus runtime");
|
||||
|
||||
|
||||
@@ -202,6 +202,7 @@ pub struct VirtualDom {
|
||||
pub(crate) suspended_scopes: FxHashSet<ScopeId>,
|
||||
|
||||
rx: futures_channel::mpsc::UnboundedReceiver<SchedulerMsg>,
|
||||
flush_tx: flume::Sender<()>,
|
||||
}
|
||||
|
||||
impl VirtualDom {
|
||||
@@ -305,10 +306,12 @@ impl VirtualDom {
|
||||
/// ```
|
||||
pub(crate) fn new_with_component(root: impl AnyProps + 'static) -> Self {
|
||||
let (tx, rx) = futures_channel::mpsc::unbounded();
|
||||
let (flush_tx, flush_rx) = flume::unbounded(); // I don't think this needs to be unbounded
|
||||
|
||||
let mut dom = Self {
|
||||
rx,
|
||||
runtime: Runtime::new(tx),
|
||||
flush_tx,
|
||||
runtime: Runtime::new(tx, flush_rx),
|
||||
scopes: Default::default(),
|
||||
dirty_scopes: Default::default(),
|
||||
templates: Default::default(),
|
||||
@@ -419,8 +422,8 @@ impl VirtualDom {
|
||||
/// let dom = VirtualDom::new(app);
|
||||
/// ```
|
||||
pub async fn wait_for_work(&mut self) {
|
||||
// Ping tasks waiting on the flush table - they're waiting for sync stuff to be done before progressing
|
||||
self.clear_flush_table();
|
||||
// Send the flush signal to the runtime
|
||||
_ = self.flush_tx.try_send(());
|
||||
|
||||
// And then poll the futures
|
||||
self.poll_tasks().await;
|
||||
@@ -448,17 +451,6 @@ impl VirtualDom {
|
||||
}
|
||||
}
|
||||
|
||||
fn clear_flush_table(&mut self) {
|
||||
// Make sure we set the runtime since we're running user code
|
||||
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
||||
|
||||
// Manually flush tasks that called `flush().await`
|
||||
// Tasks that might've been waiting for `flush` finally have a chance to run to their next await point
|
||||
for task in self.runtime.flush_table.take() {
|
||||
self.runtime.handle_task_wakeup(task);
|
||||
}
|
||||
}
|
||||
|
||||
/// Process all events in the queue until there are no more left
|
||||
pub fn process_events(&mut self) {
|
||||
let _runtime = RuntimeGuard::new(self.runtime.clone());
|
||||
|
||||
Reference in new issue
Block a user