Make cx.spawn() return an Option<TaskId>, returns None if finished immediately
This commit is contained in:
1 parent
695bf009d1
commit
69ca1fd8a7
8 files changed
+38
-37
No files matched your search
@@ -19,7 +19,7 @@ pub struct TaskId(pub usize);
|
||||
/// the task itself is the waker
|
||||
pub(crate) struct LocalTask {
|
||||
pub scope: ScopeId,
|
||||
pub(super) task: RefCell<Pin<Box<dyn Future<Output = ()> + 'static>>>,
|
||||
pub task: RefCell<Pin<Box<dyn Future<Output = ()> + 'static>>>,
|
||||
pub waker: Waker,
|
||||
}
|
||||
|
||||
@@ -33,7 +33,11 @@ impl Scheduler {
|
||||
///
|
||||
/// Spawning a future onto the root scope will cause it to be dropped when the root component is dropped - which
|
||||
/// will only occur when the VirtuaalDom itself has been dropped.
|
||||
pub fn spawn(&self, scope: ScopeId, task: impl Future<Output = ()> + 'static) -> TaskId {
|
||||
pub fn spawn(
|
||||
&self,
|
||||
scope: ScopeId,
|
||||
task: impl Future<Output = ()> + 'static,
|
||||
) -> Option<TaskId> {
|
||||
let mut tasks = self.tasks.borrow_mut();
|
||||
|
||||
let entry = tasks.vacant_entry();
|
||||
@@ -48,20 +52,25 @@ impl Scheduler {
|
||||
})),
|
||||
};
|
||||
|
||||
let mut cx = std::task::Context::from_waker(&task.waker);
|
||||
if task.task.borrow_mut().as_mut().poll(&mut cx).is_ready() {
|
||||
return None;
|
||||
}
|
||||
|
||||
entry.insert(task);
|
||||
|
||||
self.sender
|
||||
.unbounded_send(SchedulerMsg::TaskNotified(task_id))
|
||||
.expect("Scheduler should exist");
|
||||
|
||||
task_id
|
||||
Some(task_id)
|
||||
}
|
||||
|
||||
/// Drop the future with the given TaskId
|
||||
///
|
||||
/// This does not abort the task, so you'll want to wrap it in an aborthandle if that's important to you
|
||||
pub fn remove(&self, id: TaskId) {
|
||||
self.tasks.borrow_mut().try_remove(id.0);
|
||||
pub fn remove(&self, id: TaskId) -> Option<LocalTask> {
|
||||
self.tasks.borrow_mut().try_remove(id.0)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -214,10 +214,10 @@ impl ScopeContext {
|
||||
}
|
||||
|
||||
/// Pushes the future onto the poll queue to be polled after the component renders.
|
||||
pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
|
||||
let id = self.tasks.spawn(self.id, fut);
|
||||
pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
|
||||
let id = self.tasks.spawn(self.id, fut)?;
|
||||
self.spawned_tasks.borrow_mut().insert(id);
|
||||
id
|
||||
Some(id)
|
||||
}
|
||||
|
||||
/// Spawns the future but does not return the [`TaskId`]
|
||||
@@ -228,9 +228,9 @@ impl ScopeContext {
|
||||
/// Spawn a future that Dioxus won't clean up when this component is unmounted
|
||||
///
|
||||
/// This is good for tasks that need to be run after the component has been dropped.
|
||||
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
|
||||
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
|
||||
// The root scope will never be unmounted so we can just add the task at the top of the app
|
||||
let id = self.tasks.spawn(ScopeId::ROOT, fut);
|
||||
let id = self.tasks.spawn(ScopeId::ROOT, fut)?;
|
||||
|
||||
// wake up the scheduler if it is sleeping
|
||||
self.tasks
|
||||
@@ -240,7 +240,7 @@ impl ScopeContext {
|
||||
|
||||
self.spawned_tasks.borrow_mut().insert(id);
|
||||
|
||||
id
|
||||
Some(id)
|
||||
}
|
||||
|
||||
/// Informs the scheduler that this task is no longer needed and should be removed.
|
||||
@@ -339,7 +339,7 @@ pub fn throw(error: impl Debug + 'static) -> Option<()> {
|
||||
|
||||
/// Pushes the future onto the poll queue to be polled after the component renders.
|
||||
pub fn push_future(fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
|
||||
with_current_scope(|cx| cx.push_future(fut))
|
||||
with_current_scope(|cx| cx.push_future(fut)).flatten()
|
||||
}
|
||||
|
||||
/// Spawns the future but does not return the [`TaskId`]
|
||||
@@ -351,7 +351,7 @@ pub fn spawn(fut: impl Future<Output = ()> + 'static) {
|
||||
///
|
||||
/// This is good for tasks that need to be run after the component has been dropped.
|
||||
pub fn spawn_forever(fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
|
||||
with_current_scope(|cx| cx.spawn_forever(fut))
|
||||
with_current_scope(|cx| cx.spawn_forever(fut)).flatten()
|
||||
}
|
||||
|
||||
/// Informs the scheduler that this task is no longer needed and should be removed.
|
||||
|
||||
@@ -309,7 +309,7 @@ impl<'src> ScopeState {
|
||||
}
|
||||
|
||||
/// Pushes the future onto the poll queue to be polled after the component renders.
|
||||
pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
|
||||
pub fn push_future(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
|
||||
self.context().push_future(fut)
|
||||
}
|
||||
|
||||
@@ -321,7 +321,7 @@ impl<'src> ScopeState {
|
||||
/// Spawn a future that Dioxus won't clean up when this component is unmounted
|
||||
///
|
||||
/// This is good for tasks that need to be run after the component has been dropped.
|
||||
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> TaskId {
|
||||
pub fn spawn_forever(&self, fut: impl Future<Output = ()> + 'static) -> Option<TaskId> {
|
||||
self.context().spawn_forever(fut)
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user