summaryrefslogtreecommitdiff
path: root/lib/Ultramarine/Model/Collection.pm6
blob: a2ec43262c27513cf615fc877983b8d50d6d4979 (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
41
42
43
44
45
46
47
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() {
        # 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 use this to check that all the files in 
                # the db have been seen, and delete those that haven't 
                $.db.remove-unseen-files();
                $!mark-ready.keep(True);
            }
            when $path ~~ IO::Path & :f {
                my $mtime = $path.modified.floor;
                # we only care to keep track of which files we've 
                # seen during the initial scan (so we can clean up 
                # rows for files that were removed while we were 
                # not running); after that, we'll get notification 
                # of deleted files, and we'll remove those 
                $.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,:%metadataif %metadata.keys;
                }
            }
            when $path ~~ IO::Path & :!f {
                $.db.ensure-file-absent(:$path);
            }
        }
    }
}