create.rs 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. use crate::factory::RenderReturn;
  2. use crate::innerlude::{Mutations, SuspenseContext};
  3. use crate::mutations::Mutation;
  4. use crate::mutations::Mutation::*;
  5. use crate::nodes::VNode;
  6. use crate::nodes::{DynamicNode, TemplateNode};
  7. use crate::virtual_dom::VirtualDom;
  8. use crate::{AttributeValue, ElementId, ScopeId, TemplateAttribute};
  9. impl VirtualDom {
  10. pub(crate) fn create_scope<'a>(
  11. &mut self,
  12. scope: ScopeId,
  13. mutations: &mut Mutations<'a>,
  14. template: &'a VNode<'a>,
  15. ) -> usize {
  16. self.scope_stack.push(scope);
  17. let out = self.create(mutations, template);
  18. self.scope_stack.pop();
  19. out
  20. }
  21. /// Create this template and write its mutations
  22. pub(crate) fn create<'a>(
  23. &mut self,
  24. mutations: &mut Mutations<'a>,
  25. template: &'a VNode<'a>,
  26. ) -> usize {
  27. // The best renderers will have templates prehydrated and registered
  28. // Just in case, let's create the template using instructions anyways
  29. if !self.templates.contains_key(&template.template.id) {
  30. for node in template.template.roots {
  31. let mutations = &mut mutations.template_mutations;
  32. self.create_static_node(mutations, template, node);
  33. }
  34. mutations.template_mutations.push(SaveTemplate {
  35. name: template.template.id,
  36. m: template.template.roots.len(),
  37. });
  38. self.templates
  39. .insert(template.template.id, template.template.clone());
  40. }
  41. // Walk the roots, creating nodes and assigning IDs
  42. // todo: adjust dynamic nodes to be in the order of roots and then leaves (ie BFS)
  43. let mut dynamic_attrs = template.template.attr_paths.iter().enumerate().peekable();
  44. let mut dynamic_nodes = template.template.node_paths.iter().enumerate().peekable();
  45. let mut on_stack = 0;
  46. for (root_idx, root) in template.template.roots.iter().enumerate() {
  47. on_stack += match root {
  48. TemplateNode::Element { .. }
  49. | TemplateNode::Text(_)
  50. | TemplateNode::DynamicText { .. } => {
  51. mutations.push(LoadTemplate {
  52. name: template.template.id,
  53. index: root_idx,
  54. });
  55. 1
  56. }
  57. TemplateNode::Dynamic(id) => {
  58. self.create_dynamic_node(mutations, template, &template.dynamic_nodes[*id], *id)
  59. }
  60. };
  61. // we're on top of a node that has a dynamic attribute for a descendant
  62. // Set that attribute now before the stack gets in a weird state
  63. while let Some((mut attr_id, path)) =
  64. dynamic_attrs.next_if(|(_, p)| p[0] == root_idx as u8)
  65. {
  66. let id = self.next_element(template);
  67. mutations.push(AssignId {
  68. path: &path[1..],
  69. id,
  70. });
  71. loop {
  72. let attribute = &template.dynamic_attrs[attr_id];
  73. attribute.mounted_element.set(id);
  74. match attribute.value {
  75. AttributeValue::Text(value) => mutations.push(SetAttribute {
  76. name: attribute.name,
  77. value,
  78. id,
  79. }),
  80. AttributeValue::Listener(_) => todo!("create listener attributes"),
  81. AttributeValue::Float(_) => todo!(),
  82. AttributeValue::Int(_) => todo!(),
  83. AttributeValue::Bool(_) => todo!(),
  84. AttributeValue::Any(_) => todo!(),
  85. AttributeValue::None => todo!(),
  86. }
  87. // Only push the dynamic attributes forward if they match the current path (same element)
  88. match dynamic_attrs.next_if(|(_, p)| *p == path) {
  89. Some((next_attr_id, _)) => attr_id = next_attr_id,
  90. None => break,
  91. }
  92. }
  93. }
  94. // We're on top of a node that has a dynamic child for a descendant
  95. // Skip any node that's a root
  96. while let Some((idx, path)) =
  97. dynamic_nodes.next_if(|(_, p)| p.len() > 1 && p[0] == root_idx as u8)
  98. {
  99. let node = &template.dynamic_nodes[idx];
  100. let m = self.create_dynamic_node(mutations, template, node, idx);
  101. if m > 0 {
  102. mutations.push(ReplacePlaceholder {
  103. m,
  104. path: &path[1..],
  105. });
  106. }
  107. }
  108. }
  109. on_stack
  110. }
  111. pub(crate) fn create_static_node<'a>(
  112. &mut self,
  113. mutations: &mut Vec<Mutation<'a>>,
  114. template: &'a VNode<'a>,
  115. node: &'a TemplateNode<'static>,
  116. ) {
  117. match *node {
  118. // Todo: create the children's template
  119. TemplateNode::Dynamic(_) => {
  120. let id = self.next_element(template);
  121. mutations.push(CreatePlaceholder { id })
  122. }
  123. TemplateNode::Text(value) => mutations.push(CreateText { value }),
  124. TemplateNode::DynamicText { .. } => mutations.push(CreateText {
  125. value: "placeholder",
  126. }),
  127. TemplateNode::Element {
  128. attrs,
  129. children,
  130. namespace,
  131. tag,
  132. } => {
  133. let id = self.next_element(template);
  134. mutations.push(CreateElement {
  135. name: tag,
  136. namespace,
  137. id,
  138. });
  139. mutations.extend(attrs.into_iter().filter_map(|attr| match attr {
  140. TemplateAttribute::Static { name, value, .. } => {
  141. Some(SetAttribute { name, value, id })
  142. }
  143. _ => None,
  144. }));
  145. children
  146. .into_iter()
  147. .for_each(|child| self.create_static_node(mutations, template, child));
  148. mutations.push(AppendChildren { m: children.len() })
  149. }
  150. }
  151. }
  152. pub(crate) fn create_dynamic_node<'a>(
  153. &mut self,
  154. mutations: &mut Mutations<'a>,
  155. template: &'a VNode<'a>,
  156. node: &'a DynamicNode<'a>,
  157. idx: usize,
  158. ) -> usize {
  159. match &node {
  160. DynamicNode::Text { id, value } => {
  161. let new_id = self.next_element(template);
  162. id.set(new_id);
  163. mutations.push(HydrateText {
  164. id: new_id,
  165. path: &template.template.node_paths[idx][1..],
  166. value,
  167. });
  168. 1
  169. }
  170. DynamicNode::Component {
  171. props, placeholder, ..
  172. } => {
  173. let scope = self
  174. .new_scope(unsafe { std::mem::transmute(props.get()) })
  175. .id;
  176. let return_nodes = unsafe { self.run_scope(scope).extend_lifetime_ref() };
  177. match return_nodes {
  178. RenderReturn::Sync(None) | RenderReturn::Async(_) => {
  179. let new_id = self.next_element(template);
  180. placeholder.set(Some(new_id));
  181. self.scopes[scope.0].placeholder.set(Some(new_id));
  182. mutations.push(AssignId {
  183. id: new_id,
  184. path: &template.template.node_paths[idx][1..],
  185. });
  186. 0
  187. }
  188. RenderReturn::Sync(Some(template)) => {
  189. let mutations_to_this_point = mutations.len();
  190. self.scope_stack.push(scope);
  191. let mut created = self.create(mutations, template);
  192. self.scope_stack.pop();
  193. if !self.collected_leaves.is_empty() {
  194. if let Some(boundary) =
  195. self.scopes[scope.0].has_context::<SuspenseContext>()
  196. {
  197. let mut boundary_mut = boundary.borrow_mut();
  198. let split_off = mutations.split_off(mutations_to_this_point);
  199. let split_off = unsafe { std::mem::transmute(split_off) };
  200. boundary_mut.mutations.edits = split_off;
  201. boundary_mut
  202. .waiting_on
  203. .extend(self.collected_leaves.drain(..));
  204. // Since this is a boundary, use it as a placeholder
  205. let new_id = self.next_element(template);
  206. placeholder.set(Some(new_id));
  207. self.scopes[scope.0].placeholder.set(Some(new_id));
  208. mutations.push(AssignId {
  209. id: new_id,
  210. path: &template.template.node_paths[idx][1..],
  211. });
  212. created = 0;
  213. }
  214. }
  215. // handle any waiting on futures accumulated by async calls down the tree
  216. // if this is a boundary, we split off the tree
  217. created
  218. }
  219. }
  220. }
  221. DynamicNode::Fragment(children) => children
  222. .iter()
  223. .fold(0, |acc, child| acc + self.create(mutations, child)),
  224. DynamicNode::Placeholder(_) => {
  225. let id = self.next_element(template);
  226. mutations.push(CreatePlaceholder { id });
  227. 1
  228. }
  229. }
  230. }
  231. }