Skip to content

Commit

Permalink
Unified loopUntilNoEvents() with run() in the native code.
Browse files Browse the repository at this point in the history
 * Just return the return value of uv_run() and let the javascript-side
   code to loop, including process._tickDomainCallback() like
   loopWhile().

 * Fixed some whitespace clutters.
  • Loading branch information
achimnol committed Mar 30, 2016
1 parent de00325 commit e9b15ce
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 28 deletions.
35 changes: 19 additions & 16 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@
* Copyright 2014-2015 Abbr
* Released under the MIT license
*/

(function () {

var fs = require('fs'),
path = require('path'),
binding;

// Seed random numbers [gh-82] if on Windows. See https://github.com/laverdet/node-fibers/issues/82
if(process.platform === 'win32') Math.random();


// Look for binary for this platform
var nodeV = 'node-' + /[0-9]+\.[0-9]+/.exec(process.versions.node)[0];
var nodeVM = 'node-' + /[0-9]+/.exec(process.versions.node)[0];
Expand Down Expand Up @@ -50,32 +50,35 @@
function cb(e, r) {
err = e;
res = r;
done = true;
done = true;
}
}
}

module.exports = deasync;

module.exports.sleep = deasync(function(timeout, done) {
setTimeout(done, timeout);
});

module.exports.runLoopOnce = function(){
process._tickDomainCallback();
binding.run();
};

module.exports.loopUntilNoEvents = function(){
process._tickDomainCallback();
binding.loopUntilNoEvents();
var more = false;
do {
process._tickDomainCallback();
more = binding.run();
} while (more);
};

module.exports.loopWhile = function(pred){
while(pred()){
process._tickDomainCallback();
if(pred()) binding.run();
}
while(pred()){
process._tickDomainCallback();
if(pred()) binding.run();
}
};

}());
15 changes: 3 additions & 12 deletions src/deasync.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@ using namespace v8;

NAN_METHOD(Run) {
Nan::HandleScope scope;
uv_run(uv_default_loop(), UV_RUN_ONCE);
info.GetReturnValue().Set(Nan::Undefined());
}

NAN_METHOD(LoopUntilNoEvents) {
Nan::HandleScope scope;
bool more = false;
do {
more = uv_run(uv_default_loop(), UV_RUN_ONCE);
} while (more);
info.GetReturnValue().Set(Nan::Undefined());
bool _more = uv_run(uv_default_loop(), UV_RUN_ONCE);
v8::Local<v8::Boolean> more = Nan::New(_more);
info.GetReturnValue().Set(more);
}

static NAN_MODULE_INIT(init) {
Nan::Set(target, Nan::New("run").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(Run)).ToLocalChecked());
Nan::Set(target, Nan::New("loopUntilNoEvents").ToLocalChecked(), Nan::GetFunction(Nan::New<FunctionTemplate>(LoopUntilNoEvents)).ToLocalChecked());
}

NODE_MODULE(deasync, init)

0 comments on commit e9b15ce

Please sign in to comment.