ecs.rs 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //! Example: ECS Architecture for very list selectors
  2. //! --------------------------------------------------
  3. //! Sometimes, you need *peak* performance. Cloning and Rc might simply be too much overhead for your app.
  4. //! If you're building CPU intense apps like graphics editors, simulations, or advanced visualizations,
  5. //! slicing up your state beyond atoms might be desirable.
  6. //!
  7. //! Instead of storing groups of entities in a collection of structs, the ECS Architecture instead stores
  8. //! an array for each field in of a struct. This tends to improve performance for batch operations on
  9. //! individual fields at the cost of complexity. Fortunately, this ECS model is built right into Recoil,
  10. //! making it easier than ever to enable sharded datastructures in your app.
  11. //!
  12. //! Instead of defining a struct for our primary datastructure, we'll instead use a type tuple, and then later
  13. //! index that tuple to get the value we care about. Unfortunately, we lose name information wrt to each
  14. //! type in the type tuple. This can be solved with an associated module, the derive EcsMacro, or just
  15. //! by good documentation.
  16. //!
  17. //! This approach is best suited for applications where individual entries in families are very large
  18. //! and updates to neighbors are costly in terms of Clone or field comparisons for memoization.
  19. use dioxus::prelude::*;
  20. use dioxus_core as dioxus;
  21. use recoil::*;
  22. type TodoModel = (
  23. bool, // checked
  24. String, // name
  25. String, // contents
  26. );
  27. const TODOS: EcsModel<u32, TodoModel> = |builder| {};
  28. // const SELECT_TITLE: SelectorBorrowed<u32, &str> = |s, k| TODOS.field(0).select(k);
  29. // const SELECT_SUBTITLE: SelectorBorrowed<u32, &str> = |s, k| TODOS.field(1).select(k);
  30. static App: FC<()> = |ctx| {
  31. use_init_recoil_root(ctx, |_| {});
  32. // let title = use_recoil_value(ctx, &C_SELECTOR);
  33. let title = "";
  34. rsx! { in ctx,
  35. div {
  36. "{title}"
  37. // button { onclick: {next_light}, "Next light" }
  38. }
  39. }
  40. };
  41. fn main() {
  42. wasm_bindgen_futures::spawn_local(dioxus_web::WebsysRenderer::start(App))
  43. }