hitbox_actix/
handlers.rs

1//! Actix Handler<QueryCache> implementation.
2
3use crate::{ActixAdapter, CacheActor, QueryCache};
4use actix::{
5    dev::{MessageResponse, ResponseFuture, ToEnvelope},
6    prelude::*,
7};
8use hitbox::states::initial::Initial;
9use hitbox::{
10    dev::{Backend, Delete, Get, Lock, Set},
11    CacheError, Cacheable, CacheableResponse,
12};
13use serde::{de::DeserializeOwned, Serialize};
14
15impl<'a, A, M, B> Handler<QueryCache<A, M>> for CacheActor<B>
16where
17    B: Actor + Backend,
18    <B as Actor>::Context:
19        ToEnvelope<B, Get> + ToEnvelope<B, Set> + ToEnvelope<B, Lock> + ToEnvelope<B, Delete>,
20    A: Actor + Handler<M> + Send,
21    M: Message + Cacheable + Send + 'static,
22    M::Result: MessageResponse<A, M> + CacheableResponse + std::fmt::Debug + Send,
23    <<M as actix::Message>::Result as CacheableResponse>::Cached: Serialize + DeserializeOwned,
24    <A as Actor>::Context: ToEnvelope<A, M>,
25{
26    type Result = ResponseFuture<Result<<M as Message>::Result, CacheError>>;
27
28    fn handle(&mut self, msg: QueryCache<A, M>, _: &mut Self::Context) -> Self::Result {
29        let adapter_result = ActixAdapter::new(msg, self.backend.clone()); // @TODO: remove clone
30        let settings = self.settings.clone();
31        Box::pin(async move {
32            let initial_state = Initial::new(settings, adapter_result?);
33            initial_state.transitions().await
34        })
35    }
36}