FileWatcher.hx 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package asys;
  2. import haxe.Error;
  3. import haxe.NoData;
  4. import haxe.async.*;
  5. import haxe.io.FilePath;
  6. typedef FileWatcherNative =
  7. #if doc_gen
  8. {function ref():Void; function unref():Void;};
  9. #elseif eval
  10. eval.uv.FileWatcher;
  11. #elseif hl
  12. hl.uv.FileWatcher;
  13. #elseif neko
  14. neko.uv.FileWatcher;
  15. #else
  16. #error "file watcher not supported on this platform"
  17. #end
  18. /**
  19. File watchers can be obtained with the `asys.FileSystem.watch` method.
  20. Instances of this class will emit signals whenever any file in their watched
  21. path is modified.
  22. **/
  23. class FileWatcher {
  24. /**
  25. Emitted when a watched file is modified.
  26. **/
  27. public final changeSignal:Signal<FileWatcherEvent> = new ArraySignal();
  28. /**
  29. Emitted when `this` watcher is fully closed. No further signals will be
  30. emitted.
  31. **/
  32. public final closeSignal:Signal<NoData> = new ArraySignal();
  33. /**
  34. Emitted when an error occurs.
  35. **/
  36. public final errorSignal:Signal<Error> = new ArraySignal();
  37. private var native:FileWatcherNative;
  38. private function new(filename:FilePath, recursive:Bool) {
  39. #if !doc_gen
  40. native = new FileWatcherNative(filename, recursive, (err, event) -> {
  41. if (err != null)
  42. return errorSignal.emit(err);
  43. changeSignal.emit(event);
  44. });
  45. #end
  46. }
  47. /**
  48. Closes `this` watcher. This operation is asynchronous and will emit the
  49. `closeSignal` once done. If `listener` is given, it will be added to the
  50. `closeSignal`.
  51. **/
  52. public function close(?listener:Listener<NoData>):Void {
  53. if (listener != null)
  54. closeSignal.once(listener);
  55. #if doc_gen
  56. var err:haxe.Error = null;
  57. ({
  58. #else
  59. native.close((err, _) -> {
  60. #end
  61. if (err != null)
  62. errorSignal.emit(err);
  63. closeSignal.emit(new NoData());
  64. });
  65. }
  66. public function ref():Void {
  67. native.ref();
  68. }
  69. public function unref():Void {
  70. native.unref();
  71. }
  72. }