From 1a61d9d6ed16df849538a8702abb9eadea091d79 Mon Sep 17 00:00:00 2001 From: FrogAi <91348155+FrogAi@users.noreply.github.com> Date: Sun, 9 Aug 2026 20:15:30 -0700 Subject: [PATCH 1/2] Own map download progress lifecycle --- settings/download.go | 44 +++++++++++++++++++++++++++++++++++--------- settings/settings.go | 14 ++++++++++---- 2 files changed, 45 insertions(+), 13 deletions(-) diff --git a/settings/download.go b/settings/download.go index 466b553..2f45002 100644 --- a/settings/download.go +++ b/settings/download.go @@ -113,6 +113,38 @@ func (p *DownloadProgress) addLocationDetails(path string) { } } +func (p DownloadProgress) clone() DownloadProgress { + clone := p + clone.LocationsToDownload = append([]string(nil), p.LocationsToDownload...) + clone.LocationDetails = make(map[string]*DownloadLocationDetail, len(p.LocationDetails)) + for path, detail := range p.LocationDetails { + if detail == nil { + clone.LocationDetails[path] = nil + continue + } + detailCopy := *detail + clone.LocationDetails[path] = &detailCopy + } + return clone +} + +func (d *download) publishProgress() { + progress := d.progress.clone() + select { + case d.progressChan <- progress: + return + default: + } + select { + case <-d.progressChan: + default: + } + select { + case d.progressChan <- progress: + default: + } +} + func Download(paths string, progressChan chan DownloadProgress, cancelChan chan bool) { slog.Info("download", "paths", paths) pathsSplit := strings.Split(paths, ",") @@ -141,10 +173,7 @@ func Download(paths string, progressChan chan DownloadProgress, cancelChan chan } } d.progress.Active = false - select { // nonblocking update of progress - case d.progressChan <- d.progress: - default: - } + d.publishProgress() // nonblocking update of progress } func adjustedBounds(bounds Bounds) (int, int, int, int) { @@ -170,11 +199,8 @@ func (d *download) downloadBounds(bounds Bounds, locationName string) (err error d.progress.LocationDetails[locationName].TotalFiles = countFilesForBounds(bounds) for i := minLat; i < maxLat; i += GROUP_AREA_BOX_DEGREES { for j := minLon; j < maxLon; j += GROUP_AREA_BOX_DEGREES { - select { // nonblocking update of progress - case d.progressChan <- d.progress: - default: - } - select { // cancel if sent message + d.publishProgress() // nonblocking update of progress + select { // cancel if sent message case cancel := <-d.cancelChan: if cancel { return nil, true diff --git a/settings/settings.go b/settings/settings.go index abb7fb0..511afc9 100644 --- a/settings/settings.go +++ b/settings/settings.go @@ -19,7 +19,6 @@ const SETTINGS_VERSION = 2 // Used for migrations var Settings = MapdSettings{ SettingsVersion: SETTINGS_VERSION, downloadProgress: make(chan DownloadProgress, 1), - cancelDownload: make(chan bool, 1), } type SpeedLimitPriority string @@ -301,6 +300,9 @@ func (s *MapdSettings) GetDownloadProgress() (progress DownloadProgress, success select { case progress = <-s.downloadProgress: s.downloadActive = progress.Active + if !progress.Active { + s.cancelDownload = nil + } return progress, true default: } @@ -327,9 +329,11 @@ func (s *MapdSettings) Handle(input custom.MapdIn) { case custom.MapdInputType_loadRecommendedSettings: s.Recommended() case custom.MapdInputType_cancelDownload: - select { - case s.cancelDownload <- true: - default: + if s.downloadActive { + select { + case s.cancelDownload <- true: + default: + } } case custom.MapdInputType_download: path, err := input.Str() @@ -338,6 +342,8 @@ func (s *MapdSettings) Handle(input custom.MapdIn) { return } if !s.downloadActive { + s.downloadActive = true + s.cancelDownload = make(chan bool, 1) go Download(path, s.downloadProgress, s.cancelDownload) } case custom.MapdInputType_acceptSpeedLimit: From c97c07234d29b776c1723f83639ca6aec083dac8 Mon Sep 17 00:00:00 2001 From: FrogAi <91348155+FrogAi@users.noreply.github.com> Date: Sun, 6 Sep 2026 11:06:03 -0700 Subject: [PATCH 2/2] Cleanup --- settings/download.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/settings/download.go b/settings/download.go index 8f8ff52..823da93 100644 --- a/settings/download.go +++ b/settings/download.go @@ -153,8 +153,7 @@ func (d *download) publishProgress() { progress.LocationDetails[path] = &locationDetail } - // Replace queued progress in the settings owner's one-slot channel. - select { + select { // discard queued progress case <-d.progressChan: default: }