aboutsummaryrefslogtreecommitdiff
path: root/lib/ScanDir.rakumod
diff options
context:
space:
mode:
authordakkar <dakkar@thenautilus.net>2024-01-20 17:30:25 +0000
committerdakkar <dakkar@thenautilus.net>2024-01-20 17:35:21 +0000
commit491dc1aeab9b445ee28f972e19a1cbb4cb9f3af7 (patch)
treeb4213dfea7e02f16e286b6b814dc196c19714cdf /lib/ScanDir.rakumod
parentquite the ScanDir supply when files are being modified (diff)
downloadmedia-control-491dc1aeab9b445ee28f972e19a1cbb4cb9f3af7.tar.gz
media-control-491dc1aeab9b445ee28f972e19a1cbb4cb9f3af7.tar.bz2
media-control-491dc1aeab9b445ee28f972e19a1cbb4cb9f3af7.zip
look at fs on demand, don't watch it
ScanDir (well, fs notifications in raku) is too slow to keep up with actual fs changes (especially when e.g. a file is being downloaded) there's actually no need to watch fs changes, we can just sync the db with the file system we look at each directory
Diffstat (limited to 'lib/ScanDir.rakumod')
-rw-r--r--lib/ScanDir.rakumod58
1 files changed, 0 insertions, 58 deletions
diff --git a/lib/ScanDir.rakumod b/lib/ScanDir.rakumod
deleted file mode 100644
index 0f33a8b..0000000
--- a/lib/ScanDir.rakumod
+++ /dev/null
@@ -1,58 +0,0 @@
-use v6.d;
-unit module ScanDir;
-
-class End {};
-
-sub scan-dir(*@paths --> Supply) is export {
- my $s = supply {
- my %watched-dirs;
-
- CATCH { when X::IO { }; default { warn $_ } }
-
- sub start-watching(IO::Path $dir) {
- return unless $dir ~~ :e;
- return if %watched-dirs{$dir.Str};
- %watched-dirs{$dir.Str} = True;
-
- whenever $dir.watch {
- my $path-io = .path.IO;
- emit $path-io;
- when $path-io ~~ :e & :d {
- add-dir($path-io) unless %watched-dirs{$path-io.Str};
- }
- when $path-io ~~ :!e {
- %watched-dirs{$path-io.Str}:delete
- }
- }
- }
-
- sub add-dir(*@todo) {
- while @todo {
- my $next = @todo.shift;
-
- next unless $next ~~ :e & :r & :d;
- start-watching($next);
-
- for $next.dir {
- emit $_;
- when .e && .d {
- @todo.push($_);
- start-watching($_);
- }
- }
- }
-
- }
-
- add-dir(@paths».IO);
- emit End;
- };
-
- # let's not return multiple events for the same path too quickly,
- # otherwise the consumer will get overwhelmed when (for example) a
- # large file is being written
- return $s.unique(
- with => sub { $^a !~~ End && $^b !~~ End && $^a eq $^b },
- expires => 0.1,
- );
-}