More granular progress report for JSPF and scrobblerlog

This commit is contained in:
Philipp Wolfer 2025-05-23 07:47:52 +02:00
parent 83eac8c801
commit c7af90b585
No known key found for this signature in database
GPG key ID: 8FDF744D4919943B
4 changed files with 55 additions and 19 deletions

View file

@ -22,6 +22,7 @@ THE SOFTWARE.
package models
import (
"iter"
"strings"
"time"
@ -244,3 +245,38 @@ func (p *Progress) Complete() {
func (p *Progress) Abort() {
p.Aborted = true
}
func IterExportProgress[T any](
items []T, t *TransferProgress, c chan TransferProgress,
) iter.Seq2[int, T] {
return iterProgress(items, t, t.Export, c, true)
}
func IterImportProgress[T any](
items []T, t *TransferProgress, c chan TransferProgress,
) iter.Seq2[int, T] {
return iterProgress(items, t, t.Import, c, false)
}
func iterProgress[T any](
items []T, t *TransferProgress,
p *Progress, c chan TransferProgress,
autocomplete bool,
) iter.Seq2[int, T] {
// Report progress in 1% steps
steps := len(items) / 100
return func(yield func(int, T) bool) {
for i, item := range items {
yield(i, item)
p.Elapsed++
if i%steps == 0 {
c <- *t
}
}
if autocomplete {
p.Complete()
c <- *t
}
}
}