Fix for callbacks called synchronously before yield
parent
006193174f
commit
7912f93f13
|
@ -1,8 +1,11 @@
|
||||||
var gen = require('./gen-thread.js');
|
var gen = require('./index.js');
|
||||||
|
|
||||||
function* test()
|
function* test()
|
||||||
{
|
{
|
||||||
console.log('start');
|
console.log('start');
|
||||||
|
// Should give no "Generator is already running" error
|
||||||
|
var v = yield gen.cb()('expected value');
|
||||||
|
console.log(v[0] === 'expected value');
|
||||||
var cb = gen.unsafe();
|
var cb = gen.unsafe();
|
||||||
console.log([ 'next', yield setTimeout(function() { cb('zhopa', 123); }, 500) ]);
|
console.log([ 'next', yield setTimeout(function() { cb('zhopa', 123); }, 500) ]);
|
||||||
var args = yield gen.runParallel([
|
var args = yield gen.runParallel([
|
||||||
|
|
28
index.js
28
index.js
|
@ -30,11 +30,16 @@ module.exports.throttle = function(count)
|
||||||
|
|
||||||
function runThread(generator, onsuccess, onerror)
|
function runThread(generator, onsuccess, onerror)
|
||||||
{
|
{
|
||||||
var thread = function() { continueThread.apply(thread, arguments) };
|
var thread = function()
|
||||||
|
{
|
||||||
|
thread._current = null;
|
||||||
|
continueThread.apply(thread, arguments);
|
||||||
|
};
|
||||||
thread._gen = generator.next ? generator : generator();
|
thread._gen = generator.next ? generator : generator();
|
||||||
thread._finishThrottleQueue = finishThrottleQueue.bind(thread);
|
thread._finishThrottleQueue = finishThrottleQueue.bind(thread);
|
||||||
thread._onsuccess = onsuccess;
|
thread._onsuccess = onsuccess;
|
||||||
thread._onerror = onerror;
|
thread._onerror = onerror;
|
||||||
|
thread._running = false;
|
||||||
callGen(thread, 'next', []);
|
callGen(thread, 'next', []);
|
||||||
return thread;
|
return thread;
|
||||||
}
|
}
|
||||||
|
@ -52,16 +57,35 @@ function getStack(fn)
|
||||||
|
|
||||||
function callGen(thread, method, arg)
|
function callGen(thread, method, arg)
|
||||||
{
|
{
|
||||||
|
if (thread._running)
|
||||||
|
{
|
||||||
|
// callback called while generator is already running
|
||||||
|
thread._result = [ method, arg ];
|
||||||
|
return;
|
||||||
|
}
|
||||||
var v;
|
var v;
|
||||||
|
thread._running = true;
|
||||||
current = thread;
|
current = thread;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
v = thread._gen[method](arg);
|
while (1)
|
||||||
|
{
|
||||||
|
v = thread._gen[method](arg);
|
||||||
|
if (!v.done && thread._result)
|
||||||
|
{
|
||||||
|
method = thread._result[0];
|
||||||
|
arg = thread._result[1];
|
||||||
|
thread._result = null;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
catch (e)
|
catch (e)
|
||||||
{
|
{
|
||||||
v = { error: e };
|
v = { error: e };
|
||||||
}
|
}
|
||||||
|
thread._running = false;
|
||||||
current = null;
|
current = null;
|
||||||
if (v.done || v.error)
|
if (v.done || v.error)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue