From 55f308363dfcf8868b16420b5593d2f95f3a3913 Mon Sep 17 00:00:00 2001 From: Evan Almloff Date: Thu, 29 Feb 2024 10:39:02 -0600 Subject: [PATCH] expose non-blocking functions to read and write to the fullstack request --- packages/fullstack/src/render.rs | 2 +- packages/fullstack/src/server_context.rs | 28 +++++++++++++++++++++--- packages/router/src/router_cfg.rs | 2 +- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/packages/fullstack/src/render.rs b/packages/fullstack/src/render.rs index adb38ac4c4..42b6395eeb 100644 --- a/packages/fullstack/src/render.rs +++ b/packages/fullstack/src/render.rs @@ -9,8 +9,8 @@ use dioxus_ssr::{ use std::future::Future; use std::sync::Arc; use std::sync::RwLock; -use tokio::task::JoinHandle; use tokio::task::block_in_place; +use tokio::task::JoinHandle; use crate::prelude::*; use dioxus_lib::prelude::*; diff --git a/packages/fullstack/src/server_context.rs b/packages/fullstack/src/server_context.rs index a2cbfeb55a..7ddc67f3b8 100644 --- a/packages/fullstack/src/server_context.rs +++ b/packages/fullstack/src/server_context.rs @@ -84,14 +84,36 @@ mod server_fn_impl { /// Get the request that triggered: /// - The initial SSR render if called from a ScopeState or ServerFn /// - The server function to be called if called from a server function after the initial render - pub fn request_parts(&self) -> tokio::sync::RwLockReadGuard<'_, http::request::Parts> { + pub async fn request_parts( + &self, + ) -> tokio::sync::RwLockReadGuard<'_, http::request::Parts> { + self.parts.read().await + } + + /// Get the request that triggered: + /// - The initial SSR render if called from a ScopeState or ServerFn + /// - The server function to be called if called from a server function after the initial render + pub fn request_parts_blocking( + &self, + ) -> tokio::sync::RwLockReadGuard<'_, http::request::Parts> { self.parts.blocking_read() } /// Get the request that triggered: /// - The initial SSR render if called from a ScopeState or ServerFn /// - The server function to be called if called from a server function after the initial render - pub fn request_parts_mut(&self) -> tokio::sync::RwLockWriteGuard<'_, http::request::Parts> { + pub async fn request_parts_mut( + &self, + ) -> tokio::sync::RwLockWriteGuard<'_, http::request::Parts> { + self.parts.write().await + } + + /// Get the request that triggered: + /// - The initial SSR render if called from a ScopeState or ServerFn + /// - The server function to be called if called from a server function after the initial render + pub fn request_parts_mut_blocking( + &self, + ) -> tokio::sync::RwLockWriteGuard<'_, http::request::Parts> { self.parts.blocking_write() } @@ -239,6 +261,6 @@ impl< type Rejection = R; async fn from_request(req: &DioxusServerContext) -> Result { - Ok(I::from_request_parts(&mut req.request_parts_mut(), &()).await?) + Ok(I::from_request_parts(&mut *req.request_parts_mut().await, &()).await?) } } diff --git a/packages/router/src/router_cfg.rs b/packages/router/src/router_cfg.rs index 8ec762b141..f4e9177d35 100644 --- a/packages/router/src/router_cfg.rs +++ b/packages/router/src/router_cfg.rs @@ -134,7 +134,7 @@ where return Box::new(AnyHistoryProviderImplWrapper::new( MemoryHistory::::with_initial_path( dioxus_fullstack::prelude::server_context() - .request_parts() + .request_parts_blocking() .uri .to_string() .parse()