Created
October 21, 2020 06:17
-
-
Save DScheglov/487dd8d736c54aba8bb328bd9ae605b7 to your computer and use it in GitHub Desktop.
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
const getFrom = container => prop => container[prop]; | |
const createInstance = (factory, deps) => | |
deps.some(isPromise) | |
? Promise.all(deps).then(resolvedDeps => factory(...resolvedDeps)) | |
: factory(...deps); | |
export const asFactory = factory => container => | |
Array.isArray(factory.inject) | |
? createInstance(factory, factory.inject.map(getFrom(container))) | |
: factory(container); | |
export const asClass = constructor => asFactory( | |
(...args) => Reflect.construct(contructor, args) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment