UNPKG

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