Skip to content

Commit 176d4f9

Browse files
committed
update
1 parent 02e5f00 commit 176d4f9

File tree

7 files changed

+561
-486
lines changed

7 files changed

+561
-486
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@ It will know, that it should react only state.value. _Perfect_.
3333

3434
[![NPM](https://nodei.co/npm/memoize-state.png?downloads=true&stars=true)](https://nodei.co/npm/memoize-state/)
3535

36+
## Implementations
37+
- [React-memoize](https://github.com/theKashey/react-memoize) - magic memoization for React, componentWillReceiveProps optization, and selection from context.
38+
- [beautiful-react-redux](https://github.com/theKashey/beautiful-react-redux) - instant memoization for React-Redux
39+
- your project!
40+
3641
# API
3742
* `memoizeState(function, options)` - creates memoized variant of a function.
3843
- Name, length (argument count), and any other own key will be transferred to memoized result

lib/cache.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8+
9+
exports.updateCacheLine = updateCacheLine;
10+
exports.shallowEqualHit = shallowEqualHit;
11+
12+
var _proxyequal = require('proxyequal');
13+
14+
function updateCacheLine(cache, lineId, value) {
15+
for (var i = lineId; i < cache.length - 1; i++) {
16+
cache[i] = cache[i + 1];
17+
}
18+
cache[cache.length - 1] = value;
19+
}
20+
21+
function shallowEqualHit(cache, args) {
22+
for (var i = 0; i < cache.length; ++i) {
23+
var found = cache[i];
24+
var _found = found,
25+
lineArgs = _found.args,
26+
lineAffected = _found.affected,
27+
lineValue = _found.result;
28+
29+
30+
if (args.length !== lineArgs.length) {
31+
continue;
32+
}
33+
34+
for (var j = 0; j < args.length; ++j) {
35+
var a = args[j];
36+
var b = lineArgs[j];
37+
var useAffected = lineAffected[j] ? lineAffected[j].useAffected : [];
38+
var resultAffected = lineAffected[j] ? lineAffected[j].resultAffected : [];
39+
if (a === b || (typeof a === 'undefined' ? 'undefined' : _typeof(a)) === 'object' && (useAffected.length === 0 || (0, _proxyequal.proxyShallowEqual)(a, b, useAffected)) && (resultAffected.length === 0 || (0, _proxyequal.proxyCompare)(a, b, resultAffected))) {
40+
//pass
41+
} else {
42+
found = null;
43+
break;
44+
}
45+
}
46+
if (found) {
47+
updateCacheLine(cache, i, found);
48+
return lineValue;
49+
}
50+
}
51+
return undefined;
52+
}

lib/call.js

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
'use strict';
2+
3+
Object.defineProperty(exports, "__esModule", {
4+
value: true
5+
});
6+
7+
var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol && obj !== Symbol.prototype ? "symbol" : typeof obj; };
8+
9+
exports.callIn = callIn;
10+
11+
var _proxyequal = require('proxyequal');
12+
13+
var _cache = require('./cache');
14+
15+
function _toConsumableArray(arr) { if (Array.isArray(arr)) { for (var i = 0, arr2 = Array(arr.length); i < arr.length; i++) { arr2[i] = arr[i]; } return arr2; } else { return Array.from(arr); } }
16+
17+
var nothing = 'PROXY_EQUAL_NOTHING';
18+
var emptyArray = [];
19+
20+
function addAffected(affected, object) {
21+
var key = (0, _proxyequal.getProxyKey)(object);
22+
affected[key.fingerPrint].resultAffected.push(key.suffix);
23+
}
24+
25+
function deproxifyResult(result, affected, returnPureValue) {
26+
27+
var isInProxy = (0, _proxyequal.isProxyfied)(result);
28+
if (isInProxy) {
29+
addAffected(affected, result);
30+
return (0, _proxyequal.deproxify)(result);
31+
}
32+
33+
if ((typeof result === 'undefined' ? 'undefined' : _typeof(result)) === 'object') {
34+
var sub = Array.isArray(result) ? [] : {};
35+
var altered = false;
36+
37+
if (result && Object.getOwnPropertyDescriptor(result, '__proxyequal_scanEnd')) {
38+
Object.defineProperty(result, '__proxyequal_scanEnd', {
39+
value: 'here was spread guard',
40+
configurable: true,
41+
enumerable: false
42+
});
43+
}
44+
45+
for (var i in result) {
46+
if (result.hasOwnProperty(i)) {
47+
var data = result[i];
48+
var newResult = deproxifyResult(data, affected, false);
49+
if (data && newResult !== nothing) {
50+
altered = true;
51+
sub[i] = newResult;
52+
} else {
53+
sub[i] = data;
54+
}
55+
}
56+
}
57+
if (altered) {
58+
return sub;
59+
}
60+
return returnPureValue ? result : nothing;
61+
}
62+
63+
return returnPureValue ? result : nothing;
64+
}
65+
66+
function callIn(that, cache, args, func, memoizationDepth) {
67+
var proxyMap = arguments.length > 5 && arguments[5] !== undefined ? arguments[5] : [];
68+
69+
var proxies = args.map(function (arg, index) {
70+
if (arg && (typeof arg === 'undefined' ? 'undefined' : _typeof(arg)) === 'object') {
71+
var map = proxyMap[index];
72+
if (!map) {
73+
return proxyMap[index] = (0, _proxyequal.proxyState)(arg, index);
74+
}
75+
map.reset();
76+
return map.replaceState(arg);
77+
}
78+
return undefined;
79+
});
80+
var callArgs = args.map(function (arg, index) {
81+
return proxies[index] ? proxies[index].state : arg;
82+
});
83+
84+
//call the real function
85+
var preResult = func.call.apply(func, [that].concat(_toConsumableArray(callArgs)));
86+
proxies.forEach(function (proxy) {
87+
return proxy && proxy.seal();
88+
});
89+
90+
var spreadDetected = [];
91+
var affected = proxies.map(function (proxy) {
92+
if (proxy) {
93+
if (proxy.spreadDetected !== false) {
94+
spreadDetected.push(proxy.spreadDetected);
95+
}
96+
var _affected = proxy.affected || emptyArray;
97+
98+
return {
99+
useAffected: [].concat(_toConsumableArray(_affected)),
100+
resultAffected: []
101+
};
102+
}
103+
return undefined;
104+
});
105+
var result = deproxifyResult(preResult, affected, true);
106+
var cacheLine = { args: args, affected: affected, result: result, callArgs: callArgs };
107+
if (cache.length < memoizationDepth) {
108+
cache.push(cacheLine);
109+
} else {
110+
(0, _cache.updateCacheLine)(cache, 0, cacheLine);
111+
}
112+
113+
if (spreadDetected.length > 0) {
114+
if (process.env.NODE_ENV !== 'production') {
115+
console.warn('memoize-state: object spread detected in ', func, '. Keys affected: ', spreadDetected.map(function (key) {
116+
return key ? key : 'root';
117+
}), '. This is no-op.');
118+
}
119+
}
120+
121+
return result;
122+
}

0 commit comments

Comments
 (0)