generator.js 570 B

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