-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmemoize.js.html
126 lines (98 loc) · 3.56 KB
/
memoize.js.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: memoize.js</title>
<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>
<body>
<div id="main">
<h1 class="page-title">Source: memoize.js</h1>
<section>
<article>
<pre class="prettyprint source linenums"><code>'use strict';
const { promisifyClosure } = require('./utils/promisify');
const crypto = require('crypto');
const argKey = x => x.toString() + ':' + typeof x;
const generateKey = args => {
const key = args.map(argKey).join('|');
return crypto.createHash('sha256').update(key).digest('hex');
};
/**
* Function to memoize the function
* @param {Function} fn - function to memoize
* @param {Object} config - object with config
* @param {Boolean} config.isCb - function accepts callback
* @param {Number} config.cacheSize - max size of cache
* @example
* const sum = async (a, b) => {
await sleep(100);
return a + b;
};
const memoizedSum = memoize(sum);
const result = [];
const expectedResult = [4, 6, 4];
memoizedSum(1, 3)
.then(res => result.push(res))
.then(() => memoizedSum(1, 5))
.then(res => result.push(res))
.then(() => memoizedSum(1, 3))
.then(res => result.push(res))
.them(() => console.log(result));
@returns {any} the result of function from cache or calculated result
*/
const asyncMemoize = (fn, config = {}) => {
if (typeof fn !== 'function') {
throw new Error('fn is not a function');
}
const { isCb, cacheSize } = config;
const cache = new Map();
const memoized = (...args) => {
let wrappedFn = fn;
const key = generateKey(args);
if (isCb) {
const lastArg = args[args.length - 1];
const cb = typeof lastArg === 'function' ?
args.pop() : (err, data) => data;
wrappedFn = promisifyClosure(fn, cb);
}
return Promise.resolve(key)
.then(key => {
const value = cache.get(key);
if (value) return Promise.resolve(value);
return Promise.resolve(wrappedFn(...args))
.then(value => {
if (cache.size >= cacheSize) {
const firstKey = cache.keys().next().value;
cache.delete(firstKey);
}
cache.set(key, value);
return value;
});
});
};
memoized.clearCache = () => cache.clear();
return memoized;
};
module.exports = asyncMemoize;
</code></pre>
</article>
</section>
</div>
<nav>
<h2><a href="index.html">Home</a></h2><h3>Classes</h3><ul><li><a href="AsyncEmitter.html">AsyncEmitter</a></li></ul><h3>Global</h3><ul><li><a href="index.html#asyncMemoize">asyncMemoize</a></li><li><a href="index.html#map">map</a></li><li><a href="index.html#Queue">Queue</a></li><li><a href="index.html#queue">queue</a></li><li><a href="index.html#reduce">reduce</a></li><li><a href="index.html#retry">retry</a></li><li><a href="index.html#series">series</a></li><li><a href="index.html#some">some</a></li></ul>
</nav>
<br class="clear">
<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Mon Jan 04 2021 15:21:06 GMT+0200 (GMT+02:00)
</footer>
<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>