| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | /* | 
					
						
							|  |  |  | 	MIT License http://www.opensource.org/licenses/mit-license.php
 | 
					
						
							|  |  |  | */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | "use strict"; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-10-02 14:54:21 +08:00
										 |  |  | /** @template T @typedef {function(): T} FunctionReturning */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /** | 
					
						
							|  |  |  |  * @template T | 
					
						
							|  |  |  |  * @param {FunctionReturning<T>} fn memorized function | 
					
						
							|  |  |  |  * @returns {FunctionReturning<T>} new function | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | const memoize = fn => { | 
					
						
							|  |  |  | 	let cache = false; | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +08:00
										 |  |  | 	/** @type {T | undefined} */ | 
					
						
							| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | 	let result = undefined; | 
					
						
							|  |  |  | 	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
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			result = fn(); | 
					
						
							| 
									
										
										
										
											2020-12-27 05:32:57 +08:00
										 |  |  | 			cache = true; | 
					
						
							| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | 			// Allow to clean up memory for fn
 | 
					
						
							|  |  |  | 			// and all dependent resources
 | 
					
						
							| 
									
										
										
										
											2024-02-17 01:39:12 +08:00
										 |  |  | 			// eslint-disable-next-line no-warning-comments
 | 
					
						
							|  |  |  | 			// @ts-ignore
 | 
					
						
							| 
									
										
										
										
											2018-12-21 19:02:37 +08:00
										 |  |  | 			fn = undefined; | 
					
						
							| 
									
										
										
										
											2023-06-12 22:21:21 +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; |