|
11 | 11 | //! |
12 | 12 | //! Mechanism mirrors ios/NativeScript/runtime/HttpLoader.mm + ModuleInternalCallbacks.mm. |
13 | 13 |
|
14 | | -use std::cell::RefCell; |
| 14 | +use std::cell::{Cell, RefCell}; |
15 | 15 | use std::collections::{HashMap, HashSet}; |
16 | 16 | use std::sync::{Mutex, OnceLock}; |
17 | 17 |
|
@@ -348,10 +348,63 @@ fn fetch_once(url: &str, key: &str) -> Result<crate::winhttp::Response, String> |
348 | 348 | Ok(response) |
349 | 349 | } |
350 | 350 |
|
| 351 | +static DEV_BOOT_COMPLETE: std::sync::atomic::AtomicBool = std::sync::atomic::AtomicBool::new(false); |
| 352 | + |
| 353 | +thread_local! { |
| 354 | + static PUMPING: Cell<bool> = const { Cell::new(false) }; |
| 355 | +} |
| 356 | + |
| 357 | +/// `ns:module` `setDevBootComplete`: stops pumping messages during HTTP imports. |
| 358 | +pub fn set_dev_boot_complete(done: bool) { |
| 359 | + DEV_BOOT_COMPLETE.store(done, std::sync::atomic::Ordering::Relaxed); |
| 360 | +} |
| 361 | + |
| 362 | +/// Until dev boot completes, runs `fetch` off-thread and pumps messages meanwhile. |
| 363 | +fn pumping<R: Send>(fetch: impl FnOnce() -> R + Send) -> R { |
| 364 | + if !is_debug_build() || DEV_BOOT_COMPLETE.load(std::sync::atomic::Ordering::Relaxed) || PUMPING.get() { |
| 365 | + return fetch(); |
| 366 | + } |
| 367 | + use windows::core::PCWSTR; |
| 368 | + use windows::Win32::Foundation::{CloseHandle, HANDLE, WAIT_OBJECT_0}; |
| 369 | + use windows::Win32::System::Threading::{CreateEventW, SetEvent, INFINITE}; |
| 370 | + use windows::Win32::UI::WindowsAndMessaging::{MsgWaitForMultipleObjectsEx, MWMO_INPUTAVAILABLE, QS_ALLINPUT}; |
| 371 | + |
| 372 | + let Ok(done) = (unsafe { CreateEventW(None, true, false, PCWSTR::null()) }) else { |
| 373 | + return fetch(); |
| 374 | + }; |
| 375 | + let event = done.0 as usize; |
| 376 | + PUMPING.set(true); |
| 377 | + let result = std::thread::scope(|s| { |
| 378 | + let handle = s.spawn(move || { |
| 379 | + let result = fetch(); |
| 380 | + unsafe { |
| 381 | + let _ = SetEvent(HANDLE(event as *mut _)); |
| 382 | + } |
| 383 | + result |
| 384 | + }); |
| 385 | + // WAIT_OBJECT_0 + 1: messages arrived; the event or a failure ends the wait |
| 386 | + while unsafe { MsgWaitForMultipleObjectsEx(Some(&[done]), INFINITE, QS_ALLINPUT, MWMO_INPUTAVAILABLE) }.0 |
| 387 | + == WAIT_OBJECT_0.0 + 1 |
| 388 | + { |
| 389 | + crate::pump_messages(); |
| 390 | + } |
| 391 | + handle.join() |
| 392 | + }); |
| 393 | + PUMPING.set(false); |
| 394 | + unsafe { |
| 395 | + let _ = CloseHandle(done); |
| 396 | + } |
| 397 | + result.unwrap_or_else(|panic| std::panic::resume_unwind(panic)) |
| 398 | +} |
| 399 | + |
| 400 | +pub(crate) fn fetch_module(url: &str, key: &str) -> Result<FetchedModule, String> { |
| 401 | + pumping(|| fetch_module_now(url, key)) |
| 402 | +} |
| 403 | + |
351 | 404 | /// Fetch one module over HTTP (one retry on transport error) and classify the response the way |
352 | 405 | /// the iOS runtime does: 2xx JavaScript (an empty body is the empty module) or JSON; anything else |
353 | 406 | /// is an import error naming the URL. |
354 | | -pub(crate) fn fetch_module(url: &str, key: &str) -> Result<FetchedModule, String> { |
| 407 | +fn fetch_module_now(url: &str, key: &str) -> Result<FetchedModule, String> { |
355 | 408 | if !remote_url_allowed(url) { |
356 | 409 | return Err(format!( |
357 | 410 | "HTTP import blocked: remote module loading is not allowed for {url} (set security.allowRemoteModules in the app's package.json)" |
@@ -399,16 +452,20 @@ pub(crate) fn fetch_module(url: &str, key: &str) -> Result<FetchedModule, String |
399 | 452 |
|
400 | 453 | /// Fetch several modules concurrently (bounded), preserving input order. |
401 | 454 | pub(crate) fn fetch_modules(items: &[(String, String)]) -> Vec<Result<FetchedModule, String>> { |
| 455 | + pumping(|| fetch_modules_now(items)) |
| 456 | +} |
| 457 | + |
| 458 | +fn fetch_modules_now(items: &[(String, String)]) -> Vec<Result<FetchedModule, String>> { |
402 | 459 | const MAX_PARALLEL: usize = 8; |
403 | 460 | if items.len() <= 1 { |
404 | | - return items.iter().map(|(u, k)| fetch_module(u, k)).collect(); |
| 461 | + return items.iter().map(|(u, k)| fetch_module_now(u, k)).collect(); |
405 | 462 | } |
406 | 463 | let mut out: Vec<Option<Result<FetchedModule, String>>> = (0..items.len()).map(|_| None).collect(); |
407 | 464 | for (chunk_index, chunk) in items.chunks(MAX_PARALLEL).enumerate() { |
408 | 465 | std::thread::scope(|s| { |
409 | 466 | let handles: Vec<_> = chunk |
410 | 467 | .iter() |
411 | | - .map(|(u, k)| s.spawn(move || fetch_module(u, k))) |
| 468 | + .map(|(u, k)| s.spawn(move || fetch_module_now(u, k))) |
412 | 469 | .collect(); |
413 | 470 | for (i, h) in handles.into_iter().enumerate() { |
414 | 471 | out[chunk_index * MAX_PARALLEL + i] = |
|
0 commit comments