| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2025-03-11 08:28:01 +08:00
										 |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							| 
									
										
										
										
											2025-03-12 09:56:14 +08:00
										 |  |  |  * @typedef {() => T} FunctionReturning | 
					
						
							| 
									
										
										
										
											2025-03-11 08:28:01 +08:00
										 |  |  |  */ | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {FunctionReturning<T>} fn memorized function | 
					
						
							|  |  |  |  * @returns {FunctionReturning<T>} new function | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2025-07-17 00:13:14 +08:00
										 |  |  | const memoize = (fn) => { | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | 	let cache = false; | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 	/** @type {T | undefined} */ | 
					
						
							| 
									
										
										
										
											2024-07-31 06:15:03 +08:00
										 |  |  | 	let result; | 
					
						
							| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | 	return () => { | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | 		if (cache) { | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 			return /** @type {T} */ (result); | 
					
						
							| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2024-07-31 04:21:27 +08:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		result = fn(); | 
					
						
							|  |  |  | 		cache = true; | 
					
						
							|  |  |  | 		// Allow to clean up memory for fn
 | 
					
						
							|  |  |  | 		// and all dependent resources
 | 
					
						
							| 
									
										
										
										
											2024-10-01 03:05:27 +08:00
										 |  |  | 		/** @type {FunctionReturning<T> | undefined} */ | 
					
						
							|  |  |  | 		(fn) = undefined; | 
					
						
							| 
									
										
										
										
											2024-07-31 04:21:27 +08:00
										 |  |  | 		return /** @type {T} */ (result); | 
					
						
							| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | 	}; | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | module.exports = memoize; |