You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
35 lines
570 B
35 lines
570 B
2 years ago
|
'use strict';
|
||
|
|
||
|
const Promise = require('bluebird');
|
||
|
|
||
|
class Generator {
|
||
|
constructor() {
|
||
|
this.id = 0;
|
||
|
this.store = {};
|
||
|
}
|
||
|
|
||
|
list() {
|
||
|
return this.store;
|
||
|
}
|
||
|
|
||
|
get(name) {
|
||
|
return this.store[name];
|
||
|
}
|
||
|
|
||
|
register(name, fn) {
|
||
|
if (!fn) {
|
||
|
if (typeof name === 'function') {
|
||
|
fn = name;
|
||
|
name = `generator-${this.id++}`;
|
||
|
} else {
|
||
|
throw new TypeError('fn must be a function');
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (fn.length > 1) fn = Promise.promisify(fn);
|
||
|
this.store[name] = Promise.method(fn);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
module.exports = Generator;
|