Last active
December 1, 2015 18:30
-
-
Save gobwas/a044a37319ff381afccf to your computer and use it in GitHub Desktop.
Generator object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var inherits = require("inherits-js"); | |
var Generator; | |
/** | |
* @abstract | |
* @class Generator | |
* @constructor | |
*/ | |
Generator = function() { | |
var self = this; | |
var g = function*() { | |
while (!self.isDone()) { | |
yield self.doNext(); | |
} | |
}; | |
return g(); | |
}; | |
Generator.prototype = { | |
constructor: Generator, | |
/** | |
* @abstract | |
*/ | |
isDone: function() { | |
throw new TypeError("Method 'isDone' must be implemented"); | |
}, | |
/** | |
* @abstract | |
*/ | |
doNext: function() { | |
throw new TypeError("Method 'genNext' must be implemented"); | |
} | |
}; | |
Object.setPrototypeOf(Generator.prototype, Object.getPrototypeOf(function*(){})); | |
Generator.extend = function(prots, statics) { | |
return inherits(this, prots, statics); | |
}; | |
Generator.DEFAULTS = {}; | |
module.exports = Generator; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment