macrosrc.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. #![allow(unused, non_upper_case_globals, non_snake_case)]
  2. use bumpalo::Bump;
  3. use dioxus_core::prelude::*;
  4. use dioxus_core::{nodebuilder::*, virtual_dom::Properties};
  5. use std::{collections::HashMap, future::Future, marker::PhantomData};
  6. fn main() {
  7. let mut vdom = VirtualDom::new_with_props(
  8. component,
  9. Props {
  10. blah: false,
  11. text: "blah",
  12. },
  13. );
  14. vdom.progress();
  15. let somet = String::from("asd");
  16. let text = somet.as_str();
  17. /*
  18. this could be auto-generated via the macro
  19. this props is allocated in this
  20. but the component and props would like need to be cached
  21. we could box this fn, abstracting away the props requirement and just keep the entrance and allocator requirement
  22. How do we keep cached things around?
  23. Need some sort of caching mechanism
  24. how do we enter into a childscope from a parent scope?
  25. Problems:
  26. 1: Comp props need to be stored somewhere so we can re-evalute components when they receive updates
  27. 2: Trees are not evaluated
  28. */
  29. let example_caller = move |ctx: &Bump| {
  30. todo!()
  31. // let p = Props { blah: true, text };
  32. // let c = Context { props: &p };
  33. // let r = component(&c);
  34. };
  35. // check the edit list
  36. }
  37. // ~~~ Text shared between components via props can be done with lifetimes! ~~~
  38. // Super duper efficient :)
  39. struct Props<'src> {
  40. blah: bool,
  41. text: &'src str,
  42. }
  43. impl<'src> Properties for Props<'src> {
  44. fn new() -> Self {
  45. todo!()
  46. }
  47. }
  48. fn component<'a>(ctx: &'a Context<Props>) -> VNode<'a> {
  49. // Write asynchronous rendering code that immediately returns a "suspended" VNode
  50. // The concurrent API will then progress this component when the future finishes
  51. // You can suspend the entire component, or just parts of it
  52. let product_list = ctx.suspend(async {
  53. // Suspend the rendering that completes when the future is done
  54. match fetch_data().await {
  55. Ok(data) => html! { <div> </div>},
  56. Err(_) => html! { <div> </div>},
  57. }
  58. });
  59. todo!()
  60. // ctx.view(html! {
  61. // <div>
  62. // // <h1> "Products" </h1>
  63. // // // Subnodes can even be suspended
  64. // // // When completely rendered, they won't cause the component itself to re-render, just their slot
  65. // // <p> { product_list } </p>
  66. // </div>
  67. // })
  68. }
  69. fn BuilderComp<'a>(ctx: &'a Context<'a, Props>) -> VNode<'a> {
  70. // VNodes can be constructed via a builder or the html! macro
  71. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  72. // We can "view" them with Context for ultimate speed while inside components
  73. ctx.view(|bump| {
  74. div(bump)
  75. .attr("class", "edit")
  76. .child(text("Hello"))
  77. .child(text(ctx.props.text))
  78. .finish()
  79. })
  80. }
  81. #[fc]
  82. fn EffcComp(ctx: &Context, name: &str) -> VNode {
  83. // VNodes can be constructed via a builder or the html! macro
  84. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  85. // We can "view" them with Context for ultimate speed while inside components
  86. // use "phase" style allocation;
  87. /*
  88. nodes...
  89. text...
  90. attrs...
  91. <div> // node0
  92. <div> </div> // node1
  93. {// support some expression} // node 2
  94. </div>
  95. let node0;
  96. let node1;
  97. let node2 = evaluate{}.into();
  98. let g= |bump| {1};
  99. g(bump).into()
  100. */
  101. // should we automatically view the output or leave it?
  102. ctx.view(html! {
  103. <div>
  104. // your template goes here
  105. // feel free to directly use "name"
  106. </div>
  107. })
  108. }
  109. fn FullySuspended<'a>(ctx: &'a Context<Props>) -> VNode<'a> {
  110. ctx.suspend(async {
  111. let i: i32 = 0;
  112. // full suspended works great with just returning VNodes!
  113. let tex = match i {
  114. 1 => html! { <div> </div> },
  115. 2 => html! { <div> </div> },
  116. _ => html! { <div> </div> },
  117. };
  118. if ctx.props.blah {
  119. html! { <div> </div> }
  120. } else {
  121. tex
  122. }
  123. })
  124. }
  125. /// An example of a datafetching service
  126. async fn fetch_data() -> Result<String, ()> {
  127. todo!()
  128. }