Skip to content

Commit

Permalink
Throttle getAddressFromCoords
Browse files Browse the repository at this point in the history
  • Loading branch information
mjackson committed Dec 8, 2017
1 parent a512024 commit c1b9334
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 20 deletions.
15 changes: 7 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
"repository": "ReactTraining/react-workshop",
"homepage": "https://reacttraining.com",
"license": "GPL-3.0",
"authors": ["Ryan Florence", "Michael Jackson"],
"authors": [
"Ryan Florence",
"Michael Jackson"
],
"scripts": {
"start":
"node ./scripts/build.js && webpack-dev-server --inline --content-base subjects",
"server-exercise":
"supervisor -- -r babel-register subjects/ServerRendering/exercise/server.js",
"server-solution":
"supervisor -- -r babel-register subjects/ServerRendering/solution/server.js",
"start": "node ./scripts/build.js && webpack-dev-server --inline --content-base subjects",
"server-exercise": "supervisor -- -r babel-register subjects/ServerRendering/exercise/server.js",
"server-solution": "supervisor -- -r babel-register subjects/ServerRendering/solution/server.js",
"start-api": "node api.js"
},
"dependencies": {
Expand All @@ -30,7 +30,6 @@
"bootstrap": "^3.3.4",
"bootstrap-webpack": "0.0.5",
"css-loader": "^0.23.1",
"debounce-promise": "^3.0.1",
"events": "^1.0.2",
"expect": "^1.13.4",
"exports-loader": "^0.6.3",
Expand Down
38 changes: 26 additions & 12 deletions subjects/RenderProps/utils/getAddressFromCoords.js
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
import debounce from "debounce-promise"

const GoogleMapsAPI = "https://maps.googleapis.com/maps/api"

const unthrottledGetAddressFromCoords = (latitude, longitude) => {
const url = `${GoogleMapsAPI}/geocode/json?latlng=${latitude},${
longitude
}`
const getAddressFromCoords = (latitude, longitude) => {
const url = `${GoogleMapsAPI}/geocode/json?latlng=${latitude},${longitude}`

return fetch(url)
.then(res => res.json())
.then(json => json.results[0].formatted_address)
}

// Throttle requests to once per second
const getAddressFromCoords = debounce(
unthrottledGetAddressFromCoords,
1000
)
const maxCallsPerMinute = 20
let lastCallTime = 0
let lastWarningTime = 0
let promise = Promise.resolve()

const throttledGetAddressFromCoords = (latitude, longitude) => {
const currentTime = Date.now()
const requiredWaitTime = 60000 / maxCallsPerMinute

if (lastCallTime + requiredWaitTime < currentTime) {
lastCallTime = currentTime
promise = getAddressFromCoords(latitude, longitude)
} else if (currentTime - lastWarningTime > 5000) {
lastWarningTime = currentTime
window.alert(
"It looks like you're calling getAddressFromCoords many times " +
"quickly in a loop. Take a closer look at the componentDidUpdate " +
"function in <GeoAddress>..."
)
}

return promise
}

export default getAddressFromCoords
export default throttledGetAddressFromCoords

0 comments on commit c1b9334

Please sign in to comment.