main
Last change
on this file since 79a0317 was 79a0317, checked in by stefan toskovski <stefantoska84@…>, 3 days ago |
F4 Finalna Verzija
|
-
Property mode
set to
100644
|
File size:
678 bytes
|
Line | |
---|
1 | /*
|
---|
2 | MIT License http://www.opensource.org/licenses/mit-license.php
|
---|
3 | */
|
---|
4 |
|
---|
5 | "use strict";
|
---|
6 |
|
---|
7 | /** @template T @typedef {function(): T} FunctionReturning */
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * @template T
|
---|
11 | * @param {FunctionReturning<T>} fn memorized function
|
---|
12 | * @returns {FunctionReturning<T>} new function
|
---|
13 | */
|
---|
14 | const memoize = fn => {
|
---|
15 | let cache = false;
|
---|
16 | /** @type {T | undefined} */
|
---|
17 | let result;
|
---|
18 | return () => {
|
---|
19 | if (cache) {
|
---|
20 | return /** @type {T} */ (result);
|
---|
21 | }
|
---|
22 |
|
---|
23 | result = fn();
|
---|
24 | cache = true;
|
---|
25 | // Allow to clean up memory for fn
|
---|
26 | // and all dependent resources
|
---|
27 | /** @type {FunctionReturning<T> | undefined} */
|
---|
28 | (fn) = undefined;
|
---|
29 | return /** @type {T} */ (result);
|
---|
30 | };
|
---|
31 | };
|
---|
32 |
|
---|
33 | module.exports = memoize;
|
---|
Note:
See
TracBrowser
for help on using the repository browser.