live.rs 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. use dioxus::prelude::*;
  2. use tide::{with_state, Request};
  3. use tide_websockets::{WebSocket, WebSocketConnection};
  4. fn main() {
  5. let mut g = Context { props: &() };
  6. LiveComponent(&mut g);
  7. }
  8. #[cfg(not(target_arch = "wasm32"))]
  9. async fn server() -> tide::Result<()> {
  10. // Build the API
  11. let mut app = tide::with_state(());
  12. app.at("/app").get(WebSocket::new(live_handler));
  13. // Launch the server
  14. app.listen("127.0.0.1:8080").await?;
  15. Ok(())
  16. }
  17. async fn live_handler(req: Request<()>, stream: WebSocketConnection) -> tide::Result<()> {
  18. Ok(())
  19. }
  20. static LiveComponent: FC<()> = |ctx| {
  21. use_live_component(
  22. ctx,
  23. #[cfg(target_arch = "wasm32")]
  24. || {
  25. // Always wait on the context's live component API
  26. // suspend the component until this promise arrives, or fall back
  27. let g = &LiveComponent;
  28. html! {
  29. <div>
  30. {"Hello world!"}
  31. </div>
  32. }
  33. },
  34. #[cfg(not(target_arch = "wasm32"))]
  35. || {
  36. // actually use the code originally specified in the component
  37. // this gives use the function pointer. We don't necessarily get to hash the same pointer between two binaries
  38. // Some key will need to be made, likely based on the function parameter
  39. let g = &LiveComponent;
  40. html! {
  41. <div>
  42. {"Hello world!"}
  43. </div>
  44. }
  45. },
  46. )
  47. };
  48. /// This hooks connects with the LiveContext at the top of the app.
  49. fn use_live_component<T>(ctx: &mut Context<T>, b: fn() -> VNode) -> VNode {
  50. todo!()
  51. }
  52. /// LiveContext is a special, limited form of the context api that disallows the "Context API"
  53. /// Its purpose is to shield the original Context where calls to use_context will fail. Instead of panicing and confusing
  54. /// users, we simply disallow usage of "use_context" and "childen". In effect, only serialiable props can be allowed.
  55. ///
  56. /// In the future, we might try to lift these restrictions (esp children since they are virtual) but are limited via the web connection
  57. struct LiveContext {}