1 | 'use strict'
|
2 |
|
3 | module.exports = function (func, resolver) {
|
4 | var memoized = function () {
|
5 | const args = arguments
|
6 | const key = resolver.apply(this, args)
|
7 | const cache = memoized.cache
|
8 |
|
9 | if (cache.has(key)) {
|
10 | return cache.get(key)
|
11 | }
|
12 |
|
13 | const result = func.apply(this, args)
|
14 |
|
15 | result.then(function () {
|
16 | memoized.cache = cache.set(key, result) || cache
|
17 | return arguments
|
18 | })
|
19 |
|
20 | return result
|
21 | }
|
22 |
|
23 | memoized.clear = function () {
|
24 | memoized.cache = new Map()
|
25 | }
|
26 |
|
27 | memoized.clear()
|
28 |
|
29 | return memoized
|
30 | }
|