1. # stub what we need now
  2. my class Supplier { ... }
  3. my enum FileChangeEvent (:FileChanged(1), :FileRenamed(2));
  4. my class IO::Notification {
  5. my class FileWatchCancellation is repr('AsyncTask') { }
  6. class Change {
  7. has $.path;
  8. has $.event;
  9. multi method gist(Change:D:) {
  10. "$.path: $.event";
  11. }
  12. method IO { $!path.IO }
  13. }
  14. method watch-path(Str() $path, :$scheduler = $*SCHEDULER) {
  15. my $s = Supplier.new;
  16. nqp::watchfile(
  17. $scheduler.queue,
  18. -> \path, \rename, \err {
  19. if err {
  20. $s.quit(err);
  21. }
  22. else {
  23. my $event = rename ?? FileRenamed !! FileChanged;
  24. $s.emit(Change.new(:path($*SPEC.catdir($path, path)), :$event));
  25. }
  26. },
  27. $path, FileWatchCancellation);
  28. $s.Supply
  29. }
  30. }