Created
October 21, 2020 06:28
-
-
Save DScheglov/707d681ac291b4dc6799309b4454726b 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, inject = factory.inject) => container => | |
Array.isArray(inject) | |
? createInstance(factory, inject.map(getFrom(container))) | |
typeof inject === 'function' | |
? createInstance(factory, inject(container)) | |
: factory(container); | |
export const asClass = (constructor, inject = constructor.inject) => asFactory( | |
(...args) => Reflect.construct(contructor, args), | |
inject | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment