| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							| 
									
										
										
										
											2018-07-30 23:08:51 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const createHash = require("./util/createHash"); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-12 23:12:00 +08:00
										 |  |  | /** @typedef {import("./Dependency")} Dependency */ | 
					
						
							| 
									
										
										
										
											2018-07-23 23:33:29 +08:00
										 |  |  | /** @typedef {import("./DependencyTemplate")} DependencyTemplate */ | 
					
						
							| 
									
										
										
										
											2021-09-22 18:12:46 +08:00
										 |  |  | /** @typedef {typeof import("./util/Hash")} Hash */ | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-07-12 23:12:00 +08:00
										 |  |  | /** @typedef {new (...args: any[]) => Dependency} DependencyConstructor */ | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | class DependencyTemplates { | 
					
						
							| 
									
										
										
										
											2021-09-22 18:12:46 +08:00
										 |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string | Hash} hashFunction the hash function to use | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	constructor(hashFunction = "md4") { | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | 		/** @type {Map<Function, DependencyTemplate>} */ | 
					
						
							|  |  |  | 		this._map = new Map(); | 
					
						
							|  |  |  | 		/** @type {string} */ | 
					
						
							|  |  |  | 		this._hash = "31d6cfe0d16ae931b73c59d7e0c089c0"; | 
					
						
							| 
									
										
										
										
											2021-09-22 18:12:46 +08:00
										 |  |  | 		this._hashFunction = hashFunction; | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-07-12 23:12:00 +08:00
										 |  |  | 	 * @param {DependencyConstructor} dependency Constructor of Dependency | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | 	 * @returns {DependencyTemplate} template for this dependency | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	get(dependency) { | 
					
						
							|  |  |  | 		return this._map.get(dependency); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							| 
									
										
										
										
											2018-07-12 23:12:00 +08:00
										 |  |  | 	 * @param {DependencyConstructor} dependency Constructor of Dependency | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | 	 * @param {DependencyTemplate} dependencyTemplate template for this dependency | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	set(dependency, dependencyTemplate) { | 
					
						
							|  |  |  | 		this._map.set(dependency, dependencyTemplate); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/** | 
					
						
							|  |  |  | 	 * @param {string} part additional hash contributor | 
					
						
							|  |  |  | 	 * @returns {void} | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	updateHash(part) { | 
					
						
							| 
									
										
										
										
											2021-09-22 18:12:46 +08:00
										 |  |  | 		const hash = createHash(this._hashFunction); | 
					
						
							| 
									
										
										
										
											2021-09-26 08:15:41 +08:00
										 |  |  | 		hash.update(`${this._hash}${part}`); | 
					
						
							| 
									
										
										
										
											2019-07-17 22:02:33 +08:00
										 |  |  | 		this._hash = /** @type {string} */ (hash.digest("hex")); | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	getHash() { | 
					
						
							|  |  |  | 		return this._hash; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	clone() { | 
					
						
							| 
									
										
										
										
											2022-01-18 18:42:13 +08:00
										 |  |  | 		const newInstance = new DependencyTemplates(this._hashFunction); | 
					
						
							| 
									
										
										
										
											2018-07-11 19:05:13 +08:00
										 |  |  | 		newInstance._map = new Map(this._map); | 
					
						
							|  |  |  | 		newInstance._hash = this._hash; | 
					
						
							|  |  |  | 		return newInstance; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = DependencyTemplates; |