refactored full rescan to use hook/lifecycle pattern and added rescan package

This commit is contained in:
2026-02-24 16:11:41 -05:00
parent 768a8cf1f2
commit ff7649600b
7 changed files with 57 additions and 69 deletions
+7 -7
View File
@@ -148,13 +148,13 @@ func (yj *YellowJacketApp) OnStartup(ctx context.Context) {
yj.queue.SetPlayer(yj.player)
yj.queue.RestoreState()
// Give the library a reference to the queue so FullRescan can
// clear the queue and stop playback before wiping data.
yj.library.SetQueue(yj.queue)
// Give the library a reference to the playlist service so
// FullRescan can restore playlists from M3U8 files.
yj.library.SetPlaylistRestorer(yj.playlist)
// Wire cross-cutting rescan hooks so the library can
// orchestrate queue clearing and playlist restoration
// without depending on those packages directly.
yj.library.SetRescanHooks(library.RescanHooks{
PreClear: yj.queue.Clear,
PostScan: yj.playlist.RestoreAllPlaylists,
})
// Register playback finished handler to drive queue auto-advance.
yj.player.SetPlaybackFinishedHandler(yj.queue.OnPlaybackFinished)
+21 -27
View File
@@ -60,39 +60,33 @@ func newEntityCache() *entityCache {
}
}
// queueClearer is a narrow interface for clearing the playback queue.
type queueClearer interface {
Clear()
}
// playlistRestorer is a narrow interface for restoring playlists
// from M3U8 files after a library rescan.
type playlistRestorer interface {
RestoreAllPlaylists()
// RescanHooks holds optional callbacks that run before and after
// the library-clear-and-scan phase of a full rescan. The app
// layer sets these to coordinate cross-cutting concerns (e.g.
// clearing the queue, restoring playlists) without the library
// needing to know about those packages.
type RescanHooks struct {
// PreClear runs before library data is wiped
// (e.g. clear queue and stop playback).
PreClear func()
// PostScan runs after the scan completes
// (e.g. restore playlists from M3U8 files).
PostScan func()
}
// Library manages scanning and querying the music collection.
type Library struct {
ctx context.Context
logger *slog.Logger
conf *Config
db *database.DB
queue queueClearer
playlistRestorer playlistRestorer
ctx context.Context
logger *slog.Logger
conf *Config
db *database.DB
rescanHooks RescanHooks
}
// SetQueue provides the library with a reference to the queue so
// that destructive operations like FullRescan can clear the queue
// and stop playback before wiping data.
func (l *Library) SetQueue(q queueClearer) {
l.queue = q
}
// SetPlaylistRestorer provides the library with a reference to
// the playlist service so that FullRescan can restore playlists
// from M3U8 files after wiping data.
func (l *Library) SetPlaylistRestorer(p playlistRestorer) {
l.playlistRestorer = p
// SetRescanHooks provides optional hooks for cross-cutting
// orchestration during FullRescan.
func (l *Library) SetRescanHooks(h RescanHooks) {
l.rescanHooks = h
}
// NewLibrary creates a new library with the given configuration.
+9 -13
View File
@@ -6,9 +6,6 @@ import (
"path/filepath"
"time"
"github.com/wailsapp/wails/v2/pkg/runtime"
"yellowjacket/backend/events"
"yellowjacket/backend/system"
)
@@ -19,14 +16,13 @@ import (
func (l *Library) FullRescan() (*ScanMetrics, error) {
l.logger.Info("beginning full library rescan")
runtime.EventsEmit(l.ctx, events.LibraryScanStarted)
// Stop playback and clear the queue before wiping data so
// the player is not referencing now-deleted tracks.
// Run the pre-clear hook (e.g. clear queue / stop playback)
// before wiping data so the player is not referencing
// now-deleted tracks.
clearQueueStart := time.Now()
if l.queue != nil {
l.queue.Clear()
if l.rescanHooks.PreClear != nil {
l.rescanHooks.PreClear()
}
clearQueueDur := time.Since(clearQueueStart)
@@ -68,10 +64,10 @@ func (l *Library) FullRescan() (*ScanMetrics, error) {
clearDBDur + clearFilesDur
}
// Restore playlists from M3U8 files now that the library
// has been rescanned and audio_files are populated again.
if l.playlistRestorer != nil {
l.playlistRestorer.RestoreAllPlaylists()
// Run the post-scan hook (e.g. restore playlists from M3U8
// files) now that audio_files are populated again.
if l.rescanHooks.PostScan != nil {
l.rescanHooks.PostScan()
}
return metrics, err