Skip to content

Commit 1f56c6b

Browse files
committed
fix: don't copy bundled if downloading
1 parent bfba4fe commit 1f56c6b

File tree

2 files changed

+14
-10
lines changed

2 files changed

+14
-10
lines changed

src/wasm/src/database/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ impl DatabaseState {
189189
.ok();
190190

191191
// Find the most recent distribution
192-
let latest = [bundled_distribution, downloaded_distribution]
192+
let latest = [downloaded_distribution, bundled_distribution]
193193
.into_iter()
194194
.filter_map(|d| d)
195195
.reduce(|a, b| {

src/wasm/src/funcs.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::{
55

66
use anyhow::{anyhow, Context, Result};
77
use msfs::network::NetworkRequestBuilder;
8+
use once_cell::sync::Lazy;
89
use serde::{de::DeserializeOwned, Deserialize, Serialize};
910
use serde_json::{json, Value};
1011
use zip::ZipArchive;
@@ -77,11 +78,14 @@ impl Function for DownloadNavigationData {
7778
.wait_for_data()
7879
.await?;
7980

80-
// Drop the current database. We don't do this before the download as there is a chance it will fail, and then we end up with no database open.
81-
DATABASE_STATE
82-
.try_lock()
83-
.map_err(|_| anyhow!("can't lock DATABASE_STATE"))?
84-
.close_connection()?;
81+
// 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)
82+
if Lazy::get(&DATABASE_STATE).is_some() {
83+
// Drop the current database. We don't do this before the download as there is a chance it will fail, and then we end up with no database open.
84+
DATABASE_STATE
85+
.try_lock()
86+
.map_err(|_| anyhow!("can't lock DATABASE_STATE"))?
87+
.close_connection()?;
88+
}
8589

8690
// Send the deleting and extraction events
8791
InterfaceEvent::send_download_progress_event(DownloadProgressEvent {
@@ -487,10 +491,10 @@ make_function!(
487491
/// - `RunStatus::InProgress` if the future isn’t complete yet.
488492
/// - `RunStatus::Finished` if the future resolved.
489493
///
490-
/// This is useful in our environment as we need to yield back to the sim in order not to block the thread,
494+
/// This is useful in our environment as we need to yield back to the sim in order not to block the thread,
491495
/// and we may have some functions that aren't able to resolve in a single frame.
492496
///
493-
/// Once the future resolves, the result is automatically serialized into a `FunctionResult` structure and
497+
/// Once the future resolves, the result is automatically serialized into a `FunctionResult` structure and
494498
/// sent across the commbus using the `NAVIGRAPH_FunctionResult` event.
495499
///
496500
/// # Note
@@ -544,7 +548,7 @@ macro_rules! define_interface_functions {
544548
}
545549

546550
fn run(&mut self) -> anyhow::Result<RunStatus> {
547-
// We allow the function run to be async in order to wait for certain conditions.
551+
// We allow the function run to be async in order to wait for certain conditions.
548552
// However, MSFS WASM modules are not multithreaded so we need to yield back to the main thread.
549553
// We get around this by polling once per update, and then continuing to poll (if needed) in later updates.
550554
match futures_lite::future::block_on(futures_lite::future::poll_once(&mut self.future)) {
@@ -590,7 +594,7 @@ macro_rules! define_interface_functions {
590594
pub enum InterfaceFunction {
591595
$( $fn_name([<$fn_name Wrapper>]), )*
592596
}
593-
597+
594598
impl<'de> serde::Deserialize<'de> for InterfaceFunction {
595599
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
596600
where

0 commit comments

Comments
 (0)