From e9b15ce53bf29c9b8b8c1044e48b4c89bd2dd4a3 Mon Sep 17 00:00:00 2001 From: Joongi Kim Date: Wed, 30 Mar 2016 22:13:52 +0900 Subject: [PATCH] Unified loopUntilNoEvents() with run() in the native code. * 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. --- index.js | 35 +++++++++++++++++++---------------- src/deasync.cc | 15 +++------------ 2 files changed, 22 insertions(+), 28 deletions(-) diff --git a/index.js b/index.js index 0208cfa..fd3ce9d 100644 --- a/index.js +++ b/index.js @@ -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]; @@ -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(); + } }; }()); diff --git a/src/deasync.cc b/src/deasync.cc index 09f68b2..cc7f06b 100644 --- a/src/deasync.cc +++ b/src/deasync.cc @@ -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 more = Nan::New(_more); + info.GetReturnValue().Set(more); } static NAN_MODULE_INIT(init) { Nan::Set(target, Nan::New("run").ToLocalChecked(), Nan::GetFunction(Nan::New(Run)).ToLocalChecked()); - Nan::Set(target, Nan::New("loopUntilNoEvents").ToLocalChecked(), Nan::GetFunction(Nan::New(LoopUntilNoEvents)).ToLocalChecked()); } NODE_MODULE(deasync, init)