alloy_pubsub/
connect.rs

1use crate::{handle::ConnectionHandle, service::PubSubService, PubSubFrontend};
2use alloy_transport::{impl_future, TransportResult};
3
4/// Configuration objects that contain connection details for a backend.
5///
6/// Implementers should contain configuration options for the underlying
7/// transport.
8pub trait PubSubConnect: Sized + Send + Sync + 'static {
9    /// Returns `true` if the transport connects to a local resource.
10    fn is_local(&self) -> bool;
11
12    /// Spawn the backend, returning a handle to it.
13    ///
14    /// This function MUST create a long-lived task containing a
15    /// [`ConnectionInterface`], and return the corresponding handle.
16    ///
17    /// [`ConnectionInterface`]: crate::ConnectionInterface
18    fn connect(&self) -> impl_future!(<Output = TransportResult<ConnectionHandle>>);
19
20    /// Attempt to reconnect the transport.
21    ///
22    /// Override this to add custom reconnection logic to your connector. This
23    /// will be used by the internal pubsub connection managers in the event the
24    /// connection fails.
25    fn try_reconnect(&self) -> impl_future!(<Output = TransportResult<ConnectionHandle>>) {
26        self.connect()
27    }
28
29    /// Convert the configuration object into a service with a running backend.
30    fn into_service(self) -> impl_future!(<Output = TransportResult<PubSubFrontend>>) {
31        PubSubService::connect(self)
32    }
33}