chore: use existing scheduler
This commit is contained in:
1 parent
972084ee32
commit
daa5449b6b
6 files changed
+85
-112
No files matched your search
@@ -1,5 +1,4 @@
|
||||
use futures_util::task::ArcWake;
|
||||
use futures_util::FutureExt;
|
||||
|
||||
use super::{Scheduler, SchedulerMsg};
|
||||
use crate::ScopeId;
|
||||
@@ -21,20 +20,8 @@ pub struct TaskId(pub usize);
|
||||
pub(crate) struct LocalTask {
|
||||
pub scope: ScopeId,
|
||||
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 {
|
||||
/// Start a new future on the same thread as the rest of the VirtualDom.
|
||||
@@ -47,12 +34,12 @@ 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 entry = tasks.vacant_entry();
|
||||
let task_id = TaskId(0);
|
||||
let mut tasks = self.tasks.borrow_mut();
|
||||
|
||||
let entry = tasks.vacant_entry();
|
||||
let task_id = TaskId(entry.key());
|
||||
|
||||
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 {
|
||||
@@ -61,9 +48,7 @@ impl Scheduler {
|
||||
})),
|
||||
};
|
||||
|
||||
self.tasks.borrow_mut().push(task);
|
||||
|
||||
// println!("Spawning task: {:?}", task_id);
|
||||
entry.insert(task);
|
||||
|
||||
self.sender
|
||||
.unbounded_send(SchedulerMsg::TaskNotified(task_id))
|
||||
@@ -76,7 +61,7 @@ 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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user