Skip to content

Commit 0834c69

Browse files
committed
best performance
1 parent 1f65ee7 commit 0834c69

File tree

5 files changed

+55
-38
lines changed

5 files changed

+55
-38
lines changed

src/App.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,24 @@ function App() {
2929
return () => subject.detach(onBoardUpdated);
3030
},[])
3131

32-
useEffect(() => {
33-
if(state === STATE.IN_GAME) {
34-
const interval = setInterval(() => {
35-
gameController.reorderMoles();
36-
}, 1000);
37-
return () => clearInterval(interval);
38-
}
39-
}, [state]);
32+
// useEffect(() => {
33+
// if(state === STATE.IN_GAME) {
34+
// const interval = setInterval(() => {
35+
// gameController.reorderMoles();
36+
// }, 1000);
37+
// return () => clearInterval(interval);
38+
// }
39+
// }, [state]);
4040

41-
useEffect(() => {
42-
if(state === STATE.IN_GAME) {
43-
const randomTimer = randomIntFromInterval(400, 1200);
44-
const interval = setInterval(() => {
45-
gameController.showRandomMole();
46-
}, randomTimer);
47-
return () => clearInterval(interval);
48-
}
49-
}, [state]);
41+
// useEffect(() => {
42+
// if(state === STATE.IN_GAME) {
43+
// const randomTimer = randomIntFromInterval(400, 1200);
44+
// const interval = setInterval(() => {
45+
// gameController.showRandomMole();
46+
// }, randomTimer);
47+
// return () => clearInterval(interval);
48+
// }
49+
// }, [state]);
5050

5151
return (
5252
<div className="App">

src/components/Mole/index.js

+16-11
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,30 @@ import { gameController } from './../../controllers/GameController';
44
import { randomIntFromInterval } from './../../utils/randomIntFromInterval';
55
import { Logo } from './../Logo'
66

7+
const useForceRerender = () => React.useReducer(x => x + 1, 0)[1]
8+
79
export const Mole = ({mole}) => {
8-
9-
useEffect(() => {
10-
if(mole.isVisible) {
11-
const randomTime = randomIntFromInterval(mole.peepOutMin, mole.peepOutMax);
12-
const interval = setInterval(() => {
13-
gameController.hideMole(mole);
14-
}, randomTime);
15-
return () => clearInterval(interval);
16-
}
17-
}, [mole])
10+
const forceRenderer = useForceRerender();
11+
12+
// useEffect(() => {
13+
// if(mole.isVisible) {
14+
// const randomTime = randomIntFromInterval(mole.peepOutMin, mole.peepOutMax);
15+
// const interval = setInterval(() => {
16+
// gameController.hideMole(mole);
17+
// }, randomTime);
18+
// return () => clearInterval(interval);
19+
// }
20+
// }, [mole])
1821

1922
return(
20-
mole.live > 0 && mole.isVisible &&
23+
mole.live > 0 &&
24+
//mole.isVisible &&
2125
<Logo
2226
onClick={() => {
2327
scoreController.updateScore(mole);
2428
gameController.kickedMole(mole);
2529
gameController.continueInGame();
30+
forceRenderer();
2631
}}
2732
mole={mole}
2833
/>

src/models/Game.js

+8-6
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,17 @@ export class Game {
3939
}
4040

4141
hideMole(moleToHide) {
42-
this.board = this.board.map(({ id, mole }) => {
43-
return { id, mole: { ...mole.id === moleToHide.id ? {...mole, isVisible: false } : mole } }
44-
})
42+
moleToHide.hide();
43+
// this.board = this.board.map(({ id, mole }) => {
44+
// return { id, mole: { ...mole.id === moleToHide.id ? {...mole, isVisible: false } : mole } }
45+
// })
4546
}
4647

4748
kickedMole(moleKicked) {
48-
this.board = this.board.map(({ id, mole }) => {
49-
return { id, mole: { ...mole.id === moleKicked.id ? {...mole, live: mole.live - 1 } : mole } }
50-
})
49+
moleKicked.decrementLive();
50+
// this.board = this.board.map(({ id, mole }) => {
51+
// return { id, mole: { ...mole.id === moleKicked.id ? {...mole, live: mole.live - 1 } : mole } }
52+
// })
5153
}
5254

5355
continueInGame(scoreController) {

src/models/Hole.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { GenerateUUID } from './GenerateUUID';
22

33

4-
export class Hole extends GenerateUUID {
4+
export class Hole {
55
constructor(GoldenMole, Mole, index, randomNumber) {
6-
super()
6+
this.id = new GenerateUUID().id
77
this.mole = index === randomNumber ? new GoldenMole() : new Mole();
88
}
99
}

src/models/Mole.js

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,26 @@
11
import { GenerateUUID } from './GenerateUUID';
22

3-
export class Mole extends GenerateUUID {
3+
export class Mole {
44

55
constructor() {
6-
super()
6+
this.id = new GenerateUUID().id;
77
this.isVisible = false;
88
this.live = LIVE.MOLE;
99
this.name =`Mole ${this.id}`;
1010
this.peepOutMax = 1200;
1111
this.peepOutMin = 200;
1212
this.points = 10;
1313
this.type = 'normal';
14+
this.hide = this.hide;
15+
this.decrementLive = this.decrementLive;
16+
}
17+
18+
hide() {
19+
this.isVisible = false;
20+
}
21+
22+
decrementLive() {
23+
this.live--;
1424
}
1525
}
1626

0 commit comments

Comments
 (0)