Source: provider/abstractProviderByFile.js

  1. /**
  2. * @author Sloan Seaman
  3. * @copyright 2016 and on
  4. * @version .1
  5. * @license https://www.apache.org/licenses/LICENSE-2.0 Apache License 2.0
  6. */
  7. /** @private */
  8. /**
  9. * Provides items by single file with multiple items defined in the file
  10. *
  11. * items are loaded asynchronously but if a item is requested before being loaded
  12. * it will be immediately loaded and then skipped by the asychronous processing.
  13. *
  14. * @abstract
  15. * @constructor
  16. * @implements {Provider}
  17. * @implements {ItemProcessor}
  18. * @param {String} file The file to read all items from
  19. * @param {Object.<String, Object>} options Options for configuration. This can also be used as a map to pass to the itemProcessor
  20. * if the implementing class wants to pass information into the itemProcessor method
  21. * @param {Boolean} [options.preload=false] Should the file be preloaded or only loaded when a item is requested
  22. * @param {String} [options.fileEncoding=utf8] The file encoding to use when reading files and directories
  23. * @param {Object.<String, Object>} [options.itemMap] A map that may be passed in to prime the internal map with.
  24. */
  25. function AbstractProviderByFile(file, options) {
  26. if (!file) throw Error('file required');
  27. this._file = file;
  28. this._options = options;
  29. this._fileEncoding = (options && options.fileEncoding)
  30. ? options.fileEncoding
  31. : 'utf8';
  32. this._preload = (options && options.preload)
  33. ? options.preload
  34. : false;
  35. this._items = (options && options.itemMap)
  36. ? options.itemMap
  37. : {};
  38. this._fileLoaded = false;
  39. if (this._preload) { // this is a blocking call as it is sync
  40. var processed = this.processItems(this._items, file, this._options);
  41. for (var i=0;i<processed.length;i++) {
  42. this._items[processed[i].itemId] = processed[i].item;
  43. }
  44. this._fileLoaded = true;
  45. }
  46. }
  47. /**
  48. * Returns the item based on the itemId
  49. *
  50. * @function
  51. * @param {String} itemId The id of the item to retrieve. If the file is not already loaded, it will load it to look for the item
  52. * @return {item} The item. Null if no item is found
  53. */
  54. AbstractProviderByFile.prototype.getItem = function(itemId) {
  55. var item = this._items[itemId];
  56. if (!item && !this._fileLoaded) {
  57. this.processItem(this._items, itemId, this._file, this._options); // didn't find it and haven't looked for it
  58. this._fileLoaded = true;
  59. item = this._items[itemId];
  60. }
  61. return item;
  62. };
  63. /**
  64. * Returns all items that were loaded
  65. *
  66. * @function
  67. * @return {Map} Map of the items where the Key is the itemId and the Value is the item itself
  68. */
  69. AbstractProviderByFile.prototype.getItems = function() {
  70. return this._items;
  71. };
  72. /**
  73. * Processes the specific item and returns the result
  74. *
  75. * @function
  76. * @abstract
  77. * @param {Map} items Map of the items being processed
  78. * @param {String} itemId The Id of the item to process
  79. * @param {String} fileName The name of the file being processed
  80. * @param {Object} options Any options that are being passed to the ItemProcessor (can be null)
  81. */
  82. AbstractProviderByFile.prototype.processItem = function(items, itemId, fileName, options) {
  83. /*eslint no-unused-vars: ["error", { "args": "none" }]*/
  84. throw new Error('Must be implemented by subclass');
  85. };
  86. /**
  87. * Processes multiple items at once
  88. *
  89. * @function
  90. * @abstract
  91. * @param {Map} items Map of the items being processeds
  92. * @param {String} fileName The name of the file being processed
  93. * @param {Object} options Any options that are being passed to the ItemProcessor (can be null)
  94. */
  95. AbstractProviderByFile.prototype.processItems = function(items, fileName, options) {
  96. /*eslint no-unused-vars: ["error", { "args": "none" }]*/
  97. throw new Error('Must be implemented by subclass');
  98. };
  99. module.exports = AbstractProviderByFile;