wait.rs 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. use futures_util::FutureExt;
  2. use std::{
  3. rc::Rc,
  4. task::{Context, Poll},
  5. };
  6. use crate::{
  7. innerlude::{Mutation, Mutations, SuspenseContext},
  8. nodes::RenderReturn,
  9. ScopeId, TaskId, VNode, VirtualDom,
  10. };
  11. use super::{waker::ArcWake, SuspenseId};
  12. impl VirtualDom {
  13. /// Handle notifications by tasks inside the scheduler
  14. ///
  15. /// This is precise, meaning we won't poll every task, just tasks that have woken up as notified to use by the
  16. /// queue
  17. pub(crate) fn handle_task_wakeup(&mut self, id: TaskId) {
  18. let mut tasks = self.scheduler.tasks.borrow_mut();
  19. let task = match tasks.get(id.0) {
  20. Some(task) => task,
  21. None => return,
  22. };
  23. let waker = task.waker();
  24. let mut cx = Context::from_waker(&waker);
  25. // If the task completes...
  26. if task.task.borrow_mut().as_mut().poll(&mut cx).is_ready() {
  27. // Remove it from the scope so we dont try to double drop it when the scope dropes
  28. self.scopes[task.scope.0].spawned_tasks.remove(&id);
  29. // Remove it from the scheduler
  30. tasks.remove(id.0);
  31. }
  32. }
  33. pub(crate) fn acquire_suspense_boundary(&self, id: ScopeId) -> Rc<SuspenseContext> {
  34. self.scopes[id.0]
  35. .consume_context::<Rc<SuspenseContext>>()
  36. .unwrap()
  37. }
  38. pub(crate) fn handle_suspense_wakeup(&mut self, id: SuspenseId) {
  39. let leaf = self
  40. .scheduler
  41. .leaves
  42. .borrow_mut()
  43. .get(id.0)
  44. .unwrap()
  45. .clone();
  46. let scope_id = leaf.scope_id;
  47. // todo: cache the waker
  48. let waker = leaf.waker();
  49. let mut cx = Context::from_waker(&waker);
  50. // Safety: the future is always pinned to the bump arena
  51. let mut pinned = unsafe { std::pin::Pin::new_unchecked(&mut *leaf.task) };
  52. let as_pinned_mut = &mut pinned;
  53. // the component finished rendering and gave us nodes
  54. // we should attach them to that component and then render its children
  55. // continue rendering the tree until we hit yet another suspended component
  56. if let Poll::Ready(new_nodes) = as_pinned_mut.poll_unpin(&mut cx) {
  57. let fiber = self.acquire_suspense_boundary(leaf.scope_id);
  58. let scope = &mut self.scopes[scope_id.0];
  59. let arena = scope.current_frame();
  60. let ret = arena.bump.alloc(match new_nodes {
  61. Some(new) => RenderReturn::Ready(new),
  62. None => RenderReturn::default(),
  63. });
  64. arena.node.set(ret);
  65. fiber.waiting_on.borrow_mut().remove(&id);
  66. if let RenderReturn::Ready(template) = ret {
  67. let mutations_ref = &mut fiber.mutations.borrow_mut();
  68. let mutations = &mut **mutations_ref;
  69. let template: &VNode = unsafe { std::mem::transmute(template) };
  70. let mutations: &mut Mutations = unsafe { std::mem::transmute(mutations) };
  71. std::mem::swap(&mut self.mutations, mutations);
  72. let place_holder_id = scope.placeholder.get().unwrap();
  73. self.scope_stack.push(scope_id);
  74. let created = self.create(template);
  75. self.scope_stack.pop();
  76. mutations.push(Mutation::ReplaceWith {
  77. id: place_holder_id,
  78. m: created,
  79. });
  80. for leaf in self.collected_leaves.drain(..) {
  81. fiber.waiting_on.borrow_mut().insert(leaf);
  82. }
  83. std::mem::swap(&mut self.mutations, mutations);
  84. if fiber.waiting_on.borrow().is_empty() {
  85. self.finished_fibers.push(fiber.id);
  86. }
  87. }
  88. }
  89. }
  90. }