Skip to content

Commit 3afd06a

Browse files
committed
Port deeptree scenario
1 parent 3b4c1e0 commit 3afd06a

File tree

5 files changed

+179
-0
lines changed

5 files changed

+179
-0
lines changed

src/scenarios/deeptree/App.jsx

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import React from "react";
2+
import { connect } from "react-redux";
3+
4+
import Slice from "./Slice";
5+
import * as c from "./constants";
6+
import { incrementRandomCounter } from "./counters";
7+
8+
let slices;
9+
10+
const mapState = state => {
11+
if (!slices) {
12+
slices = Object.keys(state).map(key => Number(key));
13+
slices.sort();
14+
}
15+
16+
return { slices };
17+
};
18+
19+
const mapDispatch = { incrementRandomCounter };
20+
21+
class App extends React.Component {
22+
render() {
23+
return (
24+
<div>
25+
<button onClick={this.props.incrementRandomCounter}>
26+
Update Random Counter
27+
</button>
28+
<div className="row">
29+
{this.props.slices.map((slice, idx) => {
30+
return (
31+
<div style={{ display: "inline-block", minWidth: 70 }} key={idx}>
32+
<Slice idx={slice} remainingDepth={c.TREE_DEPTH} />
33+
</div>
34+
);
35+
})}
36+
</div>
37+
</div>
38+
);
39+
}
40+
}
41+
App.displayName = "App";
42+
43+
export default connect(
44+
mapState,
45+
mapDispatch
46+
)(App);

src/scenarios/deeptree/Slice.jsx

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import React, { Component } from "react";
2+
import { connect } from "react-redux";
3+
4+
const mapStateToProps = (state, props) => {
5+
return {
6+
value: state[props.idx]
7+
};
8+
};
9+
10+
const Counter = ({ value }) => {
11+
return <div>Value: {value}</div>;
12+
};
13+
14+
const ConnectedCounter = connect(mapStateToProps)(Counter);
15+
16+
class Slice extends Component {
17+
state = {};
18+
19+
componentDidMount = () => {
20+
//this.props.fillPairs(this.props.idx);
21+
};
22+
23+
render() {
24+
const { remainingDepth, idx } = this.props;
25+
26+
if (remainingDepth > 0) {
27+
return (
28+
<div>
29+
{idx}.{remainingDepth}
30+
<div>
31+
<Slice idx={idx} remainingDepth={remainingDepth - 1} />
32+
</div>
33+
</div>
34+
);
35+
}
36+
37+
return <ConnectedCounter idx={idx} />;
38+
}
39+
}
40+
Slice.displayName = "Slice";
41+
42+
export default Slice;
43+
//export default connect(mapStateToProps, actions)(Slice);

src/scenarios/deeptree/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export const NUMBER_OF_SLICES = 250;
2+
3+
export const TREE_DEPTH = 15;

src/scenarios/deeptree/counters.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { createSlice } from '@reduxjs/toolkit'
2+
import * as c from './constants'
3+
4+
const { reducer, actions } = createSlice({
5+
name: 'counters',
6+
initialState: {},
7+
reducers: {
8+
initialize(state, action) {
9+
const { numberOfCounters } = action.payload
10+
for (let i = 0; i < numberOfCounters; i++) {
11+
state[i] = 0
12+
}
13+
},
14+
increment(state, action) {
15+
const { counterId } = action.payload
16+
const value = state[counterId] || 0
17+
state[counterId] = value + 1
18+
},
19+
incrementMany(state, action) {
20+
const { mod } = action.payload
21+
for (let counterId = 0; counterId < c.NUMBER_OF_SLICES; counterId++) {
22+
if (counterId % mod === 0) {
23+
const value = state[counterId] || 0
24+
state[counterId] = value + 1
25+
}
26+
}
27+
},
28+
},
29+
})
30+
31+
export const { initialize, increment, incrementMany } = actions
32+
33+
export function incrementRandomCounter() {
34+
const counterId = Math.floor(Math.random() * c.NUMBER_OF_SLICES)
35+
return increment({ counterId })
36+
}
37+
38+
export default reducer

src/scenarios/deeptree/index.tsx

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import React, { useLayoutEffect } from 'react'
2+
import ReactDOM from 'react-dom'
3+
import { configureStore, AnyAction } from '@reduxjs/toolkit'
4+
5+
import { renderApp } from '../../common'
6+
7+
import App from './App'
8+
import * as c from './constants'
9+
import rootReducer, {
10+
initialize,
11+
incrementRandomCounter,
12+
incrementMany,
13+
} from './counters'
14+
15+
const store = configureStore({
16+
reducer: rootReducer,
17+
middleware: (gdm) =>
18+
gdm({
19+
immutabilityCheck: false,
20+
serializableCheck: false,
21+
}),
22+
})
23+
24+
store.dispatch(initialize({ numberOfCounters: c.NUMBER_OF_SLICES }))
25+
26+
function doRandomUpdate() {
27+
store.dispatch(incrementRandomCounter())
28+
}
29+
30+
function doUpdateMany(mod: number) {
31+
store.dispatch(incrementMany({ mod }))
32+
}
33+
34+
const DeepTreeApp = () => {
35+
useLayoutEffect(() => {
36+
setInterval(doRandomUpdate, 13)
37+
38+
setInterval(() => doUpdateMany(5), 21)
39+
40+
setInterval(doRandomUpdate, 34)
41+
42+
setInterval(() => doUpdateMany(3), 55)
43+
}, [])
44+
45+
return <App />
46+
}
47+
48+
// @ts-ignore
49+
renderApp(DeepTreeApp, store)

0 commit comments

Comments
 (0)