I am a beginner at web development and currently learning React, Redux and Node. I am trying to develop a smart-parking simulation application. I have already developed a Node rest api. I have another single page application built using React and Redux.
I have assumed 7 parking places in a city(every parking place has a specific latitude and longitude). Every 5 seconds, I am assuming a car comes for parking at a random place. As soon as car appears, my application call the node-api getOptimalParking
and with certain algorithm it determines a parking place where it should be parked.
Now suppose a car 1 is assigned a parking place 1 and distance between them is 7 km. I am assuming it takes 1 sec to travel 1 km, hence car 1 will take 7 secs to reach parking place 1.
After 7 seconds my react application will call an api substractOneParkingSlot
.
What I want to achieve?
The thing I am unable to figure out is as every 5 seconds car Ci
keeps coming and I have to wait for Si
seconds for every car. As soon as Si
reaches zero, I call an api. How to handle this thing i.e. how to wait for Si
seconds before calling an api and meanwhile another car Cj
can come and again for that I have to wait for Sj
seconds independently.
Node-API list
getOptimalParking(userLat, userLng){
return {
distance: distance,
lat: parkingLat,
lng: parkingLng
}
}
substractOneParkingSlot(parkingLat, parkingLng){
if(parkingSlotAvailable) {
currentParkingSlot -= 1
return sucess
}
else
return failure
}
React-redux application working
As soon as car appears, I am calling a redux action that makes a request to getOptimalParking
api and then I have another redux action that calls substractOneParkingSlot
api after Ci
seconds.
As I have multiple such concurrent call to redux actions, how to achieve this?
Thanks
EDIT:- For future readers, there is a very detailed answer available here.