|
| 1 | +use std::rc::Rc; |
| 2 | + |
| 3 | +use bitwarden_core::Client; |
| 4 | +use js_sys::Promise; |
| 5 | +use wasm_bindgen::prelude::*; |
| 6 | + |
| 7 | +#[wasm_bindgen] |
| 8 | +pub struct TestClient(Rc<Client>); |
| 9 | + |
| 10 | +impl TestClient { |
| 11 | + pub fn new(client: Rc<Client>) -> Self { |
| 12 | + Self(client) |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +#[wasm_bindgen] |
| 17 | +pub struct TestSub1Client(Rc<Client>); |
| 18 | + |
| 19 | +#[wasm_bindgen] |
| 20 | +pub struct TestSub2Client(Rc<Client>); |
| 21 | + |
| 22 | +#[allow(clippy::unused_async)] |
| 23 | +#[wasm_bindgen] |
| 24 | +impl TestClient { |
| 25 | + pub fn sub1(&self) -> TestSub1Client { |
| 26 | + TestSub1Client(self.0.clone()) |
| 27 | + } |
| 28 | + |
| 29 | + #[allow(clippy::unused_async)] |
| 30 | + pub async fn async_echo(&self, msg: String) -> String { |
| 31 | + format!("ECHOED: '{}'", msg.to_uppercase()) |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +#[allow(clippy::unused_async)] |
| 36 | +#[wasm_bindgen] |
| 37 | +impl TestSub1Client { |
| 38 | + pub fn sub2(&self) -> TestSub2Client { |
| 39 | + TestSub2Client(self.0.clone()) |
| 40 | + } |
| 41 | + |
| 42 | + pub async fn async_echo_cb( |
| 43 | + &self, |
| 44 | + #[wasm_bindgen(unchecked_param_type = "(text: string) => Promise<string>")] |
| 45 | + f: &js_sys::Function, |
| 46 | + ) -> Result<String, JsValue> { |
| 47 | + let this = JsValue::null(); |
| 48 | + let val = f.call1(&this, &"hello".into())?; |
| 49 | + |
| 50 | + let pr: Promise = val.dyn_into()?; |
| 51 | + let fut = wasm_bindgen_futures::JsFuture::from(pr); |
| 52 | + let val = fut |
| 53 | + .await? |
| 54 | + .as_string() |
| 55 | + .ok_or_else(|| js_sys::Error::new("result is not a string"))?; |
| 56 | + |
| 57 | + Ok(format!("Result async: '{}'", val.to_uppercase())) |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +#[allow(clippy::unused_async)] |
| 62 | +#[wasm_bindgen] |
| 63 | +impl TestSub2Client { |
| 64 | + #[allow(clippy::unused_async)] |
| 65 | + pub async fn get_flags(&self) -> String { |
| 66 | + format!("{:?}", self.0.internal.get_flags()) |
| 67 | + } |
| 68 | +} |
0 commit comments