Source: instantiators/AbstractInstantiator.js

  1. /**
  2. * Abstract class of all instantiator implementations. An Instantiator will create an instance of a module.
  3. * You cannot be sure, you get an actual instance of the object, since it could be possible to implement the module
  4. * as e.g. rpc module which wraps an API.
  5. * It will keep track of instances to react to circular dependencies.
  6. *
  7. * @author Wolfgang Felbermeier (@f3lang)
  8. */
  9. class AbstractInstantiator {
  10. constructor() {
  11. this.requestCollection = [];
  12. }
  13. /**
  14. * Will create a new instance of the requested module
  15. * @param {string} path The file path of the module
  16. * @param {object} config The injection config resolved by the module analyzer
  17. * @param {string} root The tree to work in
  18. * @param {string} requestId The unique request id to identify circular dependencies
  19. */
  20. getInstance(path, config, root, requestId) {
  21. throw Error('This is an abstract class, use a derived class instead');
  22. }
  23. /**
  24. * Will check, if the current request has already visited this stage once.
  25. * @param {string} path The file path of the module
  26. * @param {string} root The tree to work in
  27. * @param {string} requestId The unique request id to identify circular dependencies
  28. * @return {boolean} true, if a circular dependency occurred.
  29. */
  30. requestIsCircular(path, root, requestId) {
  31. if(this.requestCollection.indexOf(requestId + ':' + path + ':' + root) > -1) {
  32. console.warn("Warning: circular dependency detected for " + requestId + ':' + path + ':' + root);
  33. return true;
  34. }
  35. this.requestCollection.push(requestId + ':' + path + ':' + root);
  36. }
  37. }
  38. module.exports = AbstractInstantiator;
  39. module.exports.inject = [];