@@ -152,6 +152,7 @@ export function PathProfileApp() {
152152 } | null > ( null ) ;
153153 const pathHistoryRef = useRef < PathSnapshot [ ] > ( [ ] ) ;
154154 const profileRequestIdRef = useRef ( 0 ) ;
155+ const profileRequestBusyRef = useRef ( false ) ;
155156 const resizeDragRef = useRef < ResizeDrag | null > ( null ) ;
156157
157158 const pathToRestore = useMemo (
@@ -167,6 +168,16 @@ export function PathProfileApp() {
167168 [ ] ,
168169 ) ;
169170
171+ const invalidateProfileRequest = useCallback ( ( clearBusy = true ) => {
172+ profileRequestIdRef . current += 1 ;
173+ if ( profileRequestBusyRef . current ) {
174+ profileRequestBusyRef . current = false ;
175+ if ( clearBusy ) {
176+ setBusy ( false ) ;
177+ }
178+ }
179+ } , [ ] ) ;
180+
170181 const profileStats = useMemo ( ( ) => {
171182 const elevations = profilePoints
172183 . map ( ( point ) => point . elevation )
@@ -220,6 +231,7 @@ export function PathProfileApp() {
220231
221232 const requestId = profileRequestIdRef . current + 1 ;
222233 profileRequestIdRef . current = requestId ;
234+ profileRequestBusyRef . current = true ;
223235 setBusy ( true ) ;
224236 setError ( null ) ;
225237 setStatus ( "Sampling profile" ) ;
@@ -243,6 +255,7 @@ export function PathProfileApp() {
243255 setStatus ( "Profile failed" ) ;
244256 } finally {
245257 if ( profileRequestIdRef . current === requestId ) {
258+ profileRequestBusyRef . current = false ;
246259 setBusy ( false ) ;
247260 }
248261 }
@@ -272,7 +285,7 @@ export function PathProfileApp() {
272285
273286 setStatus ( "Loading DEM" ) ;
274287 const summary = await api . loadDsmProject ( paths ) ;
275- profileRequestIdRef . current += 1 ;
288+ invalidateProfileRequest ( false ) ;
276289 setProject ( summary ) ;
277290 setWarnings ( summary . warnings ) ;
278291 setProfilePoints ( [ ] ) ;
@@ -299,7 +312,7 @@ export function PathProfileApp() {
299312 } finally {
300313 setBusy ( false ) ;
301314 }
302- } , [ syncLineOfSightEndpoints ] ) ;
315+ } , [ invalidateProfileRequest , syncLineOfSightEndpoints ] ) ;
303316
304317 const handleExport = useCallback ( async ( ) => {
305318 const api = getPathProfileApi ( ) ;
@@ -348,7 +361,7 @@ export function PathProfileApp() {
348361 setProfilePoints ( [ ] ) ;
349362 syncLineOfSightEndpoints ( null ) ;
350363 setActivePoint ( null ) ;
351- profileRequestIdRef . current += 1 ;
364+ invalidateProfileRequest ( ) ;
352365 setDrawingEnabled ( false ) ;
353366 setPathEditEnabled ( false ) ;
354367 setClearPathRequest ( ( request ) => request + 1 ) ;
@@ -363,7 +376,12 @@ export function PathProfileApp() {
363376 setStatus ( "A to B path" ) ;
364377 void generateProfileForPath ( straightPath , projection ) ;
365378 } ,
366- [ currentPathSnapshot , generateProfileForPath , syncLineOfSightEndpoints ] ,
379+ [
380+ currentPathSnapshot ,
381+ generateProfileForPath ,
382+ invalidateProfileRequest ,
383+ syncLineOfSightEndpoints ,
384+ ] ,
367385 ) ;
368386
369387 const handleStartPath = useCallback ( ( ) => {
@@ -379,7 +397,7 @@ export function PathProfileApp() {
379397 pathHistoryRef . current = [ ] ;
380398 setOpenPopover ( null ) ;
381399 setStatus ( "Place A, then B" ) ;
382- } , [ syncLineOfSightEndpoints ] ) ;
400+ } , [ invalidateProfileRequest , syncLineOfSightEndpoints ] ) ;
383401
384402 const handleEndPathEdit = useCallback ( ( ) => {
385403 setPathEditEnabled ( false ) ;
@@ -399,13 +417,18 @@ export function PathProfileApp() {
399417 setProfilePoints ( [ ] ) ;
400418 syncLineOfSightEndpoints ( null ) ;
401419 setActivePoint ( null ) ;
402- profileRequestIdRef . current += 1 ;
420+ invalidateProfileRequest ( ) ;
403421 setDrawingEnabled ( false ) ;
404422 setPathEditEnabled ( false ) ;
405423 setClearPathRequest ( ( request ) => request + 1 ) ;
406424 setOpenPopover ( null ) ;
407425 setStatus ( project ? "Path cleared" : "Idle" ) ;
408- } , [ currentPathSnapshot , project , syncLineOfSightEndpoints ] ) ;
426+ } , [
427+ currentPathSnapshot ,
428+ invalidateProfileRequest ,
429+ project ,
430+ syncLineOfSightEndpoints ,
431+ ] ) ;
409432
410433 const handleUndoPath = useCallback ( ( ) => {
411434 const snapshot = pathHistoryRef . current . pop ( ) ;
0 commit comments