DependencyTemplates.js 1.7 KB

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