无限文件监视器 [英] Infinite file watcher

查看:159
本文介绍了无限文件监视器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我试图在Dart中实现文件监视器。问题是,我不知道如何使流永远的列表。在我的代码 print 语句仅在文件更改时触发一次。我尝试了 while(true){} ,但没有影响。

Hello I'm trying to implement file watcher in Dart. And the problem is that I can't figure out how to make forever lister for stream. In my code print statement fires only once, when file was changed. I tried while(true){} but didn't make affect.

import "dart:io";


void main(){
  List<String> paths = ['any.dart'];
  paths.forEach((fp){
    File f = new File(fp);
    f.watch().listen((e){
      print(e);
    });
  });
}

Dart信息: Dart VM版本:1.4.0 (Tue May 20 04:56:35 2014)onlinux_x64

推荐答案

查看watch函数的文档:

Please have a look into the doc of the watch function:

The implementation uses platform-dependent event-based APIs for receiving file-system notifications, thus behavior depends on the platform. 

* Windows: Uses ReadDirectoryChangesW. The implementation only 
    supports watching directories. Recursive watching is supported.

* `Linux`: Uses `inotify`. The implementation supports watching both 
    files and directories. Recursive watching is not supported.
    Note: When watching files directly, delete events might not happen
    as expected.

* `Mac OS`: Uses `FSEvents`. The implementation supports watching both 
    files and directories. Recursive watching is supported.

这意味着这不会在Windows上工作。对于你的问题,它说:

This means this won't work on Windows. For you problem it says:

The returned value is an endless broadcast [Stream], that only stops when
one of the following happends:

  * The [Stream] is canceled, e.g. by calling `cancel` on the
     [StreamSubscription].
  * The [FileSystemEntity] being watches, is deleted.

这意味着您的文件被删除,流被取消或这是一个错误,

This means either your file got deleted, the stream got canceled or this is a bug and you should file a ticket for this.

在Windows上没有任何反应。在Mac我得到这与您的代码:

On Windows there is no reaction for me. On Mac I get this with your code:

FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)
FileSystemModifyEvent('test_file_watcher.dart', contentChanged=true)


$ b b

如何测试你的代码?我简单地编辑和保存我的文件。所以它的工作对我很好。您使用哪个版本?

How do you test your code? I simple edited and saved my file. So it's working great for me. Which version are you using?

回报
Robert

Regards Robert

这篇关于无限文件监视器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆