server_future uses use_resource

This commit is contained in:
Jonathan Kelley committed 2024-02-02 14:08:21 -08:00
1 parent 0fd7799bc2
commit f44b72f5e1
4 files changed
+64 -99

No files matched your search

+14 -5
View File
@@ -1,11 +1,11 @@
use crate::innerlude::{remove_future, spawn, Runtime};
use crate::ScopeId;
use futures_util::task::ArcWake;
use std::pin::Pin;
use std::sync::Arc;
use std::task::Waker;
use std::{cell::Cell, future::Future};
use std::{cell::RefCell, rc::Rc};
use std::{pin::Pin, task::Poll};
/// A task's unique identifier.
///
@@ -50,6 +50,11 @@ impl Task {
Runtime::with(|rt| _ = rt.sender.unbounded_send(SchedulerMsg::TaskNotified(*self)));
}
/// Poll the task immediately.
pub fn poll_now(&self) -> Poll<()> {
Runtime::with(|rt| rt.handle_task_wakeup(*self)).unwrap()
}
/// Set the task as active or paused.
pub fn set_active(&self, active: bool) {
Runtime::with(|rt| rt.tasks.borrow()[self.0].active.set(active));
@@ -122,19 +127,19 @@ impl Runtime {
self.tasks.borrow().get(task.0)?.parent
}
pub(crate) fn handle_task_wakeup(&self, id: Task) {
pub(crate) fn handle_task_wakeup(&self, id: Task) -> Poll<()> {
debug_assert!(Runtime::current().is_some(), "Must be in a dioxus runtime");
let task = self.tasks.borrow().get(id.0).cloned();
// The task was removed from the scheduler, so we can just ignore it
let Some(task) = task else {
return;
return Poll::Ready(());
};
// If a task woke up but is paused, we can just ignore it
if !task.active.get() {
return;
return Poll::Pending;
}
let mut cx = std::task::Context::from_waker(&task.waker);
@@ -144,7 +149,9 @@ impl Runtime {
self.rendering.set(false);
self.current_task.set(Some(id));
if task.task.borrow_mut().as_mut().poll(&mut cx).is_ready() {
let poll_result = task.task.borrow_mut().as_mut().poll(&mut cx);
if poll_result.is_ready() {
// Remove it from the scope so we dont try to double drop it when the scope dropes
self.get_state(task.scope)
.unwrap()
@@ -160,6 +167,8 @@ impl Runtime {
self.scope_stack.borrow_mut().pop();
self.rendering.set(true);
self.current_task.set(None);
poll_result
}
/// Drop the future with the given TaskId
+2 -2
View File
@@ -454,7 +454,7 @@ impl VirtualDom {
match self.rx.next().await.expect("channel should never close") {
SchedulerMsg::Immediate(id) => self.mark_dirty(id),
SchedulerMsg::TaskNotified(id) => self.runtime.handle_task_wakeup(id),
SchedulerMsg::TaskNotified(id) => _ = self.runtime.handle_task_wakeup(id),
};
}
}
@@ -467,7 +467,7 @@ impl VirtualDom {
while let Ok(Some(msg)) = self.rx.try_next() {
match msg {
SchedulerMsg::Immediate(id) => self.mark_dirty(id),
SchedulerMsg::TaskNotified(task) => self.runtime.handle_task_wakeup(task),
SchedulerMsg::TaskNotified(task) => _ = self.runtime.handle_task_wakeup(task),
}
}
}