Add 'throttler' for gen-thread.js

master
Vitaliy Filippov 2016-07-17 00:35:35 +03:00
parent 2cde41bb5f
commit d60d4fc326
2 changed files with 59 additions and 1 deletions

View File

@ -22,3 +22,13 @@ function* test(thread)
}
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);

View File

@ -1,17 +1,65 @@
module.exports.run = runThread;
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)
{
var thread = function()
{
// pass parameters as yield result
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)
done(v.value);
};
thread.id = tid++;
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();
}