lib

Core libraries for Radroots
git clone https://radroots.dev/git/lib.git
Log | Files | Refs | README

client.rs (4422B)


      1 use super::inputs::bounded_string;
      2 use super::{CommandRequest, Error, InquiryInput, ListingInput, Receipt, SessionState, StartMode};
      3 use js_sys::JsString;
      4 use radroots_domain::{CommandDigest, OperationId};
      5 use wasm_bindgen::prelude::*;
      6 
      7 /// One actual origin-thread client. Foreign calls add no task owner or runtime.
      8 #[derive(Clone)]
      9 #[wasm_bindgen(js_name = Client)]
     10 pub struct BrowserClient {
     11     inner: crate::Client,
     12 }
     13 impl BrowserClient {
     14     /// Injects the explicitly configured actual browser client without starting it.
     15     pub fn from_browser(inner: crate::Client) -> Self {
     16         Self { inner }
     17     }
     18 }
     19 /// Actual authenticated shared scope, never constructed from a JS descriptor.
     20 #[derive(Clone)]
     21 #[wasm_bindgen]
     22 pub struct Workspace {
     23     inner: crate::WorkspaceSession,
     24 }
     25 impl Workspace {
     26     /// Injects an actual shared-client attachment; ownership is rechecked on use.
     27     pub fn from_session(inner: crate::WorkspaceSession) -> Self {
     28         Self { inner }
     29     }
     30 }
     31 #[wasm_bindgen]
     32 impl Workspace {
     33     /// Releases the attachment credential root without revoking the grant.
     34     pub fn close(&self) {
     35         self.inner.close();
     36     }
     37 }
     38 #[wasm_bindgen(js_class = Client)]
     39 impl BrowserClient {
     40     pub fn state(&self) -> SessionState {
     41         self.inner.state().into()
     42     }
     43     pub async fn start(&self, mode: StartMode) -> Result<SessionState, JsValue> {
     44         self.inner
     45             .start(mode.into())
     46             .await
     47             .map(Into::into)
     48             .map_err(|v| Error::from(v).into())
     49     }
     50     /// Resolves only after actual owned tasks, callbacks and storage close.
     51     pub async fn close(&self) -> Result<(), JsValue> {
     52         self.inner
     53             .close()
     54             .await
     55             .map(|_| ())
     56             .map_err(|v| Error::from(v).into())
     57     }
     58     pub async fn publish_listing(
     59         &self,
     60         workspace: &Workspace,
     61         request: &CommandRequest,
     62         input: &ListingInput,
     63     ) -> Result<Receipt, JsValue> {
     64         let context = request.capture(&workspace.inner)?;
     65         let value = self
     66             .inner
     67             .listings()
     68             .publish(context, input.inner.clone())
     69             .await
     70             .map_err(Error::from)?;
     71         Receipt::listing(&value).map_err(Into::into)
     72     }
     73     pub async fn inquire(
     74         &self,
     75         workspace: &Workspace,
     76         request: &CommandRequest,
     77         input: &InquiryInput,
     78     ) -> Result<Receipt, JsValue> {
     79         let context = request.capture(&workspace.inner)?;
     80         let value = self
     81             .inner
     82             .inbox()
     83             .inquire(context, input.captured_copy()?)
     84             .await
     85             .map_err(Error::from)?;
     86         Receipt::chat(&value).map_err(Into::into)
     87     }
     88     /// Scoped observation; absence never proves safe nonissuance.
     89     pub async fn operation(
     90         &self,
     91         workspace: &Workspace,
     92         operation_id: JsString,
     93         expected_digest: Option<JsString>,
     94     ) -> Result<Option<Receipt>, JsValue> {
     95         let (id, expected) = selector(operation_id, expected_digest)?;
     96         self.inner
     97             .operations(&workspace.inner)
     98             .get_matching(id, expected)
     99             .await
    100             .map_err(Error::from)?
    101             .as_ref()
    102             .map(Receipt::status)
    103             .transpose()
    104             .map_err(Into::into)
    105     }
    106     /// Explicitly continues original retained work, without reissuing a claim.
    107     pub async fn resume(
    108         &self,
    109         workspace: &Workspace,
    110         operation_id: JsString,
    111         expected_digest: Option<JsString>,
    112     ) -> Result<Receipt, JsValue> {
    113         let (id, expected) = selector(operation_id, expected_digest)?;
    114         let value = self
    115             .inner
    116             .operations(&workspace.inner)
    117             .resume(id, expected)
    118             .await
    119             .map_err(Error::from)?;
    120         Receipt::status(&value).map_err(Into::into)
    121     }
    122 }
    123 fn selector(
    124     operation: JsString,
    125     expected: Option<JsString>,
    126 ) -> Result<(OperationId, Option<CommandDigest>), Error> {
    127     let operation = bounded_string(operation, 64, "operation_id")?
    128         .parse()
    129         .map_err(|_| Error::input("operation_id"))?;
    130     let expected = expected
    131         .map(|v| {
    132             bounded_string(v, 64, "command_digest")
    133                 .and_then(|v| v.parse().map_err(|_| Error::input("command_digest")))
    134         })
    135         .transpose()?;
    136     Ok((operation, expected))
    137 }