DependencyTemplates.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /*
  2. MIT License http://www.opensource.org/licenses/mit-license.php
  3. Author Tobias Koppers @sokra
  4. */
  5. "use strict";
  6. const { DEFAULTS } = require("./config/defaults");
  7. const createHash = require("./util/createHash");
  8. /** @typedef {import("./Dependency")} Dependency */
  9. /** @typedef {import("./DependencyTemplate")} DependencyTemplate */
  10. /** @typedef {typeof import("./util/Hash")} Hash */
  11. /** @typedef {new (...args: EXPECTED_ANY[]) => Dependency} DependencyConstructor */
  12. class DependencyTemplates {
  13. /**
  14. * @param {string | Hash} hashFunction the hash function to use
  15. */
  16. constructor(hashFunction = DEFAULTS.HASH_FUNCTION) {
  17. /** @type {Map<DependencyConstructor, DependencyTemplate>} */
  18. this._map = new Map();
  19. /** @type {string} */
  20. this._hash = "31d6cfe0d16ae931b73c59d7e0c089c0";
  21. this._hashFunction = hashFunction;
  22. }
  23. /**
  24. * @param {DependencyConstructor} dependency Constructor of Dependency
  25. * @returns {DependencyTemplate | undefined} template for this dependency
  26. */
  27. get(dependency) {
  28. return this._map.get(dependency);
  29. }
  30. /**
  31. * @param {DependencyConstructor} dependency Constructor of Dependency
  32. * @param {DependencyTemplate} dependencyTemplate template for this dependency
  33. * @returns {void}
  34. */
  35. set(dependency, dependencyTemplate) {
  36. this._map.set(dependency, dependencyTemplate);
  37. }
  38. /**
  39. * @param {string} part additional hash contributor
  40. * @returns {void}
  41. */
  42. updateHash(part) {
  43. const hash = createHash(this._hashFunction);
  44. hash.update(`${this._hash}${part}`);
  45. this._hash = /** @type {string} */ (hash.digest("hex"));
  46. }
  47. getHash() {
  48. return this._hash;
  49. }
  50. clone() {
  51. const newInstance = new DependencyTemplates(this._hashFunction);
  52. newInstance._map = new Map(this._map);
  53. newInstance._hash = this._hash;
  54. return newInstance;
  55. }
  56. }
  57. module.exports = DependencyTemplates;