summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/ScanDir.rakumod
blob: f0ea0b7206e04abe67b86ef4b58377a5bc38e07a (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
48
49
50
51
52
use v6.d;
unit module MaildirIndexer::ScanDir;
 
class End {};
 
sub scan-dir(*@paths --> Supplyis export {
    supply {
        my %watched-dirs;
 
        CATCH { when X::IO { }default { warn $_done } }
 
        sub start-watching(IO::Path $dir{
            %watched-dirs{$dir.Str} = True;
            
            whenever $dir.watch {
                my $path-io = .path.IO;
                emit $path-io;
                when $path-io ~~ :e & :d {
                    add-dir($path-iounless %watched-dirs{$path-io.Str};
                }
                when $path-io ~~ :!e {
                    %watched-dirs{$path-io.Str}:delete
                }
            }
        }
 
        sub add-dir(IO::Path @dirs{
            my (@todo= @dirs;
 
            while @todo {
                my $next = @todo.shift;
 
                emit $next;
 
                next unless $next ~~ :e & :r & :d;
                start-watching($next);
 
                for $next.dir {
                    emit $_;
                    when .e && .d {
                        @todo.push($_);
                        start-watching($_);
                    }
                }
            }
 
        }
 
        add-dir(Array[IO::Path].new(@paths».IO));
        emit End;
    }
}