use v6.d.PREVIEW;
need Ultramarine::Model::DirScanner;
class Ultramarine::Model::Collection {
has $.db is required handles <top-folders dir-children-of songs-children-of all-songs>;
has $.dirscan is required;
has $.file-class is required;
method new(|c) {
my $self = self.bless(|c);
start $self.update();
return $self;
}
has Promise $.is-ready .= new;
has $!mark-ready = $!is-ready.vow;
method update() {
CATCH { default { .perl.say } }
react whenever $.dirscan.scan -> $path {
when $path ~~ Ultramarine::Model::DirScanner::EndOfScan {
$.db.remove-unseen-files();
$!mark-ready.keep(True);
}
when $path ~~ IO::Path & :f {
my $mtime = $path.modified.floor;
$.db.seen-file(:$path) unless $.is-ready.status ~~ Kept;
unless $.db.is-up-to-date(:$path,:$mtime) {
my %metadata = $.file-class.new(:$path).metadata;
$.db.ensure-song(:$path,:$mtime,:%metadata) if %metadata.keys;
}
}
when $path ~~ IO::Path & :!f {
$.db.ensure-file-absent(:$path);
}
}
}
}