pub struct Dispatcher<CallerCtx> { /* private fields */ }Expand description
Dispatches a JmapRequest to registered method handlers.
Register handlers with Dispatcher::register, then call
Dispatcher::dispatch per request. CallerCtx is cloned for each
method call in the batch, so it must be Clone.
CallerCtx must also be 'static because each handler call is spawned as
a tokio::task. To share non-static data (e.g. a database connection),
wrap it in Arc<T> — Arc is Clone + Send + 'static when T: Send + Sync.
§Thread safety
Dispatcher is both Send and Sync. Register handlers on one thread,
then wrap in Arc and share across tasks — dispatch takes &self and is
safe to call concurrently.
Implementations§
Source§impl<CallerCtx: Clone + Send + 'static> Dispatcher<CallerCtx>
impl<CallerCtx: Clone + Send + 'static> Dispatcher<CallerCtx>
Sourcepub fn register(
&mut self,
method: impl Into<String>,
handler: Arc<dyn JmapHandler<CallerCtx>>,
)
pub fn register( &mut self, method: impl Into<String>, handler: Arc<dyn JmapHandler<CallerCtx>>, )
Register a handler for the given method name.
Registering the same name twice replaces the earlier handler.
Using Arc rather than Box allows the same handler instance to be
shared across multiple method name registrations (via Arc::clone).
Sourcepub async fn dispatch(
&self,
request: JmapRequest,
caller: CallerCtx,
session_state: State,
) -> JmapResponse
pub async fn dispatch( &self, request: JmapRequest, caller: CallerCtx, session_state: State, ) -> JmapResponse
Process a validated JmapRequest and return a JmapResponse.
Method calls are processed sequentially per RFC 8620 §3.3. Each
handler runs in a tokio::task::spawn for panic isolation: a panicking
handler returns a serverFail invocation rather than crashing the
connection task.
CallerCtx must be Clone + Send + 'static; see the struct-level doc.
§Cancellation
If this future is dropped while a handler task is running (e.g., the
HTTP connection closes), the spawned task runs to completion — tokio
does not cancel tasks when their JoinHandle is dropped. The handler
result is discarded. Callers that need strict cancellation should
implement it at the handler level (e.g., tokio::select! with a
shutdown signal).