1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
pub mod routes;
use bee_api_types::responses::OutputResponse;
use bee_block::output::OutputId;
#[cfg(not(target_family = "wasm"))]
use crate::constants::MAX_PARALLEL_API_REQUESTS;
use crate::{Client, Result};
impl Client {
pub async fn get_outputs(&self, output_ids: Vec<OutputId>) -> Result<Vec<OutputResponse>> {
let mut outputs = Vec::new();
#[cfg(target_family = "wasm")]
for output_id in output_ids {
outputs.push(self.get_output(&output_id).await?);
}
#[cfg(not(target_family = "wasm"))]
for output_ids_chunk in output_ids.chunks(MAX_PARALLEL_API_REQUESTS).map(<[OutputId]>::to_vec) {
let mut tasks = Vec::new();
for output_id in output_ids_chunk {
let client_ = self.clone();
tasks.push(async move {
tokio::spawn(async move {
let output_response = client_.get_output(&output_id).await?;
crate::Result::Ok(output_response)
})
.await
});
}
for res in futures::future::try_join_all(tasks).await? {
let output_response = res?;
outputs.push(output_response);
}
}
Ok(outputs)
}
pub async fn try_get_outputs(&self, output_ids: Vec<OutputId>) -> Result<Vec<OutputResponse>> {
let mut outputs = Vec::new();
#[cfg(target_family = "wasm")]
for output_id in output_ids {
if let Ok(output_response) = self.get_output(&output_id).await {
outputs.push(output_response);
}
}
#[cfg(not(target_family = "wasm"))]
for output_ids_chunk in output_ids.chunks(MAX_PARALLEL_API_REQUESTS).map(<[OutputId]>::to_vec) {
let mut tasks = Vec::new();
for output_id in output_ids_chunk {
let client_ = self.clone();
tasks.push(async move {
tokio::spawn(async move {
if let Ok(output_response) = client_.get_output(&output_id).await {
Some(output_response)
} else {
None
}
})
.await
});
}
for output_response in (futures::future::try_join_all(tasks).await?).into_iter().flatten() {
outputs.push(output_response);
}
}
Ok(outputs)
}
}