bff028020a90bb306073fa0d339d029bee80aa3f1ef5fc5faaaad86fed7144241275e31daec76c0a9594cc0ede2a84c8289526f97e7f77d4a37ace04f1660d 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. export const collection = new Map();
  2. export default function staticRegister(namespace = 'common') {
  3. if (!collection.has(namespace)) {
  4. collection.set(namespace, new Map());
  5. }
  6. const subCollection = collection.get(namespace);
  7. /**
  8. * Register an item to the collection. If the item under the same was exist earlier then this item will be replaced with new one.
  9. *
  10. * @param {String} name Identification of the item.
  11. * @param {*} item Item to save in the collection.
  12. */
  13. function register(name, item) {
  14. subCollection.set(name, item);
  15. }
  16. /**
  17. * Retrieve the item from the collection.
  18. *
  19. * @param {String} name Identification of the item.
  20. * @returns {*} Returns item which was saved in the collection.
  21. */
  22. function getItem(name) {
  23. return subCollection.get(name);
  24. }
  25. /**
  26. * Check if item under specyfied name is exists.
  27. *
  28. * @param {String} name Identification of the item.
  29. * @returns {Boolean} Returns `true` or `false` depends on if element exists in the collection.
  30. */
  31. function hasItem(name) {
  32. return subCollection.has(name);
  33. }
  34. /**
  35. * Retrieve list of names registered from the collection.
  36. *
  37. * @returns {Array} Returns an array of strings with all names under which objects are stored.
  38. */
  39. function getNames() {
  40. return [...subCollection.keys()];
  41. }
  42. /**
  43. * Retrieve all registered values from the collection.
  44. *
  45. * @returns {Array} Returns an array with all values stored in the collection.
  46. */
  47. function getValues() {
  48. return [...subCollection.values()];
  49. }
  50. return {
  51. register,
  52. getItem,
  53. hasItem,
  54. getNames,
  55. getValues,
  56. };
  57. }