Created
November 24, 2017 10:06
-
-
Save claustres/6a186bd639c5e89b4a499f7bda9fa7ad to your computer and use it in GitHub Desktop.
Generic service entities
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import _ from 'lodash' | |
class Service { | |
constructor (generators = {}) { | |
this.generators = {} | |
_.forOwn(generators, (value, key) => { | |
this.registerGenerator(key, value) | |
}) | |
} | |
registerGenerator (type, generator) { | |
this.generators[type] = generator | |
} | |
unregisterGenerator (type) { | |
delete this.generators[type] | |
} | |
generate (type, ...options) { | |
let generator = this.generators[type] | |
if (!generator) return null | |
else return generator.apply(this, options) | |
} | |
} | |
export default Service |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment