Skip to content

Instantly share code, notes, and snippets.

@DScheglov
Created October 21, 2020 06:28
Show Gist options
  • Save DScheglov/707d681ac291b4dc6799309b4454726b to your computer and use it in GitHub Desktop.
Save DScheglov/707d681ac291b4dc6799309b4454726b to your computer and use it in GitHub Desktop.
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