@@ -5,6 +5,7 @@ use std::{
5
5
6
6
use anyhow:: { anyhow, Context , Result } ;
7
7
use msfs:: network:: NetworkRequestBuilder ;
8
+ use once_cell:: sync:: Lazy ;
8
9
use serde:: { de:: DeserializeOwned , Deserialize , Serialize } ;
9
10
use serde_json:: { json, Value } ;
10
11
use zip:: ZipArchive ;
@@ -77,11 +78,14 @@ impl Function for DownloadNavigationData {
77
78
. wait_for_data ( )
78
79
. await ?;
79
80
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
+ }
85
89
86
90
// Send the deleting and extraction events
87
91
InterfaceEvent :: send_download_progress_event ( DownloadProgressEvent {
@@ -487,10 +491,10 @@ make_function!(
487
491
/// - `RunStatus::InProgress` if the future isn’t complete yet.
488
492
/// - `RunStatus::Finished` if the future resolved.
489
493
///
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,
491
495
/// and we may have some functions that aren't able to resolve in a single frame.
492
496
///
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
494
498
/// sent across the commbus using the `NAVIGRAPH_FunctionResult` event.
495
499
///
496
500
/// # Note
@@ -544,7 +548,7 @@ macro_rules! define_interface_functions {
544
548
}
545
549
546
550
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.
548
552
// However, MSFS WASM modules are not multithreaded so we need to yield back to the main thread.
549
553
// We get around this by polling once per update, and then continuing to poll (if needed) in later updates.
550
554
match futures_lite:: future:: block_on( futures_lite:: future:: poll_once( & mut self . future) ) {
@@ -590,7 +594,7 @@ macro_rules! define_interface_functions {
590
594
pub enum InterfaceFunction {
591
595
$( $fn_name( [ <$fn_name Wrapper >] ) , ) *
592
596
}
593
-
597
+
594
598
impl <' de> serde:: Deserialize <' de> for InterfaceFunction {
595
599
fn deserialize<D >( deserializer: D ) -> Result <Self , D :: Error >
596
600
where
0 commit comments