Skip to content

Commit

Permalink
Add timeout param to get_device and get_user_devices (#60)
Browse files Browse the repository at this point in the history
  • Loading branch information
richvdh authored Nov 30, 2023
1 parent f4df1a6 commit 8fe1dfa
Showing 1 changed file with 41 additions and 10 deletions.
51 changes: 41 additions & 10 deletions src/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -691,41 +691,72 @@ impl OlmMachine {

/// Get a map holding all the devices of a user.
///
/// `user_id` represents the unique ID of the user that the
/// devices belong to.
/// ### Parameters
///
/// * `user_id` - The unique ID of the user that the device belongs to.
///
/// * `timeout_secs` - The amount of time we should wait for a `/keys/query`
/// response before returning if the user's device list has been marked as
/// stale. **Note**, this assumes that the requests from {@link
/// OlmMachine.outgoingRequests} are being processed and sent out.
///
/// If unset, we will return immediately even if the device list is stale.
///
/// ### Returns
///
/// A {@link UserDevices} object.
#[wasm_bindgen(js_name = "getUserDevices")]
pub fn get_user_devices(&self, user_id: &identifiers::UserId) -> Promise {
pub fn get_user_devices(
&self,
user_id: &identifiers::UserId,
timeout_secs: Option<f64>,
) -> Promise {
let user_id = user_id.inner.clone();
let timeout_duration = timeout_secs.map(Duration::from_secs_f64);

let me = self.inner.clone();

future_to_promise::<_, device::UserDevices>(async move {
// wait for up to a second for any in-flight device list requests to complete.
// The reason for this isn't so much to avoid races (some level of raciness is
// inevitable for this method) but to make testing easier.
Ok(me.get_user_devices(&user_id, Some(Duration::from_secs(1))).await.map(Into::into)?)
Ok(me.get_user_devices(&user_id, timeout_duration).await.map(Into::into)?)
})
}

/// Get a specific device of a user if one is found and the crypto store
/// didn't throw an error.
/// Get a specific device of a user.
///
/// ### Parameters
///
/// * `user_id` - The unique ID of the user that the device belongs to.
///
/// * `device_id` - The unique ID of the device.
///
/// * `timeout_secs` - The amount of time we should wait for a `/keys/query`
/// response before returning if the user's device list has been marked as
/// stale. **Note**, this assumes that the requests from {@link
/// OlmMachine.outgoingRequests} are being processed and sent out.
///
/// If unset, we will return immediately even if the device list is stale.
///
/// ### Returns
///
/// `user_id` represents the unique ID of the user that the
/// identity belongs to. `device_id` represents the unique ID of
/// the device.
/// If the device is known, a {@link Device}. Otherwise, `undefined`.
#[wasm_bindgen(js_name = "getDevice")]
pub fn get_device(
&self,
user_id: &identifiers::UserId,
device_id: &identifiers::DeviceId,
timeout_secs: Option<f64>,
) -> Promise {
let user_id = user_id.inner.clone();
let device_id = device_id.inner.clone();
let timeout_duration = timeout_secs.map(Duration::from_secs_f64);

let me = self.inner.clone();

future_to_promise::<_, Option<device::Device>>(async move {
Ok(me.get_device(&user_id, &device_id, None).await?.map(Into::into))
Ok(me.get_device(&user_id, &device_id, timeout_duration).await?.map(Into::into))
})
}

Expand Down

0 comments on commit 8fe1dfa

Please sign in to comment.