container.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. /**
  2. * Container to be used by this library for inversion control. If container was not implicitly set then by default
  3. * container simply creates a new instance of the given class.
  4. */
  5. var defaultContainer = new (/** @class */ (function () {
  6. function class_1() {
  7. this.instances = [];
  8. }
  9. class_1.prototype.get = function (someClass) {
  10. var instance = this.instances.find(function (instance) { return instance.type === someClass; });
  11. if (!instance) {
  12. instance = { type: someClass, object: new someClass() };
  13. this.instances.push(instance);
  14. }
  15. return instance.object;
  16. };
  17. return class_1;
  18. }()))();
  19. var userContainer;
  20. var userContainerOptions;
  21. /**
  22. * Sets container to be used by this library.
  23. */
  24. export function useContainer(iocContainer, options) {
  25. userContainer = iocContainer;
  26. userContainerOptions = options;
  27. }
  28. /**
  29. * Gets the IOC container used by this library.
  30. */
  31. export function getFromContainer(someClass) {
  32. if (userContainer) {
  33. try {
  34. var instance = userContainer.get(someClass);
  35. if (instance)
  36. return instance;
  37. if (!userContainerOptions || !userContainerOptions.fallback)
  38. return instance;
  39. }
  40. catch (error) {
  41. if (!userContainerOptions || !userContainerOptions.fallbackOnErrors)
  42. throw error;
  43. }
  44. }
  45. return defaultContainer.get(someClass);
  46. }
  47. //# sourceMappingURL=container.js.map