Skip to content
Snippets Groups Projects
Commit 19943a28 authored by Janne Alatalo's avatar Janne Alatalo :neutral_face:
Browse files

Fix race condition in data fetching

parent f970b5e9
No related branches found
No related tags found
No related merge requests found
Pipeline #3976 passed
...@@ -3,16 +3,10 @@ import { Map } from 'immutable'; ...@@ -3,16 +3,10 @@ import { Map } from 'immutable';
import PointMap from './PointMap.js'; import PointMap from './PointMap.js';
import './App.css'; import './App.css';
import { get_lam_points, connect_lam_ws, get_lam_station_data } from './utils/api-client.js'; import { get_lam_points, connect_lam_ws } from './utils/api-client.js';
class App extends Component { class App extends Component {
set_lam_points(lam_points) {
this.setState((prevState) => {
return { ...prevState, lam_points };
});
}
update_state(fn) { update_state(fn) {
this.setState((prevState) => { this.setState((prevState) => {
return fn(prevState); return fn(prevState);
...@@ -33,8 +27,7 @@ class App extends Component { ...@@ -33,8 +27,7 @@ class App extends Component {
let width = window.innerWidth; let width = window.innerWidth;
let height = window.innerHeight; let height = window.innerHeight;
this.state = { lam_points: Map(), mapSize: { width, height } }; this.state = { lam_points: Map(), mapSize: { width, height } };
get_lam_points("http://tie.digitraffic.fi", this.set_lam_points.bind(this)); get_lam_points("http://tie.digitraffic.fi", this.update_state.bind(this));
get_lam_station_data("http://tie.digitraffic.fi", this.update_state.bind(this));
connect_lam_ws("ws://tie.digitraffic.fi", this.update_state.bind(this)); connect_lam_ws("ws://tie.digitraffic.fi", this.update_state.bind(this));
this.handleResize = this.handleResize.bind(this); this.handleResize = this.handleResize.bind(this);
} }
......
...@@ -3,7 +3,7 @@ import { Map } from 'immutable'; ...@@ -3,7 +3,7 @@ import { Map } from 'immutable';
import LamStation from './lam-station.js'; import LamStation from './lam-station.js';
export function get_lam_points(api_url, set_state) { export function get_lam_points(api_url, set_state) {
fetch(`${api_url}/api/v1/metadata/tms-stations`) return fetch(`${api_url}/api/v1/metadata/tms-stations`)
.then((res) => res.json()) .then((res) => res.json())
.then((json) => { .then((json) => {
let stations = Map(); let stations = Map();
...@@ -13,13 +13,17 @@ export function get_lam_points(api_url, set_state) { ...@@ -13,13 +13,17 @@ export function get_lam_points(api_url, set_state) {
let station = new LamStation(id, coordinates, 0, 0); let station = new LamStation(id, coordinates, 0, 0);
stations = stations.set(id, station); stations = stations.set(id, station);
} }
set_state(stations); set_state((prevState) => {
const lam_points = stations;
return { ...prevState, lam_points };
});
}) })
.then(() => get_lam_station_data(api_url, set_state))
.catch(e => (console.error(e))); .catch(e => (console.error(e)));
} }
export function get_lam_station_data(api_url, set_state) { export function get_lam_station_data(api_url, set_state) {
fetch(`${api_url}/api/v1/data/tms-data`) return fetch(`${api_url}/api/v1/data/tms-data`)
.then((res) => res.json()) .then((res) => res.json())
.then((json) => { .then((json) => {
set_state((prevState) => { set_state((prevState) => {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment