Skip to content

Commit 23e2c5a

Browse files
committed
cache proxy state
1 parent 563720a commit 23e2c5a

File tree

3 files changed

+23
-3
lines changed

3 files changed

+23
-3
lines changed

CHANGELOG.md

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Change Log
22

33
## [Unreleased]
4+
### Changed
5+
- Cache proxy state for more performance
46

57
## [1.1.0] - 2019-02-17
68
### Changed

dist/index.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,21 @@ var useReduxState = function useReduxState() {
6060
lastState.current = state;
6161
}); // trapped
6262

63+
var proxyMap = (0, _react.useRef)(new WeakMap());
64+
var trappedMap = (0, _react.useRef)(new WeakMap());
6365
var lastTrapped = (0, _react.useRef)(null);
64-
var trapped = (0, _proxyequal.proxyState)(state);
66+
var trapped;
6567
(0, _react.useEffect)(function () {
6668
lastTrapped.current = trapped;
67-
}); // subscription
69+
});
70+
71+
if (trappedMap.current.has(state)) {
72+
trapped = trappedMap.current.get(state);
73+
} else {
74+
trapped = (0, _proxyequal.proxyState)(state, null, proxyMap.current);
75+
trappedMap.current.set(state, trapped);
76+
} // subscription
77+
6878

6979
(0, _react.useEffect)(function () {
7080
var callback = function callback() {

src/index.js

+9-1
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,19 @@ export const useReduxState = () => {
5757
lastState.current = state;
5858
});
5959
// trapped
60+
const proxyMap = useRef(new WeakMap());
61+
const trappedMap = useRef(new WeakMap());
6062
const lastTrapped = useRef(null);
61-
const trapped = proxyState(state);
63+
let trapped;
6264
useEffect(() => {
6365
lastTrapped.current = trapped;
6466
});
67+
if (trappedMap.current.has(state)) {
68+
trapped = trappedMap.current.get(state);
69+
} else {
70+
trapped = proxyState(state, null, proxyMap.current);
71+
trappedMap.current.set(state, trapped);
72+
}
6573
// subscription
6674
useEffect(() => {
6775
const callback = () => {

0 commit comments

Comments
 (0)