macrosrc.rs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. #![allow(unused, non_upper_case_globals, non_snake_case)]
  2. use bumpalo::Bump;
  3. use dioxus_core::nodebuilder::*;
  4. use dioxus_core::prelude::*;
  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".into(),
  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 {
  40. blah: bool,
  41. text: String,
  42. // text: &'src str,
  43. }
  44. impl Properties for Props {
  45. fn new() -> Self {
  46. todo!()
  47. }
  48. }
  49. fn component<'a>(ctx: Context<'a, Props>) -> VNode<'a> {
  50. // Write asynchronous rendering code that immediately returns a "suspended" VNode
  51. // The concurrent API will then progress this component when the future finishes
  52. // You can suspend the entire component, or just parts of it
  53. let product_list = ctx.suspend(async {
  54. // Suspend the rendering that completes when the future is done
  55. match fetch_data().await {
  56. Ok(data) => html! { <div> "success!" </div>},
  57. Err(_) => html! { <div> "failure :(" </div>},
  58. }
  59. });
  60. // todo!()
  61. ctx.view(html! {
  62. <div>
  63. <h1> "Products" </h1>
  64. <button> "hello!" </button>
  65. // Subnodes can even be suspended
  66. // When completely rendered, they won't cause the component itself to re-render, just their slot
  67. // <p> { product_list } </p>
  68. </div>
  69. })
  70. }
  71. fn BuilderComp<'a>(ctx: &'a Context<'a, Props>) -> VNode<'a> {
  72. // VNodes can be constructed via a builder or the html! macro
  73. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  74. // We can "view" them with Context for ultimate speed while inside components
  75. ctx.view(|bump| {
  76. div(bump)
  77. .attr("class", "edit")
  78. .child(text("Hello"))
  79. .child(text(ctx.props.text.as_ref()))
  80. .finish()
  81. })
  82. }
  83. #[fc]
  84. fn EffcComp(ctx: &Context, name: &str) -> VNode {
  85. // VNodes can be constructed via a builder or the html! macro
  86. // However, both of these are "lazy" - they need to be evaluated (aka, "viewed")
  87. // We can "view" them with Context for ultimate speed while inside components
  88. // use "phase" style allocation;
  89. ctx.view(html! {
  90. <div>
  91. // your template goes here
  92. // feel free to directly use "name"
  93. </div>
  94. })
  95. }
  96. fn FullySuspended<'a>(ctx: &'a Context<Props>) -> VNode<'a> {
  97. ctx.suspend(async {
  98. let i: i32 = 0;
  99. // full suspended works great with just returning VNodes!
  100. // Feel free to capture the html! macro directly
  101. // Anything returned here is automatically viewed
  102. let tex = match i {
  103. 1 => html! { <div> </div> },
  104. 2 => html! { <div> </div> },
  105. _ => html! { <div> </div> },
  106. };
  107. if ctx.props.blah {
  108. html! { <div> </div> }
  109. } else {
  110. tex
  111. }
  112. })
  113. }
  114. /// An example of a datafetching service
  115. async fn fetch_data() -> Result<String, ()> {
  116. todo!()
  117. }