-
-
Save b2gills/8e0187f873b1663b5075e32e30229c82 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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