warp_adapter.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. use crate::events;
  2. use dioxus_core::prelude::*;
  3. use futures_util::{pin_mut, SinkExt, StreamExt};
  4. use tokio::sync::mpsc;
  5. use tokio_stream::wrappers::UnboundedReceiverStream;
  6. use tokio_util::task::LocalPoolHandle;
  7. use warp::ws::{Message, WebSocket};
  8. impl crate::Liveview {
  9. pub async fn upgrade(&self, ws: warp::ws::WebSocket, app: fn(Scope) -> Element) {
  10. connect(ws, self.pool.clone(), app, ()).await;
  11. }
  12. pub async fn upgrade_with_props<T>(&self, ws: warp::ws::WebSocket, app: fn(Scope<T>) -> Element, props: T)
  13. where
  14. T: Send + Sync + 'static
  15. {
  16. connect(ws, self.pool.clone(), app, props).await;
  17. }
  18. }
  19. pub async fn connect<T>(ws: WebSocket, pool: LocalPoolHandle, app: fn(Scope<T>) -> Element, props: T)
  20. where
  21. T: Send + Sync+ 'static
  22. {
  23. // Use a counter to assign a new unique ID for this user.
  24. // Split the socket into a sender and receive of messages.
  25. let (mut user_ws_tx, mut user_ws_rx) = ws.split();
  26. let (event_tx, event_rx) = mpsc::unbounded_channel();
  27. let (edits_tx, edits_rx) = mpsc::unbounded_channel();
  28. let mut edits_rx = UnboundedReceiverStream::new(edits_rx);
  29. let mut event_rx = UnboundedReceiverStream::new(event_rx);
  30. let vdom_fut = pool.spawn_pinned(move || async move {
  31. let mut vdom = VirtualDom::new_with_props(app, props);
  32. let edits = vdom.rebuild();
  33. let serialized = serde_json::to_string(&edits.edits).unwrap();
  34. edits_tx.send(serialized).unwrap();
  35. loop {
  36. use futures_util::future::{select, Either};
  37. let new_event = {
  38. let vdom_fut = vdom.wait_for_work();
  39. pin_mut!(vdom_fut);
  40. match select(event_rx.next(), vdom_fut).await {
  41. Either::Left((l, _)) => l,
  42. Either::Right((_, _)) => None,
  43. }
  44. };
  45. if let Some(new_event) = new_event {
  46. vdom.handle_message(dioxus_core::SchedulerMsg::Event(new_event));
  47. } else {
  48. let mutations = vdom.work_with_deadline(|| false);
  49. for mutation in mutations {
  50. let edits = serde_json::to_string(&mutation.edits).unwrap();
  51. edits_tx.send(edits).unwrap();
  52. }
  53. }
  54. }
  55. });
  56. loop {
  57. use futures_util::future::{select, Either};
  58. match select(user_ws_rx.next(), edits_rx.next()).await {
  59. Either::Left((l, _)) => {
  60. if let Some(Ok(msg)) = l {
  61. if let Ok(Some(msg)) = msg.to_str().map(events::parse_ipc_message) {
  62. if msg.method == "user_event" {
  63. let user_event = events::trigger_from_serialized(msg.params);
  64. event_tx.send(user_event).unwrap();
  65. }
  66. } else {
  67. break;
  68. }
  69. } else {
  70. break;
  71. }
  72. }
  73. Either::Right((edits, _)) => {
  74. if let Some(edits) = edits {
  75. // send the edits to the client
  76. if user_ws_tx.send(Message::text(edits)).await.is_err() {
  77. break;
  78. }
  79. } else {
  80. break;
  81. }
  82. }
  83. }
  84. }
  85. vdom_fut.abort();
  86. }