Skip to content

Commit fb5c61a

Browse files
committed
add salvoravida fork of react-redux and add dummy count
1 parent 644f4b4 commit fb5c61a

File tree

7 files changed

+87
-8
lines changed

7 files changed

+87
-8
lines changed

__tests__/all_spec.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,13 @@ const names = [
1515
'use-context-selector',
1616
'mobx-react-lite',
1717
'use-subscription',
18+
'salvoravida-react-redux',
1819
];
1920

2021
const sleep = ms => new Promise(r => setTimeout(r, ms));
2122
jest.setTimeout(15 * 1000);
22-
const REPEAT = 10;
23+
const REPEAT = 5;
24+
const DOUBLE = 2; // two clicks to increment one
2325
const NUM_COMPONENTS = 50; // defined in src/common.js
2426

2527
names.forEach((name) => {
@@ -41,7 +43,7 @@ names.forEach((name) => {
4143

4244
it('check1: updated properly', async () => {
4345
delays = [];
44-
for (let loop = 0; loop < REPEAT; ++loop) {
46+
for (let loop = 0; loop < REPEAT * DOUBLE; ++loop) {
4547
const start = Date.now();
4648
// click buttons three times
4749
await Promise.all([
@@ -52,7 +54,7 @@ names.forEach((name) => {
5254
delays.push(Date.now() - start);
5355
}
5456
console.log(name, delays);
55-
// check if all counts become 15 = REPEAT * 3
57+
// check if all counts become REPEAT * 3
5658
await Promise.all([...Array(NUM_COMPONENTS).keys()].map(async (i) => {
5759
await expect(page).toMatchElement(`.count:nth-of-type(${i + 1})`, {
5860
text: `${REPEAT * 3}`,
@@ -81,8 +83,11 @@ names.forEach((name) => {
8183
page.click('#remoteIncrement'),
8284
page.click('#remoteIncrement'),
8385
page.click('#localIncrement'),
86+
page.click('#remoteIncrement'),
87+
page.click('#remoteIncrement'),
88+
page.click('#localIncrement'),
8489
]);
85-
// check if all counts become 18 = REPEAT * 3 + 3
90+
// check if all counts become REPEAT * 3 + 2
8691
await Promise.all([...Array(NUM_COMPONENTS).keys()].map(async (i) => {
8792
await expect(page).toMatchElement(`.count:nth-of-type(${i + 1})`, {
8893
text: `${REPEAT * 3 + 2}`,

package-lock.json

Lines changed: 13 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
"build:use-context-selector": "NAME=use-context-selector webpack",
2727
"build:mobx-react-lite": "NAME=mobx-react-lite webpack",
2828
"build:use-subscription": "NAME=use-subscription webpack",
29+
"build:salvoravida-react-redux": "NAME=salvoravida-react-redux webpack",
2930
"build-all": "run-s build:*"
3031
},
3132
"keywords": [
@@ -35,6 +36,7 @@
3536
],
3637
"license": "MIT",
3738
"dependencies": {
39+
"@salvoravida/react-redux": "^7.1.6",
3840
"constate": "^1.3.2",
3941
"mobx": "^5.15.0",
4042
"mobx-react-lite": "^2.0.0-alpha.2",

public/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
</head>
88
<body>
99
<p>This is a button to trigger action from outside of React</p>
10-
<button id="remoteIncrement">Increment remote count</button>
10+
<button id="remoteIncrement">Increment remote count (two clicks to increment one)</button>
1111
<div id="app"></div>
1212
</body>
1313
</html>

src/common.js

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,20 @@ export const useRegisterIncrementDispatcher = (listener) => {
1818
}, [listener]);
1919
};
2020

21-
export const initialState = { count: 0 };
21+
export const initialState = {
22+
count: 0,
23+
dummy: 0,
24+
};
2225

2326
export const reducer = (state = initialState, action) => {
2427
switch (action.type) {
2528
case 'increment':
26-
return { ...state, count: state.count + 1 };
29+
return {
30+
...state,
31+
dummy: state.dummy + 1,
32+
// only update once in two
33+
count: state.dummy % 2 === 1 ? state.count + 1 : state.count,
34+
};
2735
default:
2836
return state;
2937
}

src/mobx-react-lite/index.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,10 @@ const Main = () => {
2525
const store = useContext(Ctx);
2626
useCheckTearing();
2727
useRegisterIncrementDispatcher(React.useCallback(() => {
28-
store.count += 1;
28+
store.dummy += 1;
29+
if (store.dummy % 2 === 1) {
30+
store.count += 1;
31+
}
2932
}, [store]));
3033
const [localCount, localIncrement] = React.useReducer(c => c + 1, 0);
3134
return useObserver(() => {

src/salvoravida-react-redux/index.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import React from 'react';
2+
import { createStore } from 'redux';
3+
import { Provider, useSelector, useDispatch } from '@salvoravida/react-redux';
4+
5+
import {
6+
syncBlock,
7+
useRegisterIncrementDispatcher,
8+
reducer,
9+
ids,
10+
useCheckTearing,
11+
shallowEqual,
12+
} from '../common';
13+
14+
const store = createStore(reducer);
15+
16+
const Counter = React.memo(() => {
17+
const count = useSelector(state => state.count);
18+
syncBlock();
19+
return <div className="count">{count}</div>;
20+
}, shallowEqual);
21+
22+
const Main = () => {
23+
const dispatch = useDispatch();
24+
const count = useSelector(state => state.count);
25+
useCheckTearing();
26+
useRegisterIncrementDispatcher(React.useCallback(() => {
27+
dispatch({ type: 'increment' });
28+
}, [dispatch]));
29+
const [localCount, localIncrement] = React.useReducer(c => c + 1, 0);
30+
return (
31+
<div>
32+
<h1>Remote Count</h1>
33+
{ids.map(id => <Counter key={id} />)}
34+
<div className="count">{count}</div>
35+
<h1>Local Count</h1>
36+
{localCount}
37+
<button type="button" id="localIncrement" onClick={localIncrement}>Increment local count</button>
38+
</div>
39+
);
40+
};
41+
42+
const App = () => (
43+
<Provider store={store}>
44+
<Main />
45+
</Provider>
46+
);
47+
48+
export default App;

0 commit comments

Comments
 (0)