test_dom.rs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. //! A DOM for testing - both internal and external code.
  2. use bumpalo::Bump;
  3. use crate::innerlude::*;
  4. use crate::nodes::IntoVNode;
  5. pub struct TestDom {
  6. bump: Bump,
  7. scheduler: Scheduler,
  8. }
  9. impl TestDom {
  10. pub fn new() -> TestDom {
  11. let bump = Bump::new();
  12. let (sender, receiver) = futures_channel::mpsc::unbounded::<SchedulerMsg>();
  13. let scheduler = Scheduler::new(sender, receiver);
  14. TestDom { bump, scheduler }
  15. }
  16. pub fn new_factory(&self) -> NodeFactory {
  17. NodeFactory::new(&self.bump)
  18. }
  19. pub fn render_direct<'a>(&'a self, lazy_nodes: Option<LazyNodes<'a, '_>>) -> VNode<'a> {
  20. lazy_nodes.into_vnode(NodeFactory::new(&self.bump))
  21. }
  22. pub fn render<'a>(&'a self, lazy_nodes: Option<LazyNodes<'a, '_>>) -> &'a VNode<'a> {
  23. self.bump
  24. .alloc(lazy_nodes.into_vnode(NodeFactory::new(&self.bump)))
  25. }
  26. pub fn diff<'a>(&'a self, old: &'a VNode<'a>, new: &'a VNode<'a>) -> Mutations<'a> {
  27. let mutations = Mutations::new();
  28. let mut machine = DiffMachine::new(mutations, &self.scheduler.pool);
  29. machine.stack.push(DiffInstruction::Diff { new, old });
  30. machine.mutations
  31. }
  32. pub fn create<'a>(&'a self, left: Option<LazyNodes<'a, '_>>) -> Mutations<'a> {
  33. let old = self.bump.alloc(self.render_direct(left));
  34. let mut machine = DiffMachine::new(Mutations::new(), &self.scheduler.pool);
  35. machine.stack.create_node(old, MountType::Append);
  36. machine.work(&mut || false);
  37. machine.mutations
  38. }
  39. pub fn lazy_diff<'a>(
  40. &'a self,
  41. left: Option<LazyNodes<'a, '_>>,
  42. right: Option<LazyNodes<'a, '_>>,
  43. ) -> (Mutations<'a>, Mutations<'a>) {
  44. let (old, new) = (self.render(left), self.render(right));
  45. let mut machine = DiffMachine::new(Mutations::new(), &self.scheduler.pool);
  46. machine.stack.create_node(old, MountType::Append);
  47. machine.work(|| false);
  48. let create_edits = machine.mutations;
  49. let mut machine = DiffMachine::new(Mutations::new(), &self.scheduler.pool);
  50. machine.stack.push(DiffInstruction::Diff { old, new });
  51. machine.work(&mut || false);
  52. let edits = machine.mutations;
  53. (create_edits, edits)
  54. }
  55. }
  56. impl Default for TestDom {
  57. fn default() -> Self {
  58. Self::new()
  59. }
  60. }