Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

switched activeSuspender to use a stack, instead of single reference

  • Loading branch information...
commit def54e83595f10c8438c20020850f32eec34d9a4 1 parent 9d99fcd
@jmar777 authored
Showing with 28 additions and 14 deletions.
  1. +28 −14 lib/suspend.js
View
42 lib/suspend.js
@@ -45,12 +45,12 @@ suspend.run = function run(generator, callback) {
};
suspend.resume = function resumeFactory() {
- if (!activeSuspender) {
+ var suspender = getActiveSuspender();
+ if (!suspender) {
throw new Error('resume() must be called from the generator body.');
}
- var alreadyResumed = false,
- suspender = activeSuspender;
+ var alreadyResumed = false;
return function resume() {
if (alreadyResumed) {
@@ -63,30 +63,29 @@ suspend.resume = function resumeFactory() {
suspend.resumeRaw = function resumeRawFactory() {
var resume = suspend.resume.apply(this, arguments);
- activeSuspender.rawResume = true;
+ getActiveSuspender().rawResume = true;
return resume;
};
suspend.fork = function fork() {
- if (!activeSuspender) {
+ var suspender = getActiveSuspender();
+ if (!suspender) {
throw new Error('fork() must be called from the generator body.');
}
- return activeSuspender.forkFactory();
+ return suspender.forkFactory();
};
suspend.join = function join() {
- if (!activeSuspender) {
+ var suspender = getActiveSuspender();
+ if (!suspender) {
throw new Error('join() must be called from the generator body.');
}
- if (activeSuspender.pendingJoin) {
+ if (suspender.pendingJoin) {
throw new Error('There is already a join() pending unresolved forks.');
}
- activeSuspender.join();
+ suspender.join();
};
-// keep track of the currently active generator (used by the resumer factory).
-var activeSuspender = null;
-
/**
* Constructor function used for "wrapping" generator. Manages the state and
* interactions with a suspend-wrapped generator.
@@ -148,7 +147,7 @@ Suspender.prototype.handleYield = function handleYield(ret) {
*/
Suspender.prototype.nextOrThrow = function next(val, isError) {
this.syncResume = true;
- activeSuspender = this;
+ setActiveSuspender(this);
var ret;
try {
ret = isError ? this.iterator.throw(val) : this.iterator.next(val);
@@ -160,7 +159,7 @@ Suspender.prototype.nextOrThrow = function next(val, isError) {
}
} finally {
this.syncResume = false;
- activeSuspender = null;
+ clearActiveSuspender();
}
// everything was ok, so keep going
this.handleYield(ret);
@@ -237,6 +236,21 @@ Suspender.prototype.join = function join() {
this.resume(err, results);
};
+// keep track of the currently active generator (used by the resumer factory).
+var suspenderStack = [];
+
+function setActiveSuspender(suspender) {
+ suspenderStack.push(suspender);
+}
+
+function getActiveSuspender(suspender) {
+ return suspenderStack[suspenderStack.length - 1];
+}
+
+function clearActiveSuspender() {
+ suspenderStack.pop();
+}
+
function isGeneratorFunction(v) {
return v && v.constructor && v.constructor.name === 'GeneratorFunction';
}
Please sign in to comment.
Something went wrong with that request. Please try again.