@@ -502,6 +502,11 @@ fn run_all(state: &PreviewBuild, task: &TaskGuard, lanes: usize, force: bool, ac
502502 if state. background && ( wait_for_user_work ( activity, & state. cancel , task) || wait_on_screen ( & state. cancel ) ) {
503503 break ;
504504 }
505+ // a build that was asked for (an import's, Build Previews) waits while an export runs (the export ranks
506+ // higher, `crate::sched`) and gives the photo on screen a moment
507+ if !state. background && ( wait_for_export ( activity, & state. cancel , task) || give_way_to_screen ( & state. cancel ) ) {
508+ break ;
509+ }
505510 let next = {
506511 let mut q = queue. lock ( ) ;
507512 match state. next_index ( & q) {
@@ -587,6 +592,49 @@ fn wait_for_user_work(activity: &crate::activity::Activity, cancel: &AtomicBool,
587592 cancel. load ( Ordering :: Relaxed )
588593}
589594
595+ /// Wait while an export runs, saying so in the build's row (a build that was asked for: rank 2 below the export,
596+ /// `crate::sched`). `true`: cancelled meanwhile.
597+ fn wait_for_export ( activity : & crate :: activity:: Activity , cancel : & AtomicBool , task : & TaskGuard ) -> bool {
598+ let mut paused = false ;
599+ while activity. running_kind ( & [ "export" ] ) . is_some ( ) {
600+ if cancel. load ( Ordering :: Relaxed ) {
601+ return true ;
602+ }
603+ if !paused {
604+ paused = true ;
605+ task. detail ( "Paused while exporting" ) ;
606+ }
607+ #[ cfg( not( target_arch = "wasm32" ) ) ]
608+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 100 ) ) ;
609+ #[ cfg( target_arch = "wasm32" ) ]
610+ break ;
611+ }
612+ if paused {
613+ task. detail ( "" ) ;
614+ }
615+ cancel. load ( Ordering :: Relaxed )
616+ }
617+
618+ /// How long a build that was asked for waits for the photo on screen before its next photo (ms): the loupe's own render
619+ /// goes first, and the build never stalls behind a user who keeps stepping.
620+ #[ cfg( not( target_arch = "wasm32" ) ) ]
621+ const SCREEN_WAIT_MS : u64 = 600 ;
622+
623+ /// Wait while the photo on screen is being made, at most [`SCREEN_WAIT_MS`]. `true`: cancelled meanwhile.
624+ fn give_way_to_screen ( cancel : & AtomicBool ) -> bool {
625+ #[ cfg( not( target_arch = "wasm32" ) ) ]
626+ {
627+ let t0 = web_time:: Instant :: now ( ) ;
628+ while crate :: prep:: foreground_busy ( ) && t0. elapsed ( ) < std:: time:: Duration :: from_millis ( SCREEN_WAIT_MS ) {
629+ if cancel. load ( Ordering :: Relaxed ) {
630+ return true ;
631+ }
632+ std:: thread:: sleep ( std:: time:: Duration :: from_millis ( 15 ) ) ;
633+ }
634+ }
635+ cancel. load ( Ordering :: Relaxed )
636+ }
637+
590638/// The long edge the background preparation builds standard previews at: the library's standard
591639/// size, or what the loupe asks for on this screen when that is more ([`Session::loupe_edge`]), so
592640/// the loupe finds what was built.
@@ -630,7 +678,9 @@ fn build(s: &mut Session, p: &Value) -> Result<Value> {
630678 let wait = p. get ( "wait" ) . and_then ( Value :: as_bool) . unwrap_or ( false ) || cfg ! ( target_arch = "wasm32" ) ;
631679 // `feed`: the caller makes the jobs a few photos a frame ([`feed`]), nearest the user first;
632680 // making all of them here froze the UI for 1.3 s at every build of a 2,597-photo folder (2026-11-10)
633- let lazy = background && !wait && p. get ( "feed" ) . and_then ( Value :: as_bool) . unwrap_or ( false ) ;
681+ // (an import's build is fed too once the app ticks the preparation: its jobs then carry the photo's path when they
682+ // are made, nearest the user first, not when the import's batch came in)
683+ let lazy = ( background || s. prep . ticking ( ) ) && !wait && p. get ( "feed" ) . and_then ( Value :: as_bool) . unwrap_or ( false ) ;
634684 let ( order, slots) : ( Vec < u64 > , Vec < Slot > ) = if lazy {
635685 ( ids. iter ( ) . map ( |id| id. 0 ) . collect ( ) , ids. iter ( ) . map ( |_| Some ( None ) ) . collect ( ) )
636686 } else {
@@ -819,12 +869,84 @@ pub fn build_after_import(s: &mut Session, ids: &[lcc_catalog::PhotoId]) {
819869 if ids. is_empty ( ) {
820870 return ;
821871 }
872+ // An import commits its photos a batch at a time: while the build of an earlier batch runs, these photos wait and
873+ // the next frame after it builds all that waited ([`build_owed_import`]). Before, every batch after the first
874+ // was dropped ("a preview build is already running"): 16 of 261 photos got their previews.
875+ if s. prep . ticking ( ) && s. preview_build . as_ref ( ) . is_some_and ( |b| b. running ( ) && !b. background ) {
876+ s. prep . owed_import . extend ( ids. iter ( ) . copied ( ) ) ;
877+ return ;
878+ }
879+ start_import_build ( s, size, ids) ;
880+ }
881+
882+ /// Start the build of an import's previews (`size` as `library.buildPreviews`), from the photo the user is on.
883+ fn start_import_build ( s : & mut Session , size : & str , ids : & [ lcc_catalog:: PhotoId ] ) {
822884 let ids: Vec < u64 > = ids. iter ( ) . map ( |id| id. 0 ) . collect ( ) ;
823- if let Err ( e) = build ( s, & json ! ( { "size" : size, "ids" : ids} ) ) {
885+ let mut p = json ! ( { "size" : size, "ids" : ids, "feed" : true } ) ;
886+ if let Some ( at) = s. active ( ) {
887+ p[ "from" ] = json ! ( at. 0 ) ;
888+ }
889+ if let Err ( e) = build ( s, & p) {
824890 log:: warn!( "previews at import: {e}" ) ;
825891 }
826892}
827893
894+ /// The photos of an import whose previews wait for the build before them ([`build_after_import`]): built once that
895+ /// build has ended. Cheap; called every frame ([`Session::prep_tick`]).
896+ pub ( crate ) fn build_owed_import ( s : & mut Session ) {
897+ if s. prep . owed_import . is_empty ( ) || s. preview_build . as_ref ( ) . is_some_and ( |b| b. running ( ) && !b. background ) {
898+ return ;
899+ }
900+ let size = match s. preview_prefs . at_import {
901+ ImportPreviews :: Minimal => {
902+ s. prep . owed_import . clear ( ) ;
903+ return ;
904+ }
905+ ImportPreviews :: Standard => "standard" ,
906+ ImportPreviews :: Full => "full" ,
907+ } ;
908+ let mut seen = std:: collections:: HashSet :: new ( ) ;
909+ let ids: Vec < lcc_catalog:: PhotoId > = std:: mem:: take ( & mut s. prep . owed_import ) . into_iter ( ) . filter ( |id| seen. insert ( * id) ) . collect ( ) ;
910+ start_import_build ( s, size, & ids) ;
911+ }
912+
913+ /// The photos of `ids` whose file moved (Rename Photos, a move, a relink, their undo): the running build's jobs made
914+ /// for them are made again, with the photo's path now. A build queued just before a rename kept the old paths and
915+ /// failed 84 photos with "cannot find the file".
916+ pub ( crate ) fn refresh_jobs_of ( s : & mut Session , ids : & [ lcc_catalog:: PhotoId ] ) {
917+ let Some ( b) = s. preview_build . clone ( ) . filter ( |b| b. running ( ) ) else { return } ;
918+ let moved: std:: collections:: HashSet < u64 > = ids. iter ( ) . map ( |id| id. 0 ) . collect ( ) ;
919+ let stale: Vec < usize > = {
920+ let mut q = b. queue . lock ( ) ;
921+ b. order
922+ . iter ( )
923+ . enumerate ( )
924+ . filter ( |( _, id) | moved. contains ( * id) )
925+ . filter_map ( |( i, _) | {
926+ let slot = q. get_mut ( i) ?;
927+ matches ! ( slot, Some ( Some ( _) ) ) . then ( || {
928+ * slot = Some ( None ) ;
929+ i
930+ } )
931+ } )
932+ . collect ( )
933+ } ;
934+ for i in stale {
935+ let Some ( id) = b. order . get ( i) . copied ( ) else { continue } ;
936+ let jobs = jobs_for ( s, lcc_catalog:: PhotoId ( id) , b. edge ) ;
937+ let mut q = b. queue . lock ( ) ;
938+ if let Some ( slot) = q. get_mut ( i) . filter ( |s| matches ! ( s, Some ( None ) ) ) {
939+ if jobs. is_empty ( ) {
940+ * slot = None ;
941+ b. skipped . fetch_add ( 1 , Ordering :: Relaxed ) ;
942+ b. done . fetch_add ( 1 , Ordering :: Relaxed ) ;
943+ } else {
944+ * slot = Some ( Some ( jobs) ) ;
945+ }
946+ }
947+ }
948+ }
949+
828950/// One photo's smart preview to build or discard: (photo, its file in the smart previews folder,
829951/// how to load the original at preview size, whether the photo can take a preview-fitted look:
830952/// vendor raws and DNGs (whose tone can come from their preview); only those proxies go stale when
0 commit comments