Skip to content

Commit 0f91ad1

Browse files
author
Stephen Algeo
committed
Order markets and debounce expensive operation
1 parent 57e504f commit 0f91ad1

File tree

10 files changed

+77
-11
lines changed

10 files changed

+77
-11
lines changed

app/src/Scenes/Event.js

+16-3
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,34 @@
11
import React from "react";
22
import Market from "../components/Market";
33
import { useParams, Link } from "react-router-dom";
4-
import useEvent from "../hooks/useEvent";
54
import formatStartTime from "../helpers/startTime";
65
import OddsToggle from "../components/OddsToggle";
6+
import { useDebounce, useEvent, useStore, useMarkets } from "../hooks/";
77

88
const INITIAL_MARKETS = 10;
99

1010
const Event = () => {
1111
const { eventId } = useParams();
1212
const event = useEvent(eventId);
1313

14+
useMarkets(event);
15+
16+
const allMarkets = useDebounce(useStore("markets"), 10);
17+
18+
const orderedMarkets = React.useMemo(
19+
() =>
20+
Object.values(allMarkets)
21+
.sort((a, b) => a.displayOrder > b.displayOrder)
22+
.map(({ marketId }) => marketId)
23+
.slice(0, INITIAL_MARKETS),
24+
[allMarkets]
25+
);
26+
1427
if (!event) {
1528
return <div>Loading...</div>;
1629
}
1730

18-
const { markets, startTime } = event;
31+
const { startTime } = event;
1932

2033
return (
2134
<div className="c-full-event">
@@ -47,7 +60,7 @@ const Event = () => {
4760
<div className="c-full-event__meta-item">Request a Bet</div>
4861
)}
4962
</div>
50-
{markets.slice(0, INITIAL_MARKETS).map(id => (
63+
{orderedMarkets.slice(0, INITIAL_MARKETS).map(id => (
5164
<Market key={id} id={id} />
5265
))}
5366
</div>

app/src/components/Market.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import React from "react";
22
import Outcome from "./Outcome";
3-
import useMarket from "../hooks/useMarket";
3+
import { useMarket } from "../hooks/";
44

55
const Market = ({ id }) => {
66
const market = useMarket(id);

app/src/components/Outcome.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React, { useContext } from "react";
2-
import useOutcome from "../hooks/useOutcome";
2+
import { useOutcome } from "../hooks/";
33
import { OddsContext } from "../App";
44

55
const Outcome = ({ id }) => {

app/src/hooks/index.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import useDebounce from "./useDebounce";
2+
import useEvent from "./useEvent";
3+
import useLiveEvents from "./useLiveEvents";
4+
import useMarket from "./useMarket";
5+
import useMarkets from "./useMarkets";
6+
import useOutcome from "./useOutcome";
7+
import useStore from "./useStore";
8+
9+
export {
10+
useDebounce,
11+
useEvent,
12+
useLiveEvents,
13+
useMarket,
14+
useMarkets,
15+
useOutcome,
16+
useStore
17+
};

app/src/hooks/useDebounce.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { useState, useEffect } from "react";
2+
3+
const useDebounce = (value, delay) => {
4+
const [debouncedValue, setDebouncedValue] = useState(value);
5+
6+
useEffect(() => {
7+
const handler = setTimeout(() => {
8+
setDebouncedValue(value);
9+
}, delay);
10+
11+
return () => {
12+
clearTimeout(handler);
13+
};
14+
}, [value]);
15+
16+
return debouncedValue;
17+
};
18+
19+
export default useDebounce;

app/src/hooks/useEvent.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useContext, useEffect } from "react";
22
import { SocketContext } from "../App";
3-
import useStore from "./useStore";
3+
import { useStore } from "./";
44

55
const useEvent = id => {
66
const [socket] = useContext(SocketContext);

app/src/hooks/useLiveEvents.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useContext, useEffect } from "react";
22
import { SocketContext } from "../App";
3-
import useStore from "./useStore";
3+
import { useStore } from "./";
44

55
const useLiveEvents = () => {
66
const [socket] = useContext(SocketContext);

app/src/hooks/useMarket.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { useContext, useEffect } from "react";
22
import { SocketContext } from "../App";
3-
import useStore from "./useStore";
3+
import { useStore } from "./";
44

55
const useMarket = id => {
66
const [socket] = useContext(SocketContext);
77
const market = useStore("markets", id);
88

99
useEffect(() => {
10-
socket.send(JSON.stringify({ type: "getMarket", id }));
11-
}, [id, socket]);
10+
if (!market) {
11+
socket.send(JSON.stringify({ type: "getMarket", id }));
12+
}
13+
}, [id, socket, market]);
1214

1315
return market;
1416
};

app/src/hooks/useMarkets.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { useContext, useEffect } from "react";
2+
import { SocketContext } from "../App";
3+
4+
const useMarkets = event => {
5+
const [socket] = useContext(SocketContext);
6+
7+
useEffect(() => {
8+
if (!event) return;
9+
event.markets.forEach(id =>
10+
socket.send(JSON.stringify({ type: "getMarket", id }))
11+
);
12+
}, [event, socket]);
13+
};
14+
15+
export default useMarkets;

app/src/hooks/useOutcome.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { useContext, useEffect } from "react";
22
import { SocketContext } from "../App";
3-
import useStore from "./useStore";
3+
import { useStore } from "./";
44

55
const useOutcome = id => {
66
const outcome = useStore("outcomes", id);

0 commit comments

Comments
 (0)