Skip to content

Commit

Permalink
Remove StatWatcher's dep on C++ EventEmitter
Browse files Browse the repository at this point in the history
  • Loading branch information
ry committed Jul 19, 2011
1 parent 2e16ae7 commit 0a3fc1d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
29 changes: 28 additions & 1 deletion lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ var binding = process.binding('fs');
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;
Expand Down Expand Up @@ -587,8 +588,34 @@ fs.writeFileSync = function(path, data, encoding) {

// 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;
Expand All @@ -608,7 +635,7 @@ fs.watchFile = function(filename) {
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);
}
Expand Down
11 changes: 2 additions & 9 deletions src/node_stat_watcher.cc
Original file line number Diff line number Diff line change
Expand Up @@ -31,21 +31,14 @@ using namespace v8;

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);

Expand All @@ -61,7 +54,7 @@ void StatWatcher::Callback(EV_P_ ev_stat *watcher, int revents) {
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);
}


Expand Down Expand Up @@ -113,7 +106,7 @@ Handle<Value> StatWatcher::Start(const Arguments& args) {
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();
}
Expand Down
4 changes: 2 additions & 2 deletions src/node_stat_watcher.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@

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);
Expand Down

0 comments on commit 0a3fc1d

Please sign in to comment.