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