Skip to content

Commit

Permalink
Update to nan version 2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Jonas Dohse committed Aug 5, 2015
1 parent c56e8b9 commit 7692532
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 27 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"version": "0.2.4",
"dependencies": {
"bindings": "1.1.0",
"nan": "1.8.4"
"nan": "2.0.0"
},
"devDependencies": {
"should": "1.2.1",
Expand Down
46 changes: 20 additions & 26 deletions toobusy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ static uint32_t s_currentLag;
static uint64_t s_lastMark;

NAN_METHOD(TooBusy) {
NanScope();
Nan::HandleScope scope;
// No HandleScope required, because this function allocates no
// v8 classes that reside on the heap.
bool block = false;
Expand All @@ -36,39 +36,37 @@ NAN_METHOD(TooBusy) {
double r = (rand() / (double) RAND_MAX) * 100.0;
if (r < pctToBlock) block = true;
}
NanReturnValue(NanNew(block));
info.GetReturnValue().Set(Nan::New(block));
}

NAN_METHOD(ShutDown) {
// No HandleScope required, because this function allocates no
// v8 classes that reside on the heap.

uv_timer_stop(&s_timer);

NanReturnUndefined();
}

NAN_METHOD(Lag) {
NanScope();
NanReturnValue(NanNew(s_currentLag));
Nan::HandleScope scope;
info.GetReturnValue().Set(Nan::New(s_currentLag));
}

NAN_METHOD(HighWaterMark) {
NanScope();
if (args.Length() >= 1) {
if (!args[0]->IsNumber()) {
NanThrowError("expected numeric first argument");
NanReturnUndefined();
Nan::HandleScope scope;
if (info.Length() >= 1) {
if (!info[0]->IsNumber()) {
Nan::ThrowError("expected numeric first argument");
return;
}
int hwm = args[0]->Int32Value();
int hwm = info[0]->Int32Value();
if (hwm < 10) {
NanThrowError("maximum lag should be greater than 10ms");
NanReturnUndefined();
Nan::ThrowError("maximum lag should be greater than 10ms");
return;
}
HIGH_WATER_MARK_MS = hwm;
}

NanReturnValue(NanNew(HIGH_WATER_MARK_MS));
info.GetReturnValue().Set(Nan::New(HIGH_WATER_MARK_MS));
}

static void every_second(uv_timer_t* handle)
Expand All @@ -92,17 +90,13 @@ static void every_second(uv_timer_t* handle, int) {
}
#pragma GCC diagnostic pop

extern "C" void init(Handle<Object> exports) {
NanScope();

exports->Set(NanNew("toobusy"),
NanNew<FunctionTemplate>(TooBusy)->GetFunction());
exports->Set(NanNew("shutdown"),
NanNew<FunctionTemplate>(ShutDown)->GetFunction());
exports->Set(NanNew("lag"),
NanNew<FunctionTemplate>(Lag)->GetFunction());
exports->Set(NanNew("maxLag"),
NanNew<FunctionTemplate>(HighWaterMark)->GetFunction());
NAN_MODULE_INIT(init) {
Nan::HandleScope scope;

Nan::SetMethod(exports, "toobusy", TooBusy);
Nan::SetMethod(exports, "shutdown", ShutDown);
Nan::SetMethod(exports, "lag", Lag);
Nan::SetMethod(exports, "maxLag", HighWaterMark);

uv_timer_init(uv_default_loop(), &s_timer);
uv_timer_start(&s_timer, every_second, POLL_PERIOD_MS, POLL_PERIOD_MS);
Expand Down

0 comments on commit 7692532

Please sign in to comment.