Skip to content

Instantly share code, notes, and snippets.

@b2gills

b2gills/test.p6 Secret

Last active December 8, 2018 19:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save b2gills/8e0187f873b1663b5075e32e30229c82 to your computer and use it in GitHub Desktop.
Save b2gills/8e0187f873b1663b5075e32e30229c82 to your computer and use it in GitHub Desktop.
# http://sprunge.us/Yqnpg9
# http://sprunge.us/a425r1
sub tree-watch ( IO() $root ) {
supply {
# make sure there is only one tap for each file/dir
my %taps;
# make sure that only one thread is changing %taps
my Lock $lock .= new;
sub add ( IO $path ) {
# make sure there isn't some form of race
return if $lock.protect: { %taps{$path} };
if $path.d {
# add the existing files in the directory in parrallel.
# that way we don't have to wait for them all to be added initially
start { add $_ for $path.dir }
# the lock.protect is only needed to add the tap
$lock.protect:
{
%taps{$path} = do whenever $path.watch
{
say 'dir tap fired';
# a file might have been added
start { add $_ for $path.dir }
.emit;
LAST $lock.protect: { %taps{$path}:delete };
}
}
} else {
# the lock.protect is only needed to add the tap
$lock.protect:
{
%taps{$path} = do whenever $path.watch
{
.emit;
LAST $lock.protect: { %taps{$path}:delete };
}
}
}
}
add( $root );
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment