Skip to content

Commit

Permalink
perf: add execution pull to get vehicle locations
Browse files Browse the repository at this point in the history
  • Loading branch information
NoamGaash committed Aug 26, 2023
1 parent 11638b7 commit 1ad38bc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
30 changes: 22 additions & 8 deletions src/api/useVehicleLocations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ class LocationObservable {
}&offset=${offset}`
if (lineRef) url += `&${config.lineRefField}=${lineRef}`

const response = await fetch(url)
const data = await response.json()
const response = await fetchWithQueue(url)
const data = await response!.json()
if (data.length === 0) {
this.loading = false
this.#notifyObservers({
Expand Down Expand Up @@ -85,6 +85,19 @@ class LocationObservable {
}
}

const pool = new Array(10).fill(0).map(() => Promise.resolve<void | Response>(void 0))
async function fetchWithQueue(url: string, retries = 10) {
let queue = pool.shift()!
queue = queue
.then(() => fetch(url))
.catch(async () => {
await new Promise((resolve) => setTimeout(resolve, 10000 * Math.random() + 100))
return fetchWithQueue(url, retries - 1)
})
pool.push(queue)
return queue
}

// this function checks the cache for the data, and if it's not there, it loads it
function getLocations({
from,
Expand All @@ -105,14 +118,15 @@ function getLocations({
return observable.observe(onUpdate)
}

function getMinutesInRange(from: Dateable, to: Dateable) {
function getMinutesInRange(from: Dateable, to: Dateable, minutesGap = 1) {
const start = new Date(from).setSeconds(0, 0)
const end = new Date(to).setSeconds(0, 0)
const gap = 60000 * minutesGap

// array of minutes to load
const minutes = Array.from({ length: (end - start) / 60000 }, (_, i) => ({
from: new Date(start + i * 60000),
to: new Date(start + (i + 1) * 60000),
const minutes = Array.from({ length: (end - start) / gap }, (_, i) => ({
from: new Date(start + i * gap),
to: new Date(start + (i + 1) * gap),
}))
return minutes
}
Expand All @@ -121,12 +135,12 @@ export default function useVehicleLocations({
from,
to,
lineRef,
splitMinutes: split = true,
splitMinutes: split = 1,
}: {
from: Dateable
to: Dateable
lineRef?: number
splitMinutes?: boolean
splitMinutes?: false | number
}) {
const [locations, setLocations] = useState<VehicleLocation[]>([])
const [isLoading, setIsLoading] = useState<boolean[]>([])
Expand Down
2 changes: 1 addition & 1 deletion src/pages/SingleLineMapPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const SingleLineMapPage = () => {
from: selectedRouteIds ? new Date(timestamp).setHours(0, 0, 0, 0) : 0,
to: selectedRouteIds ? new Date(timestamp).setHours(23, 59, 59, 999) : 0,
lineRef: selectedRoute?.lineRef ?? 0,
// splitMinutes: false, // uncomment to load everything in one request
splitMinutes: 20,
})

const positions = useMemo(() => {
Expand Down

0 comments on commit 1ad38bc

Please sign in to comment.