var constants = process.binding('constants');
var fs = exports;
var Stream = require('stream').Stream;
+var EventEmitter = require('events').EventEmitter;
var kMinPoolSpace = 128;
var kPoolSize = 40 * 1024;
// Stat Change Watchers
+function StatWatcher() {
+ var self = this;
+ this._handle = new binding.StatWatcher();
+
+ this._handle.onchange = function(current, previous) {
+ self.emit('change', current, previous);
+ };
+
+ this._handle.onstop = function() {
+ self.emit('stop');
+ };
+}
+util.inherits(StatWatcher, EventEmitter);
+
+
+StatWatcher.prototype.start = function(filename, persistent, interval) {
+ this._handle.start(filename, persistent, interval);
+};
+
+
+StatWatcher.prototype.stop = function() {
+ this._handle.stop();
+};
+
+
var statWatchers = {};
+
fs.watchFile = function(filename) {
var stat;
var options;
if (statWatchers[filename]) {
stat = statWatchers[filename];
} else {
- statWatchers[filename] = new binding.StatWatcher();
+ statWatchers[filename] = new StatWatcher();
stat = statWatchers[filename];
stat.start(filename, options.persistent, options.interval);
}
Persistent<FunctionTemplate> StatWatcher::constructor_template;
-static Persistent<String> change_symbol;
-static Persistent<String> stop_symbol;
-
void StatWatcher::Initialize(Handle<Object> target) {
HandleScope scope;
Local<FunctionTemplate> t = FunctionTemplate::New(StatWatcher::New);
constructor_template = Persistent<FunctionTemplate>::New(t);
- constructor_template->Inherit(EventEmitter::constructor_template);
constructor_template->InstanceTemplate()->SetInternalFieldCount(1);
constructor_template->SetClassName(String::NewSymbol("StatWatcher"));
- change_symbol = NODE_PSYMBOL("change");
- stop_symbol = NODE_PSYMBOL("stop");
-
NODE_SET_PROTOTYPE_METHOD(constructor_template, "start", StatWatcher::Start);
NODE_SET_PROTOTYPE_METHOD(constructor_template, "stop", StatWatcher::Stop);
Handle<Value> argv[2];
argv[0] = Handle<Value>(BuildStatsObject(&watcher->attr));
argv[1] = Handle<Value>(BuildStatsObject(&watcher->prev));
- handler->Emit(change_symbol, 2, argv);
+ MakeCallback(handler->handle_, "onchange", 2, argv);
}
Handle<Value> StatWatcher::Stop(const Arguments& args) {
HandleScope scope;
StatWatcher *handler = ObjectWrap::Unwrap<StatWatcher>(args.Holder());
- handler->Emit(stop_symbol, 0, NULL);
+ MakeCallback(handler->handle_, "onstop", 0, NULL);
handler->Stop();
return Undefined();
}
namespace node {
-class StatWatcher : EventEmitter {
+class StatWatcher : ObjectWrap {
public:
static void Initialize(v8::Handle<v8::Object> target);
protected:
static v8::Persistent<v8::FunctionTemplate> constructor_template;
- StatWatcher() : EventEmitter() {
+ StatWatcher() : ObjectWrap() {
persistent_ = false;
path_ = NULL;
ev_init(&watcher_, StatWatcher::Callback);