UNPKG

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