pub trait ServerFn<T: 'static>where
Self: Serialize + DeserializeOwned + Sized + 'static,{
type Output: Serialize;
// Required methods
fn prefix() -> &'static str;
fn url() -> &'static str;
fn encoding() -> Encoding;
fn call_fn(
self,
cx: T
) -> Pin<Box<dyn Future<Output = Result<Self::Output, ServerFnError>>>>;
fn call_fn_client(
self,
cx: T
) -> Pin<Box<dyn Future<Output = Result<Self::Output, ServerFnError>>>>;
// Provided method
fn register_in<R: ServerFunctionRegistry<T>>() -> Result<(), ServerFnError> { ... }
}
Expand description
Defines a “server function.” A server function can be called from the server or the client,
but the body of its code will only be run on the server, i.e., if a crate feature ssr
(server-side-rendering) is enabled.
Server functions are created using the server
macro.
The function should be registered by calling ServerFn::register()
. The set of server functions
can be queried on the server for routing purposes by calling server_fn_by_path.
Technically, the trait is implemented on a type that describes the server function’s arguments.
Required Associated Types§
Required Methods§
sourcefn prefix() -> &'static str
fn prefix() -> &'static str
URL prefix that should be prepended by the client to the generated URL.
sourcefn call_fn(
self,
cx: T
) -> Pin<Box<dyn Future<Output = Result<Self::Output, ServerFnError>>>>
fn call_fn( self, cx: T ) -> Pin<Box<dyn Future<Output = Result<Self::Output, ServerFnError>>>>
Runs the function on the server.
sourcefn call_fn_client(
self,
cx: T
) -> Pin<Box<dyn Future<Output = Result<Self::Output, ServerFnError>>>>
fn call_fn_client( self, cx: T ) -> Pin<Box<dyn Future<Output = Result<Self::Output, ServerFnError>>>>
Runs the function on the client by sending an HTTP request to the server.
Provided Methods§
sourcefn register_in<R: ServerFunctionRegistry<T>>() -> Result<(), ServerFnError>
fn register_in<R: ServerFunctionRegistry<T>>() -> Result<(), ServerFnError>
Registers the server function, allowing the server to query it by URL.