176dd3d66e3ccf44cae55754837ffcc62669e6aad839f6009ad40c94f41b11e4b9edf45608abdde047c5df878a20a813927c2a2bca150b8919b3ce939b528e 2.0 KB

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