Comment detail

ファイル更新の監視 (Nested Flatten)
Cc, Ciはそれぞれ const Cc = Components.classes; const Ci = Components.interfaces; されているとして、 var fw = new FileWatch("/home/zigorou/hoge.txt", 5); fw.watch(); で5秒おきにErrorConsoleに対して更新されてればmodified表示。 fw.unwatch()で監視止める。 でももっと奇麗に書き方ありそうな気がする。Observerがそもそもあったりして(ぇ
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
var FileWatch = function(filename, duration) {
  this.file = Cc["@mozilla.org/file/local;1"].createInstance(Ci.nsILocalFile);
  this.file.initWithPath(filename);
  this.duration = duration;
};

FileWatch.prototype = {
  iid: null,
  lastTime: null,
  watch: function() {
    this.lastTime = (new Date()).getTime();
    var self = this;
    this.iid = setInterval(function() { self.watchFile(); }, this.duration * 1000);
  },
  watchFile: function() {
    if (this.file.lastModifiedTime - this.lastTime > 0) {
      this.log("modified");
    }

    this.lastTime = (new Date()).getTime();
  },
  log: function(message) {
    Cc["@mozilla.org/consoleservice;1"].getService(Ci.nsIConsoleService).logStringMessage(message);
  },
  unwatch: function() {
    clearInterval(this.iid);
  }
};

Index

Feed

Other

Link

Pathtraq

loading...