use crate::diff::DiffState;
use crate::innerlude::*;
use futures_channel::mpsc::{UnboundedReceiver, UnboundedSender};
use futures_util::{future::poll_fn, StreamExt};
use fxhash::FxHashSet;
use indexmap::IndexSet;
use std::{collections::VecDeque, iter::FromIterator, task::Poll};
pub struct VirtualDom {
scopes: ScopeArena,
pending_messages: VecDeque<SchedulerMsg>,
dirty_scopes: IndexSet<ScopeId>,
channel: (
UnboundedSender<SchedulerMsg>,
UnboundedReceiver<SchedulerMsg>,
),
}
#[derive(Debug)]
pub enum SchedulerMsg {
Event(UserEvent),
Immediate(ScopeId),
NewTask(ScopeId),
}
impl VirtualDom {
pub fn new(root: Component) -> Self {
Self::new_with_props(root, ())
}
pub fn new_with_props<P>(root: Component<P>, root_props: P) -> Self
where
P: 'static,
{
Self::new_with_props_and_scheduler(
root,
root_props,
futures_channel::mpsc::unbounded::<SchedulerMsg>(),
)
}
pub fn new_with_props_and_scheduler<P: 'static>(
root: Component<P>,
root_props: P,
channel: (
UnboundedSender<SchedulerMsg>,
UnboundedReceiver<SchedulerMsg>,
),
) -> Self {
let scopes = ScopeArena::new(channel.0.clone());
scopes.new_with_key(
root as ComponentPtr,
Box::new(VComponentProps {
props: root_props,
memo: |_a, _b| unreachable!("memo on root will neve be run"),
render_fn: root,
}),
None,
ElementId(0),
0,
);
Self {
scopes,
channel,
dirty_scopes: IndexSet::from_iter([ScopeId(0)]),
pending_messages: VecDeque::new(),
}
}
pub fn base_scope(&self) -> &ScopeState {
self.get_scope(ScopeId(0)).unwrap()
}
pub fn get_scope(&self, id: ScopeId) -> Option<&ScopeState> {
self.scopes.get_scope(id)
}
pub fn get_scheduler_channel(&self) -> UnboundedSender<SchedulerMsg> {
self.channel.0.clone()
}
pub fn get_element(&self, id: ElementId) -> Option<&VNode> {
self.scopes.get_element(id)
}
pub fn handle_message(&mut self, msg: SchedulerMsg) {
if self.channel.0.unbounded_send(msg).is_ok() {
self.process_all_messages();
}
}
pub fn has_work(&self) -> bool {
!(self.dirty_scopes.is_empty() && self.pending_messages.is_empty())
}
pub async fn wait_for_work(&mut self) {
loop {
if !self.dirty_scopes.is_empty() && self.pending_messages.is_empty() {
break;
}
if self.pending_messages.is_empty() {
if self.scopes.tasks.has_tasks() {
use futures_util::future::{select, Either};
let scopes = &mut self.scopes;
let task_poll = poll_fn(|cx| {
let mut any_pending = false;
let mut tasks = scopes.tasks.tasks.borrow_mut();
let mut to_remove = vec![];
for (id, task) in tasks.iter_mut() {
if task.as_mut().poll(cx).is_ready() {
to_remove.push(*id);
} else {
any_pending = true;
}
}
for id in to_remove {
tasks.remove(&id);
}
match any_pending {
true => Poll::Pending,
false => Poll::Ready(()),
}
});
match select(task_poll, self.channel.1.next()).await {
Either::Left((_, _)) => {}
Either::Right((msg, _)) => self.pending_messages.push_front(msg.unwrap()),
}
} else {
self.pending_messages
.push_front(self.channel.1.next().await.unwrap());
}
}
self.process_all_messages();
}
}
pub fn process_all_messages(&mut self) {
while let Ok(Some(msg)) = self.channel.1.try_next() {
self.pending_messages.push_front(msg);
}
while let Some(msg) = self.pending_messages.pop_back() {
self.process_message(msg);
}
}
pub fn process_message(&mut self, msg: SchedulerMsg) {
match msg {
SchedulerMsg::NewTask(_id) => {
}
SchedulerMsg::Event(event) => {
if let Some(element) = event.element {
self.scopes.call_listener_with_bubbling(event, element);
}
}
SchedulerMsg::Immediate(s) => {
self.dirty_scopes.insert(s);
}
}
}
#[allow(unused)]
pub fn work_with_deadline(&mut self, mut deadline: impl FnMut() -> bool) -> Vec<Mutations> {
let mut committed_mutations = vec![];
while !self.dirty_scopes.is_empty() {
let scopes = &self.scopes;
let mut diff_state = DiffState::new(scopes);
let mut ran_scopes = FxHashSet::default();
self.dirty_scopes
.retain(|id| scopes.get_scope(*id).is_some());
self.dirty_scopes.sort_by(|a, b| {
let h1 = scopes.get_scope(*a).unwrap().height;
let h2 = scopes.get_scope(*b).unwrap().height;
h1.cmp(&h2).reverse()
});
log::trace!("dirty_scopes: {:?}", self.dirty_scopes);
if let Some(scopeid) = self.dirty_scopes.pop() {
if !ran_scopes.contains(&scopeid) {
ran_scopes.insert(scopeid);
self.scopes.run_scope(scopeid);
diff_state.diff_scope(scopeid);
let DiffState { mutations, .. } = diff_state;
log::trace!("succesffuly resolved scopes {:?}", mutations.dirty_scopes);
for scope in &mutations.dirty_scopes {
self.dirty_scopes.remove(scope);
}
if !mutations.edits.is_empty() {
committed_mutations.push(mutations);
}
}
}
}
committed_mutations
}
pub fn rebuild(&mut self) -> Mutations {
let scope_id = ScopeId(0);
let mut diff_state = DiffState::new(&self.scopes);
self.scopes.run_scope(scope_id);
diff_state.element_stack.push(ElementId(0));
diff_state.scope_stack.push(scope_id);
let node = self.scopes.fin_head(scope_id);
let created = diff_state.create_node(node);
diff_state.mutations.append_children(created as u32);
self.dirty_scopes.clear();
assert!(self.dirty_scopes.is_empty());
diff_state.mutations
}
pub fn hard_diff(&mut self, scope_id: ScopeId) -> Mutations {
let mut diff_machine = DiffState::new(&self.scopes);
self.scopes.run_scope(scope_id);
let (old, new) = (
diff_machine.scopes.wip_head(scope_id),
diff_machine.scopes.fin_head(scope_id),
);
diff_machine.force_diff = true;
diff_machine.scope_stack.push(scope_id);
let scope = diff_machine.scopes.get_scope(scope_id).unwrap();
diff_machine.element_stack.push(scope.container);
diff_machine.diff_node(old, new);
diff_machine.mutations
}
pub fn render_vnodes<'a>(&'a self, lazy_nodes: LazyNodes<'a, '_>) -> &'a VNode<'a> {
let scope = self.scopes.get_scope(ScopeId(0)).unwrap();
let frame = scope.wip_frame();
let factory = NodeFactory::new(scope);
let node = lazy_nodes.call(factory);
frame.bump.alloc(node)
}
pub fn diff_vnodes<'a>(&'a self, old: &'a VNode<'a>, new: &'a VNode<'a>) -> Mutations<'a> {
let mut machine = DiffState::new(&self.scopes);
machine.element_stack.push(ElementId(0));
machine.scope_stack.push(ScopeId(0));
machine.diff_node(old, new);
machine.mutations
}
pub fn create_vnodes<'a>(&'a self, nodes: LazyNodes<'a, '_>) -> Mutations<'a> {
let mut machine = DiffState::new(&self.scopes);
machine.scope_stack.push(ScopeId(0));
machine.element_stack.push(ElementId(0));
let node = self.render_vnodes(nodes);
let created = machine.create_node(node);
machine.mutations.append_children(created as u32);
machine.mutations
}
pub fn diff_lazynodes<'a>(
&'a self,
left: LazyNodes<'a, '_>,
right: LazyNodes<'a, '_>,
) -> (Mutations<'a>, Mutations<'a>) {
let (old, new) = (self.render_vnodes(left), self.render_vnodes(right));
let mut create = DiffState::new(&self.scopes);
create.scope_stack.push(ScopeId(0));
create.element_stack.push(ElementId(0));
let created = create.create_node(old);
create.mutations.append_children(created as u32);
let mut edit = DiffState::new(&self.scopes);
edit.scope_stack.push(ScopeId(0));
edit.element_stack.push(ElementId(0));
edit.diff_node(old, new);
(create.mutations, edit.mutations)
}
}
impl Drop for VirtualDom {
fn drop(&mut self) {
let scope = self.scopes.get_scope(ScopeId(0)).unwrap();
let mut machine = DiffState::new(&self.scopes);
machine.remove_nodes([scope.root_node()], false);
let scope = unsafe { &mut *self.scopes.get_scope_raw(ScopeId(0)).unwrap() };
scope.reset();
for (_, scopeptr) in self.scopes.scopes.get_mut().drain() {
let scope = unsafe { bumpalo::boxed::Box::from_raw(scopeptr) };
drop(scope);
}
for scopeptr in self.scopes.free_scopes.get_mut().drain(..) {
let mut scope = unsafe { bumpalo::boxed::Box::from_raw(scopeptr) };
scope.reset();
drop(scope);
}
}
}