wallet_adapter/wallet_ser_der/standard_features/
disconnect.rs

1use web_sys::{js_sys, wasm_bindgen::JsValue};
2
3use crate::{Reflection, SemverVersion, StandardFunction, WalletError, WalletResult};
4
5/// `standard:disconnect` struct containing the `version` and `callback`
6/// in the field [StandardFunction]
7#[derive(Debug, Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub struct Disconnect(StandardFunction);
9
10impl Disconnect {
11    /// Parse the `standard:disconnect` callback from the [JsValue]
12    pub(crate) fn new(reflection: &Reflection, version: SemverVersion) -> WalletResult<Self> {
13        Ok(Self(StandardFunction::new(
14            reflection,
15            version,
16            "disconnect",
17            "standard",
18        )?))
19    }
20
21    /// Calling this method disconnects the wallet by internally calling the
22    /// callback function
23    pub(crate) async fn call_disconnect(&self) -> WalletResult<()> {
24        let outcome = self.0.callback.call0(&JsValue::null())?;
25
26        let outcome = js_sys::Promise::resolve(&outcome);
27
28        if let Some(error) = wasm_bindgen_futures::JsFuture::from(outcome).await.err() {
29            let value: WalletError = error.into();
30            return Err(WalletError::WalletDisconnectError(value.to_string()));
31        }
32
33        Ok(())
34    }
35}