Comparing version 1.1.2 to 1.2.0
/** | ||
* sync easy loop | ||
*/ | ||
var easyWhile = require('./easy-while'); | ||
var easyFor = require('./easy-for'); | ||
function loop(obj, func, concurrency, last){ | ||
var max = 0; | ||
var isArr = obj instanceof Array; | ||
var keys = null; | ||
if(obj instanceof Array) max = obj.length; | ||
else if(obj instanceof Object) | ||
if(typeof obj === 'function') | ||
{ | ||
keys = Object.keys(obj); | ||
max = keys.length; | ||
if(typeof concurrency === 'function') last = concurrency; | ||
easyWhile(obj, func, last); | ||
} | ||
else throw "first argument is not Array or Object => " + obj; | ||
if(typeof concurrency === 'function') | ||
else | ||
{ | ||
last = concurrency; | ||
concurrency = 1; | ||
easyFor(obj, func, concurrency, last); | ||
} | ||
else | ||
{ | ||
if(!concurrency || isNaN(concurrency)) concurrency = 1; | ||
else concurrency = parseInt(concurrency); | ||
} | ||
var count = {start:0, end:0, processing:0}; | ||
var next = function(err){ | ||
count.processing--; | ||
count.end++; | ||
if(count.end >= max || err) | ||
{ | ||
if(last) | ||
{ | ||
if(err) last(err); | ||
else last(); | ||
} | ||
} | ||
else run(); | ||
}; | ||
var run = function(){ | ||
if(max > count.start) | ||
{ | ||
count.processing++; | ||
if(isArr) func(count.start, obj[count.start++], next); | ||
else func(keys[count.start], obj[keys[count.start++]], next); | ||
if(max > count.start && count.processing < concurrency) run(); | ||
} | ||
}; | ||
max == 0 ? next() : run(); | ||
} | ||
loop["for"] = loop["while"] = loop["loop"] = loop["forEach"] = loop; | ||
loop["for"] = loop["while"] = loop["loop"] = loop["forEach"] = loop; | ||
module.exports = loop; |
{ | ||
"name": "easy-loop", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "Easy sync loop processing for Node.js", | ||
@@ -5,0 +5,0 @@ "main": "lib/easy-loop", |
@@ -9,3 +9,3 @@ | ||
1) Method | ||
: 모두 같은 동작이며 편리한 메소드를 호출하세요.(All same) | ||
: 모두 같은 동작이며 편리한 메소드를 호출하세요. | ||
@@ -16,3 +16,3 @@ var loop = require('easy-loop'); | ||
2) Arguments | ||
(1) Array or Object - require | ||
(1) Array or Object or function - require | ||
(2) process function - require | ||
@@ -188,2 +188,40 @@ (3) concurrency number - option (default : 1) | ||
Result : Now Second : 5 | ||
*/ | ||
var num = 0; | ||
console.log("Case7 Start => while and 3 arguments"); | ||
console.log("Start : Now Second : ", new Date().getSeconds()); | ||
loop.while(function(){ | ||
return num < 5; | ||
}, function(next){ | ||
setTimeout(function(){ | ||
console.log("Date : ", new Date().getSeconds()); | ||
num++; | ||
next(); | ||
}, 1000); | ||
}, function(err){ | ||
console.log("err : ", err); | ||
console.log("Case 7 result"); | ||
console.log("Result : Now Second : ", new Date().getSeconds()); | ||
}); | ||
console.log("Case7 End"); | ||
/* | ||
결과(Result) - 5초 소요(run time : 5 second) | ||
Case7 Start => while and 3 arguments | ||
Start : Now Second : 20 | ||
Case7 End | ||
Date : 21 | ||
Date : 22 | ||
Date : 23 | ||
Date : 24 | ||
Date : 25 | ||
err : undefined | ||
Case 7 result | ||
Result : Now Second : 25 | ||
*/ |
@@ -81,5 +81,5 @@ //var fs = require('fs'); | ||
*/ | ||
var arr = []; | ||
@@ -93,3 +93,3 @@ for(var i=0; i<10000; i++) arr.push(i); | ||
next(); | ||
}, 100); | ||
}, 1000); | ||
}, 10000, function(err){ | ||
@@ -101,2 +101,21 @@ console.log("err : ", err); | ||
console.log("Case6 End"); | ||
*/ | ||
var num = 0; | ||
console.log("Case7 Start => while and 3 arguments"); | ||
console.log("Start : Now Second : ", new Date().getSeconds()); | ||
loop.while(function(){ | ||
return num < 5; | ||
}, function(next){ | ||
setTimeout(function(){ | ||
console.log("Date : ", new Date().getSeconds()); | ||
num++; | ||
next(); | ||
}, 1000); | ||
}, function(err){ | ||
console.log("err : ", err); | ||
console.log("Case 7 result"); | ||
console.log("Result : Now Second : ", new Date().getSeconds()); | ||
}); | ||
console.log("Case7 End"); | ||
176
224
11389
8