Skip to content

Commit 0f995e4

Browse files
committed
feat: include chunk number in download report
1 parent eac4adc commit 0f995e4

File tree

5 files changed

+33
-52
lines changed

5 files changed

+33
-52
lines changed

example/gauge/Components/Pages/Auth/Auth.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ export class AuthPage extends DisplayComponent<AuthPageProps> {
2828
super(props);
2929

3030
this.props.navigationDataInterface.onEvent(NavigraphEventType.DownloadProgress, data => {
31-
this.displayMessage(`Downloaded ${data.downloaded_bytes}/${data.total_bytes} bytes`);
31+
this.displayMessage(
32+
`Downloaded ${data.downloaded_bytes}/${data.total_bytes} bytes (chunk ${data.current_chunk}/${data.total_chunks})`,
33+
);
3234
});
3335
}
3436

src/ts/interface/NavigationDataInterfaceTypes.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ export enum NavigraphEventType {
1414
export interface DownloadProgressData {
1515
total_bytes: number;
1616
downloaded_bytes: number;
17+
current_chunk: number;
18+
total_chunks: number;
1719
}
1820

1921
export enum NavigraphFunction {

src/wasm/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "msfs-navigation-data-interface"
3-
version = "1.2.0-rc1"
3+
version = "1.2.0-rc2"
44
edition = "2021"
55

66
[lib]

src/wasm/src/funcs.rs

Lines changed: 21 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -86,65 +86,36 @@ impl Function for DownloadNavigationData {
8686
.ok_or(anyhow!("invalid content-range"))?
8787
.parse::<usize>()?;
8888

89-
// Report the amount
90-
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
91-
total_bytes,
92-
downloaded_bytes: 0,
93-
})?;
89+
// Total amount of chunks to download
90+
let total_chunks = total_bytes.div_ceil(DOWNLOAD_CHUNK_SIZE_BYTES);
9491

9592
// We need to download the data in chunks of DOWNLOAD_CHUNK_SIZE_BYTES to avoid a timeout, so we need to keep track of a "working" accumulation of all responses
9693
let mut bytes = vec![];
9794

98-
let mut current_byte_index = 0;
99-
loop {
95+
for i in 0..total_chunks {
96+
// Calculate the range for the current chunk
97+
let range_start = i * DOWNLOAD_CHUNK_SIZE_BYTES;
98+
let range_end = ((i + 1) * DOWNLOAD_CHUNK_SIZE_BYTES - 1).min(total_bytes - 1);
99+
100+
// Report the current download progress
101+
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
102+
total_bytes,
103+
downloaded_bytes: range_start,
104+
current_chunk: i,
105+
total_chunks,
106+
})?;
107+
100108
// Dispatch the request
101-
let range_end = current_byte_index + DOWNLOAD_CHUNK_SIZE_BYTES - 1;
102-
let request = NetworkRequestBuilder::new(&self.url)
109+
let data = NetworkRequestBuilder::new(&self.url)
103110
.context("can't create new NetworkRequestBuilder")?
104-
.with_header(&format!("Range: bytes={current_byte_index}-{range_end}"))
111+
.with_header(&format!("Range: bytes={range_start}-{range_end}"))
105112
.context(".with_header() returned None")?
106113
.get()
107-
.context(".get() returned None")?;
108-
109-
request.wait_for_data().await?;
110-
111-
// Get the size of actual data. The response will be as long as the requested range is, but content-length contains the amount we actually want to read
112-
let content_length = request
113-
.header_section("content-length")
114-
.context("no content-length header")?
115-
.trim()
116-
.parse::<usize>()?;
117-
118-
// Check if we somehow have no more data (file size would be a perfect multiple of DOWNLOAD_CHUNK_SIZE_BYTES)
119-
if content_length == 0 {
120-
break;
121-
}
122-
123-
let data = request.data().ok_or(anyhow!("no data"))?;
114+
.context(".get() returned None")?
115+
.wait_for_data()
116+
.await?;
124117

125-
// Make sure we don't panic if server sent less data than claimed (should never happen, but avoid a panic)
126-
if data.len() < content_length {
127-
return Err(anyhow!(
128-
"Received less data ({}) than content-length ({})",
129-
data.len(),
130-
content_length
131-
));
132-
}
133-
134-
bytes.write_all(&data[..content_length])?;
135-
136-
// Check if we have hit the last chunk
137-
if content_length < DOWNLOAD_CHUNK_SIZE_BYTES {
138-
break;
139-
}
140-
141-
current_byte_index += content_length;
142-
143-
// Send the current download amount
144-
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
145-
total_bytes,
146-
downloaded_bytes: current_byte_index,
147-
})?;
118+
bytes.write_all(&data)?;
148119
}
149120

150121
// Only close connection if DATABASE_STATE has already been initialized - otherwise we end up unnecessarily copying the bundled data and instantly replacing it (due to initialization logic in database state)

src/wasm/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,14 @@ const HEARTBEAT_INTERVAL_MS: u128 = 1000;
2424
/// The data associated with the `DownloadProgress` event
2525
#[derive(Serialize)]
2626
pub struct DownloadProgressEvent {
27+
/// The total amount of bytes to download
2728
pub total_bytes: usize,
29+
/// The amount of bytes downloaded
2830
pub downloaded_bytes: usize,
31+
/// The chunk number (starting at 0) of the current download
32+
pub current_chunk: usize,
33+
/// The total number of chunks needed to download
34+
pub total_chunks: usize,
2935
}
3036

3137
/// The types of events that can be emitted from the interface

0 commit comments

Comments
 (0)