alloy_pubsub/managers/
req.rs

1use crate::managers::InFlight;
2use alloy_json_rpc::{Id, Response, SubId};
3use alloy_primitives::map::HashMap;
4
5/// Manages in-flight requests.
6#[derive(Debug, Default)]
7pub(crate) struct RequestManager {
8    reqs: HashMap<Id, InFlight>,
9}
10
11impl RequestManager {
12    /// Get the number of in-flight requests.
13    pub(crate) fn len(&self) -> usize {
14        self.reqs.len()
15    }
16
17    /// Get an iterator over the in-flight requests.
18    pub(crate) fn iter(&self) -> impl Iterator<Item = (&Id, &InFlight)> {
19        self.reqs.iter()
20    }
21
22    /// Insert a new in-flight request.
23    pub(crate) fn insert(&mut self, in_flight: InFlight) {
24        self.reqs.insert(in_flight.request.id().clone(), in_flight);
25    }
26
27    /// Handle a response by sending the payload to the waiter.
28    ///
29    /// If the request created a new subscription, this function returns the
30    /// subscription ID and the in-flight request for conversion to an
31    /// `ActiveSubscription`.
32    pub(crate) fn handle_response(&mut self, resp: Response) -> Option<(SubId, InFlight)> {
33        if let Some(in_flight) = self.reqs.remove(&resp.id) {
34            return in_flight.fulfill(resp);
35        }
36        None
37    }
38}