summaryrefslogtreecommitdiff
path: root/lib/Ultramarine/Model/Collection.pm6
blob: e894f1c8084a8db4ec7900681bcd80609d97fe3c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
use v6.d.PREVIEW;
need Ultramarine::Model::DirScanner;
 
class Ultramarine::Model::Collection {
    has $.db is required handles <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() {
        # I don't know what to do when things go wrong, let's "log" 
        # it, at least 
        CATCH { default { .perl.say } }
        react whenever $.dirscan.scan -> $path {
            when $path ~~ Ultramarine::Model::DirScanner::EndOfScan {
                # we should use this to check that all the files in 
                # the db have been seen, and delete those that haven't 
                # 
                # calling db.remove-unseen-files, plus db.remove-empty 
                $!mark-ready.keep(True);
            }
            when $path ~~ IO::Path & :f {
                my $mtime = $path.modified.floor;
                # call db.ensure-song and db.seen-file 
                unless $.db.is-up-to-date(:$path,:$mtime{
                    my %metadata = $.file-class.new(:$path).metadata;
                    $.db.ensure-song(:$path,:$mtime,:%metadataif %metadata.keys;
                }
            }
        }
    }
}