Skip to content

Instantly share code, notes, and snippets.

@gobwas
Last active December 1, 2015 18:30
Show Gist options
  • Save gobwas/a044a37319ff381afccf to your computer and use it in GitHub Desktop.
Save gobwas/a044a37319ff381afccf to your computer and use it in GitHub Desktop.
Generator object
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