1 | 'use strict';
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = memoize;
|
7 |
|
8 | var _identity = require('lodash/identity');
|
9 |
|
10 | var _identity2 = _interopRequireDefault(_identity);
|
11 |
|
12 | var _slice = require('./internal/slice');
|
13 |
|
14 | var _slice2 = _interopRequireDefault(_slice);
|
15 |
|
16 | var _setImmediate = require('./internal/setImmediate');
|
17 |
|
18 | var _setImmediate2 = _interopRequireDefault(_setImmediate);
|
19 |
|
20 | var _initialParams = require('./internal/initialParams');
|
21 |
|
22 | var _initialParams2 = _interopRequireDefault(_initialParams);
|
23 |
|
24 | var _wrapAsync = require('./internal/wrapAsync');
|
25 |
|
26 | var _wrapAsync2 = _interopRequireDefault(_wrapAsync);
|
27 |
|
28 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
29 |
|
30 | function has(obj, key) {
|
31 | return key in obj;
|
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 | function memoize(fn, hasher) {
|
72 | var memo = Object.create(null);
|
73 | var queues = Object.create(null);
|
74 | hasher = hasher || _identity2.default;
|
75 | var _fn = (0, _wrapAsync2.default)(fn);
|
76 | var memoized = (0, _initialParams2.default)(function memoized(args, callback) {
|
77 | var key = hasher.apply(null, args);
|
78 | if (has(memo, key)) {
|
79 | (0, _setImmediate2.default)(function () {
|
80 | callback.apply(null, memo[key]);
|
81 | });
|
82 | } else if (has(queues, key)) {
|
83 | queues[key].push(callback);
|
84 | } else {
|
85 | queues[key] = [callback];
|
86 | _fn.apply(null, args.concat(function () /*args*/{
|
87 | var args = (0, _slice2.default)(arguments);
|
88 | memo[key] = args;
|
89 | var q = queues[key];
|
90 | delete queues[key];
|
91 | for (var i = 0, l = q.length; i < l; i++) {
|
92 | q[i].apply(null, args);
|
93 | }
|
94 | }));
|
95 | }
|
96 | });
|
97 | memoized.memo = memo;
|
98 | memoized.unmemoized = fn;
|
99 | return memoized;
|
100 | }
|
101 | module.exports = exports['default']; |
\ | No newline at end of file |