summaryrefslogtreecommitdiff
path: root/lib/MaildirIndexer/ScanDir.pm6
blob: a8dfe977b65a6dcc191b7ef4576045e5994dce07 (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
use v6.d;
unit module MaildirIndexer::ScanDir;
 
sub scan-dir(IO() $path --> Supplyis export {
    supply {
        my %watched-dirs;
 
        sub add-dir(IO::Path $dir:$initial{
            %watched-dirs{$dir} = True;
 
            CATCH { when X::IO::Dir { }default { warn .perl } }
            
            whenever $dir.watch {
                my $path-io = .path.IO;
                emit $path-io;
                when $path-io.e && $path-io.d {
                    add-dir($path-iounless %watched-dirs{$path-io};
                }
                when !$path-io.e {
                    %watched-dirs{$path-io}:delete
                }
            }
 
            for $dir.dir {
                emit $_;
                when .e && .d {
                    add-dir($_);
                }
            }
        }
 
        add-dir($path);
    }
}