server_fn.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. use crate::server_context::DioxusServerContext;
  2. #[cfg(any(feature = "ssr", doc))]
  3. /// A trait object for a function that be called on serializable arguments and returns a serializable result.
  4. pub type ServerFnTraitObj = server_fn::ServerFnTraitObj<DioxusServerContext>;
  5. #[cfg(any(feature = "ssr", doc))]
  6. /// A server function that can be called on serializable arguments and returns a serializable result.
  7. pub type ServerFunction = server_fn::ServerFunction<DioxusServerContext>;
  8. #[cfg(any(feature = "ssr", doc))]
  9. #[allow(clippy::type_complexity)]
  10. static REGISTERED_SERVER_FUNCTIONS: once_cell::sync::Lazy<
  11. std::sync::Arc<std::sync::RwLock<std::collections::HashMap<&'static str, ServerFunction>>>,
  12. > = once_cell::sync::Lazy::new(Default::default);
  13. #[cfg(any(feature = "ssr", doc))]
  14. /// The registry of all Dioxus server functions.
  15. pub struct DioxusServerFnRegistry;
  16. #[cfg(any(feature = "ssr"))]
  17. impl server_fn::ServerFunctionRegistry<DioxusServerContext> for DioxusServerFnRegistry {
  18. type Error = ServerRegistrationFnError;
  19. fn register(
  20. url: &'static str,
  21. server_function: std::sync::Arc<ServerFnTraitObj>,
  22. encoding: server_fn::Encoding,
  23. ) -> Result<(), Self::Error> {
  24. // store it in the hashmap
  25. let mut write = REGISTERED_SERVER_FUNCTIONS
  26. .write()
  27. .map_err(|e| ServerRegistrationFnError::Poisoned(e.to_string()))?;
  28. let prev = write.insert(
  29. url,
  30. ServerFunction {
  31. trait_obj: server_function,
  32. encoding,
  33. },
  34. );
  35. // if there was already a server function with this key,
  36. // return Err
  37. match prev {
  38. Some(_) => Err(ServerRegistrationFnError::AlreadyRegistered(format!(
  39. "There was already a server function registered at {:?}. \
  40. This can happen if you use the same server function name \
  41. in two different modules
  42. on `stable` or in `release` mode.",
  43. url
  44. ))),
  45. None => Ok(()),
  46. }
  47. }
  48. /// Returns the server function registered at the given URL, or `None` if no function is registered at that URL.
  49. fn get(url: &str) -> Option<ServerFunction> {
  50. REGISTERED_SERVER_FUNCTIONS
  51. .read()
  52. .ok()
  53. .and_then(|fns| fns.get(url).cloned())
  54. }
  55. /// Returns the server function registered at the given URL, or `None` if no function is registered at that URL.
  56. fn get_trait_obj(url: &str) -> Option<std::sync::Arc<ServerFnTraitObj>> {
  57. REGISTERED_SERVER_FUNCTIONS
  58. .read()
  59. .ok()
  60. .and_then(|fns| fns.get(url).map(|f| f.trait_obj.clone()))
  61. }
  62. fn get_encoding(url: &str) -> Option<server_fn::Encoding> {
  63. REGISTERED_SERVER_FUNCTIONS
  64. .read()
  65. .ok()
  66. .and_then(|fns| fns.get(url).map(|f| f.encoding.clone()))
  67. }
  68. /// Returns a list of all registered server functions.
  69. fn paths_registered() -> Vec<&'static str> {
  70. REGISTERED_SERVER_FUNCTIONS
  71. .read()
  72. .ok()
  73. .map(|fns| fns.keys().cloned().collect())
  74. .unwrap_or_default()
  75. }
  76. }
  77. #[cfg(any(feature = "ssr", doc))]
  78. /// Errors that can occur when registering a server function.
  79. #[derive(thiserror::Error, Debug, Clone, serde::Serialize, serde::Deserialize)]
  80. pub enum ServerRegistrationFnError {
  81. /// The server function is already registered.
  82. #[error("The server function {0} is already registered")]
  83. AlreadyRegistered(String),
  84. /// The server function registry is poisoned.
  85. #[error("The server function registry is poisoned: {0}")]
  86. Poisoned(String),
  87. }
  88. /// Defines a "server function." A server function can be called from the server or the client,
  89. /// but the body of its code will only be run on the server, i.e., if a crate feature `ssr` is enabled.
  90. ///
  91. /// Server functions are created using the `server` macro.
  92. ///
  93. /// The function should be registered by calling `ServerFn::register()`. The set of server functions
  94. /// can be queried on the server for routing purposes by calling [server_fn::ServerFunctionRegistry::get].
  95. ///
  96. /// Technically, the trait is implemented on a type that describes the server function's arguments, not the function itself.
  97. pub trait ServerFn: server_fn::ServerFn<DioxusServerContext> {
  98. /// Registers the server function, allowing the client to query it by URL.
  99. #[cfg(any(feature = "ssr", doc))]
  100. fn register() -> Result<(), server_fn::ServerFnError> {
  101. Self::register_in::<DioxusServerFnRegistry>()
  102. }
  103. }
  104. impl<T> ServerFn for T where T: server_fn::ServerFn<DioxusServerContext> {}