Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Remove StatWatcher's dep on C++ EventEmitter

  • Loading branch information...
commit 0a3fc1d9c8becc32c63ae736ca2b3719a3d03c5b 1 parent 2e16ae7
@ry ry authored
Showing with 32 additions and 12 deletions.
  1. +28 −1 lib/fs.js
  2. +2 −9 src/node_stat_watcher.cc
  3. +2 −2 src/node_stat_watcher.h
View
29 lib/fs.js
@@ -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;
@@ -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;
@@ -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);
}
View
11 src/node_stat_watcher.cc
@@ -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);
@@ -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);
}
@@ -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();
}
View
4 src/node_stat_watcher.h
@@ -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);
Please sign in to comment.
Something went wrong with that request. Please try again.