/
Notification.pm6
38 lines (33 loc) · 1.06 KB
/
Notification.pm6
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
# stub what we need now
my class Supplier { ... }
my enum FileChangeEvent (:FileChanged(1), :FileRenamed(2));
my class IO::Notification {
my class FileWatchCancellation is repr('AsyncTask') { }
class Change {
has $.path;
has $.event;
multi method gist(Change:D:) {
"$.path: $.event";
}
method IO { $!path.IO }
}
method watch-path(Str() $path, :$scheduler = $*SCHEDULER) {
my $is-dir = $path.IO.d;
my $s = Supplier.new;
nqp::watchfile(
$scheduler.queue(:hint-affinity),
-> \path, \rename, \err {
if err {
$s.quit(err);
}
else {
my $event = rename ?? FileRenamed !! FileChanged;
my $full-path = ( $is-dir and path ) ?? $*SPEC.catdir($path, path) !! $path;
$s.emit(Change.new(:path($full-path), :$event));
}
},
$path, FileWatchCancellation);
$s.Supply
}
}
# vim: ft=perl6 expandtab sw=4