Skip to main content

codetether_browser/browser/session/
state.rs

1//! Shared browser session state.
2//!
3//! The public session handle is cloneable while the backend-specific runtime
4//! state remains behind internal synchronization.
5
6use std::sync::Arc;
7use tokio::sync::Mutex;
8
9/// Cloneable handle for executing browser commands.
10///
11/// # Examples
12///
13/// ```rust
14/// use codetether_browser::BrowserSession;
15///
16/// let session = BrowserSession::new();
17/// let clone = session.clone();
18/// drop(clone);
19/// ```
20#[derive(Clone, Default)]
21pub struct BrowserSession {
22    pub(super) inner: Arc<SessionInner>,
23}
24
25/// Internal backend state stored by a browser session.
26#[derive(Default)]
27pub(super) struct SessionInner {
28    #[cfg(feature = "tetherscript")]
29    pub native: Mutex<Option<super::native::NativeRuntime>>,
30}
31
32impl BrowserSession {
33    /// Create an empty browser session handle.
34    ///
35    /// # Examples
36    ///
37    /// ```rust
38    /// use codetether_browser::BrowserSession;
39    ///
40    /// let session = BrowserSession::new();
41    /// let _ = session.clone();
42    /// ```
43    pub fn new() -> Self {
44        Self::default()
45    }
46
47    /// Execute a browser command against this session.
48    ///
49    /// # Errors
50    ///
51    /// Returns [`crate::browser::BrowserError`] when the backend is not started
52    /// or the requested command cannot be completed.
53    pub async fn execute(
54        &self,
55        command: crate::browser::BrowserCommand,
56    ) -> Result<crate::browser::BrowserOutput, crate::browser::BrowserError> {
57        super::runtime::execute(self, command).await
58    }
59}