fix: use futuresunordered instead of bespoke waker system
This commit is contained in:
1 parent
ff697d41e3
commit
bd720e2268
12 files changed
+308
-272
No files matched your search
@@ -1,9 +1,13 @@
|
||||
use super::{waker::ArcWake, Scheduler, SchedulerMsg};
|
||||
use futures_util::task::ArcWake;
|
||||
use futures_util::FutureExt;
|
||||
|
||||
use super::{Scheduler, SchedulerMsg};
|
||||
use crate::ScopeId;
|
||||
use std::cell::RefCell;
|
||||
use std::future::Future;
|
||||
use std::pin::Pin;
|
||||
use std::sync::Arc;
|
||||
use std::task::Waker;
|
||||
|
||||
/// A task's unique identifier.
|
||||
///
|
||||
@@ -19,6 +23,17 @@ pub(crate) struct LocalTask {
|
||||
pub(super) task: RefCell<Pin<Box<dyn Future<Output = ()> + 'static>>>,
|
||||
id: TaskId,
|
||||
tx: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
||||
pub waker: Waker,
|
||||
}
|
||||
impl Future for LocalTask {
|
||||
type Output = ();
|
||||
|
||||
fn poll(
|
||||
self: Pin<&mut Self>,
|
||||
cx: &mut std::task::Context<'_>,
|
||||
) -> std::task::Poll<Self::Output> {
|
||||
self.task.borrow_mut().poll_unpin(cx)
|
||||
}
|
||||
}
|
||||
|
||||
impl Scheduler {
|
||||
@@ -32,16 +47,23 @@ 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 {
|
||||
let mut tasks = self.tasks.borrow_mut();
|
||||
let entry = tasks.vacant_entry();
|
||||
let task_id = TaskId(entry.key());
|
||||
// let entry = tasks.vacant_entry();
|
||||
let task_id = TaskId(0);
|
||||
|
||||
entry.insert(Arc::new(LocalTask {
|
||||
let task = LocalTask {
|
||||
id: task_id,
|
||||
tx: self.sender.clone(),
|
||||
task: RefCell::new(Box::pin(task)),
|
||||
scope,
|
||||
}));
|
||||
waker: futures_util::task::waker(Arc::new(LocalTaskHandle {
|
||||
id: task_id,
|
||||
tx: self.sender.clone(),
|
||||
})),
|
||||
};
|
||||
|
||||
self.tasks.borrow_mut().push(task);
|
||||
|
||||
// println!("Spawning task: {:?}", task_id);
|
||||
|
||||
self.sender
|
||||
.unbounded_send(SchedulerMsg::TaskNotified(task_id))
|
||||
@@ -54,14 +76,22 @@ impl Scheduler {
|
||||
///
|
||||
/// This does nto 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().remove(id.0);
|
||||
// self.tasks.borrow_mut().remove(id.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl ArcWake for LocalTask {
|
||||
pub struct LocalTaskHandle {
|
||||
id: TaskId,
|
||||
tx: futures_channel::mpsc::UnboundedSender<SchedulerMsg>,
|
||||
}
|
||||
|
||||
impl ArcWake for LocalTaskHandle {
|
||||
fn wake_by_ref(arc_self: &Arc<Self>) {
|
||||
_ = arc_self
|
||||
println!("waking task: {:?} {:p}", arc_self.id, arc_self);
|
||||
|
||||
arc_self
|
||||
.tx
|
||||
.unbounded_send(SchedulerMsg::TaskNotified(arc_self.id));
|
||||
.unbounded_send(SchedulerMsg::TaskNotified(arc_self.id))
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user