fix: add navigation generation counter to prevent stale list overwrite (I1)

Introduces self._nav_gen, incremented on every _navigate_to_item_data
call. The counter is threaded through _fetch_list_contents_bg and
checked in _finalize_list_loading: if the user navigated away while a
fetch was in flight, the stale results are silently discarded instead of
overwriting the active folder view and re-sorting its items.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Martin Tranberg
2026-04-12 09:44:39 +02:00
parent b800f0308d
commit d529568798

View File

@@ -671,6 +671,7 @@ class SharePointApp(wx.Frame):
self.current_items = [] # Gemmer graf-objekterne for rækkerne
self.tree_item_data = {} # Mappenoder -> {type, id, name, drive_id, path}
self.current_path_data = [] # Gemmer data-objekterne for den nuværende sti (brødkrummer)
self._nav_gen = 0 # Incremented on each navigation to discard stale fetch results
self.tree_root = None
self.is_navigating_back = False
self.active_edits = {} # item_id -> { "name": name, "event": Event, "waiting": bool }
@@ -1886,12 +1887,14 @@ class SharePointApp(wx.Frame):
self.list_ctrl.DeleteAllItems()
self.current_items = []
self.set_status(self.get_txt("status_loading_content"))
threading.Thread(target=self._fetch_list_contents_bg, args=(data,), daemon=True).start()
self._nav_gen += 1
gen = self._nav_gen
threading.Thread(target=self._fetch_list_contents_bg, args=(data, gen), daemon=True).start()
except RuntimeError:
pass
def _fetch_list_contents_bg(self, data):
def _fetch_list_contents_bg(self, data, nav_gen=0):
if not self.ensure_valid_token(): return
self.pulse_gauge(True)
items_data = []
@@ -1950,7 +1953,7 @@ class SharePointApp(wx.Frame):
url = res_data.get('@odata.nextLink')
# Finalize
wx.CallAfter(self._finalize_list_loading, items_data)
wx.CallAfter(self._finalize_list_loading, items_data, nav_gen)
self.pulse_gauge(False)
def _append_list_items(self, items):
@@ -1973,8 +1976,10 @@ class SharePointApp(wx.Frame):
self.list_ctrl.SetItem(idx, 2, size_str)
self.list_ctrl.SetItem(idx, 3, item['modified'])
def _finalize_list_loading(self, items_data):
def _finalize_list_loading(self, items_data, nav_gen=0):
if not self: return
if nav_gen != self._nav_gen:
return # User navigated away; discard stale results
self.current_items = items_data
self.apply_sorting()
self.set_status(self.get_txt("status_ready"))