generator.js 609 B

1234567891011121314151617181920212223242526272829303132
  1. 'use strict';
  2. const Promise = require('bluebird');
  3. function Generator() {
  4. this.id = 0;
  5. this.store = {};
  6. }
  7. Generator.prototype.list = function() {
  8. return this.store;
  9. };
  10. Generator.prototype.get = function(name) {
  11. return this.store[name];
  12. };
  13. Generator.prototype.register = function(name, fn) {
  14. if (!fn) {
  15. if (typeof name === 'function') {
  16. fn = name;
  17. name = `generator-${this.id++}`;
  18. } else {
  19. throw new TypeError('fn must be a function');
  20. }
  21. }
  22. if (fn.length > 1) fn = Promise.promisify(fn);
  23. this.store[name] = Promise.method(fn);
  24. };
  25. module.exports = Generator;