Add 'throttler' for gen-thread.js
parent
2cde41bb5f
commit
d60d4fc326
10
example.js
10
example.js
|
@ -22,3 +22,13 @@ function* test(thread)
|
||||||
}
|
}
|
||||||
|
|
||||||
gen.run(test, null, function(result) { console.log(result); });
|
gen.run(test, null, function(result) { console.log(result); });
|
||||||
|
|
||||||
|
function* test_throttle(thread)
|
||||||
|
{
|
||||||
|
yield thread.throttle(5);
|
||||||
|
console.log('at most 5');
|
||||||
|
yield setTimeout(thread, 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
for (var i = 0; i < 15; i++)
|
||||||
|
gen.run(test_throttle);
|
||||||
|
|
50
index.js
50
index.js
|
@ -1,17 +1,65 @@
|
||||||
module.exports.run = runThread;
|
module.exports.run = runThread;
|
||||||
module.exports.runParallel = runParallel;
|
module.exports.runParallel = runParallel;
|
||||||
|
|
||||||
|
var q = [];
|
||||||
|
var pending = [];
|
||||||
|
|
||||||
|
function finishq()
|
||||||
|
{
|
||||||
|
for (var i = 0; i < q.length; i++)
|
||||||
|
{
|
||||||
|
if (q[i]._done)
|
||||||
|
{
|
||||||
|
q.splice(i, 1);
|
||||||
|
i--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
while (pending.length > 0 && q.length < pending[0][1])
|
||||||
|
{
|
||||||
|
var t = pending.shift();
|
||||||
|
q.push(t[0]);
|
||||||
|
t[0]();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
var tid = 0;
|
||||||
function runThread(main, arg, done)
|
function runThread(main, arg, done)
|
||||||
{
|
{
|
||||||
var thread = function()
|
var thread = function()
|
||||||
{
|
{
|
||||||
// pass parameters as yield result
|
// pass parameters as yield result
|
||||||
var pass = Array.prototype.slice.call(arguments, 0);
|
var pass = Array.prototype.slice.call(arguments, 0);
|
||||||
var v = thread.gen.next(pass);
|
try
|
||||||
|
{
|
||||||
|
v = thread.gen.next(pass);
|
||||||
|
}
|
||||||
|
catch (e)
|
||||||
|
{
|
||||||
|
v = { done: 1 };
|
||||||
|
}
|
||||||
|
if (v.done)
|
||||||
|
{
|
||||||
|
thread._done = true;
|
||||||
|
finishq();
|
||||||
|
}
|
||||||
if (v.done && done)
|
if (v.done && done)
|
||||||
done(v.value);
|
done(v.value);
|
||||||
};
|
};
|
||||||
|
thread.id = tid++;
|
||||||
thread.gen = main(thread, arg);
|
thread.gen = main(thread, arg);
|
||||||
|
thread.throttle = function(count)
|
||||||
|
{
|
||||||
|
finishq();
|
||||||
|
if (q.length < count)
|
||||||
|
{
|
||||||
|
q.push(thread);
|
||||||
|
setTimeout(thread, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
pending.push([ thread, count ]);
|
||||||
|
}
|
||||||
|
};
|
||||||
thread();
|
thread();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue