macrosrc.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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: Context<'a, 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> "success!" </div>},
  56. Err(_) => html! { <div> "failure :(" </div>},
  57. }
  58. });
  59. // todo!()
  60. ctx.view(html! {
  61. <div>
  62. <h1> "Products" </h1>
  63. <button> "hello!" </button>
  64. // Subnodes can even be suspended
  65. // When completely rendered, they won't cause the component itself to re-render, just their slot
  66. // <p> { product_list } </p>
  67. </div>
  68. })
  69. }
  70. fn BuilderComp<'a>(ctx: &'a Context<'a, Props>) -> VNode<'a> {
  71. // VNodes can be constructed via a builder or the html! macro
  72. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  73. // We can "view" them with Context for ultimate speed while inside components
  74. ctx.view(|bump| {
  75. div(bump)
  76. .attr("class", "edit")
  77. .child(text("Hello"))
  78. .child(text(ctx.props.text))
  79. .finish()
  80. })
  81. }
  82. #[fc]
  83. fn EffcComp(ctx: &Context, name: &str) -> VNode {
  84. // VNodes can be constructed via a builder or the html! macro
  85. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  86. // We can "view" them with Context for ultimate speed while inside components
  87. // use "phase" style allocation;
  88. /*
  89. nodes...
  90. text...
  91. attrs...
  92. <div> // node0
  93. <div> </div> // node1
  94. {// support some expression} // node 2
  95. </div>
  96. let node0;
  97. let node1;
  98. let node2 = evaluate{}.into();
  99. let g= |bump| {1};
  100. g(bump).into()
  101. */
  102. // should we automatically view the output or leave it?
  103. ctx.view(html! {
  104. <div>
  105. // your template goes here
  106. // feel free to directly use "name"
  107. </div>
  108. })
  109. }
  110. fn FullySuspended<'a>(ctx: &'a Context<Props>) -> VNode<'a> {
  111. ctx.suspend(async {
  112. let i: i32 = 0;
  113. // full suspended works great with just returning VNodes!
  114. let tex = match i {
  115. 1 => html! { <div> </div> },
  116. 2 => html! { <div> </div> },
  117. _ => html! { <div> </div> },
  118. };
  119. if ctx.props.blah {
  120. html! { <div> </div> }
  121. } else {
  122. tex
  123. }
  124. })
  125. }
  126. /// An example of a datafetching service
  127. async fn fetch_data() -> Result<String, ()> {
  128. todo!()
  129. }