|
Last change
on this file was 9af201e, checked in by MBK <marija.karapandzova@…>, 12 days ago |
|
Fix frontend appearance
|
-
Property mode
set to
100644
|
|
File size:
766 bytes
|
| Rev | Line | |
|---|
| [9af201e] | 1 | /*
|
|---|
| 2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
|---|
| 3 | */
|
|---|
| 4 |
|
|---|
| 5 | "use strict";
|
|---|
| 6 |
|
|---|
| 7 | /**
|
|---|
| 8 | * Defines the function returning type used by this module.
|
|---|
| 9 | * @template T
|
|---|
| 10 | * @typedef {() => T} FunctionReturning
|
|---|
| 11 | */
|
|---|
| 12 |
|
|---|
| 13 | /**
|
|---|
| 14 | * Returns new function.
|
|---|
| 15 | * @template T
|
|---|
| 16 | * @param {FunctionReturning<T>} fn memorized function
|
|---|
| 17 | * @returns {FunctionReturning<T>} new function
|
|---|
| 18 | */
|
|---|
| 19 | const memoize = (fn) => {
|
|---|
| 20 | let cache = false;
|
|---|
| 21 | /** @type {T | undefined} */
|
|---|
| 22 | let result;
|
|---|
| 23 | return () => {
|
|---|
| 24 | if (cache) {
|
|---|
| 25 | return /** @type {T} */ (result);
|
|---|
| 26 | }
|
|---|
| 27 |
|
|---|
| 28 | result = fn();
|
|---|
| 29 | cache = true;
|
|---|
| 30 | // Allow to clean up memory for fn
|
|---|
| 31 | // and all dependent resources
|
|---|
| 32 | /** @type {FunctionReturning<T> | undefined} */
|
|---|
| 33 | (fn) = undefined;
|
|---|
| 34 | return /** @type {T} */ (result);
|
|---|
| 35 | };
|
|---|
| 36 | };
|
|---|
| 37 |
|
|---|
| 38 | module.exports = memoize;
|
|---|
Note:
See
TracBrowser
for help on using the repository browser.