use std::time::Instant;
use tokio::sync::oneshot;
use crate::protocol::{JSONRPCMessage, RequestId};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ConnectionState {
Disconnected,
Connecting,
Initializing,
Initialized,
}
pub(crate) struct ClientState {
connection_state: ConnectionState,
}
pub(crate) struct PendingRequest {
pub sender: oneshot::Sender<JSONRPCMessage>,
pub method: String,
pub start_time: Instant,
}
impl ClientState {
pub fn new() -> Self {
Self {
connection_state: ConnectionState::Disconnected,
}
}
pub fn connection_state(&self) -> ConnectionState {
self.connection_state
}
pub fn set_connecting(&mut self) {
self.connection_state = ConnectionState::Connecting;
}
pub fn set_initializing(&mut self) {
self.connection_state = ConnectionState::Initializing;
}
pub fn set_initialized(&mut self) {
self.connection_state = ConnectionState::Initialized;
}
pub fn set_disconnected(&mut self) {
self.connection_state = ConnectionState::Disconnected;
}
pub fn is_disconnected(&self) -> bool {
self.connection_state == ConnectionState::Disconnected
}
pub fn is_connecting(&self) -> bool {
self.connection_state == ConnectionState::Connecting
}
pub fn is_initializing(&self) -> bool {
self.connection_state == ConnectionState::Initializing
}
pub fn is_initialized(&self) -> bool {
self.connection_state == ConnectionState::Initialized
}
}
impl Default for ClientState {
fn default() -> Self {
Self::new()
}
}