| 
									
										
										
										
											2017-11-17 21:26:23 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | 	Author Tobias Koppers @sokra | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const mergeCache = new WeakMap(); | 
					
						
							| 
									
										
										
										
											2018-05-20 01:42:56 +08:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2018-05-18 01:53:28 +08:00
										 |  |  | /** | 
					
						
							| 
									
										
										
										
											2018-05-20 01:42:56 +08:00
										 |  |  |  * Merges two given objects and caches the result to avoid computation if same objects passed as arguements again. | 
					
						
							|  |  |  |  * @example | 
					
						
							|  |  |  |  * // performs Object.assign(first, second), stores the result in WeakMap and returns result
 | 
					
						
							|  |  |  |  * cachedMerge({a: 1}, {a: 2}) | 
					
						
							|  |  |  |  * {a: 2} | 
					
						
							|  |  |  |  *  // when same arguments passed, gets the result from WeakMap and returns it.
 | 
					
						
							|  |  |  |  * cachedMerge({a: 1}, {a: 2}) | 
					
						
							|  |  |  |  * {a: 2} | 
					
						
							|  |  |  |  * @param {object} first first object | 
					
						
							|  |  |  |  * @param {object} second second object | 
					
						
							|  |  |  |  * @returns {object} merged object of first and second object | 
					
						
							| 
									
										
										
										
											2018-05-18 01:53:28 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2018-04-11 04:12:30 +08:00
										 |  |  | const cachedMerge = (first, second) => { | 
					
						
							| 
									
										
										
										
											2017-11-17 21:26:23 +08:00
										 |  |  | 	let innerCache = mergeCache.get(first); | 
					
						
							| 
									
										
										
										
											2018-02-25 09:00:20 +08:00
										 |  |  | 	if (innerCache === undefined) { | 
					
						
							| 
									
										
										
										
											2017-11-17 21:26:23 +08:00
										 |  |  | 		innerCache = new WeakMap(); | 
					
						
							|  |  |  | 		mergeCache.set(first, innerCache); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2018-04-11 04:12:30 +08:00
										 |  |  | 	const prevMerge = innerCache.get(second); | 
					
						
							|  |  |  | 	if (prevMerge !== undefined) return prevMerge; | 
					
						
							| 
									
										
										
										
											2017-11-17 21:26:23 +08:00
										 |  |  | 	const newMerge = Object.assign({}, first, second); | 
					
						
							|  |  |  | 	innerCache.set(second, newMerge); | 
					
						
							|  |  |  | 	return newMerge; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | module.exports = cachedMerge; |