HEX
Server: LiteSpeed
System: Linux server.zepintelhosting.com 4.18.0 #1 SMP Mon Sep 30 15:36:27 MSK 2024 x86_64
User: enamadmin (1026)
PHP: 8.2.30
Disabled: exec,system,passthru,shell_exec,proc_open,popen,apache_child_terminate
Upload Files
File: /home/enamadmin/moodledata/filedir/4f/ee/4fee495c9cb8fb3dd0e9e71dbd18a7e9a5b2b88f
/*
	MIT License http://www.opensource.org/licenses/mit-license.php
*/

"use strict";

/** @template T @typedef {function(): T} FunctionReturning */

/**
 * @template T
 * @param {FunctionReturning<T>} fn memorized function
 * @returns {FunctionReturning<T>} new function
 */
const memoize = fn => {
	let cache = false;
	/** @type {T | undefined} */
	let result = undefined;
	return () => {
		if (cache) {
			return /** @type {T} */ (result);
		} else {
			result = fn();
			cache = true;
			// Allow to clean up memory for fn
			// and all dependent resources
			// eslint-disable-next-line no-warning-comments
			// @ts-ignore
			fn = undefined;
			return /** @type {T} */ (result);
		}
	};
};

module.exports = memoize;