import { sharedMemory } from "./sharedMemory"; import { Stop, System } from "./generated/graphql"; const baseUrl = "https://passiogo.com/mapGetData.php"; async function updateStopAndOrderedStopDataForSystem(systemToConstruct: System) { const params = { getStops: "2", }; const formDataJsonObject = { "s0": systemToConstruct.id, "sA": "1", } const formData = new FormData(); formData.set("json", JSON.stringify(formDataJsonObject)); const query = new URLSearchParams(params).toString(); const response = await fetch(`${baseUrl}?${query}`, { method: "POST", body: formData, }); const json = await response.json(); if (typeof json.stops === "object") { const jsonStops = Object.values(json.stops); systemToConstruct.stops = jsonStops.map((jsonStop: any) => { return { id: jsonStop.id, name: jsonStop.name, coordinates: { latitude: jsonStop.latitude, longitude: jsonStop.longitude, }, system: systemToConstruct, etas: [], orderedStops: [], } }); } } async function updateSystemData() { // Construct the graph from the bottom up, so that // system data is populated with all necessary fields const params = { getSystems: "2", }; const query = new URLSearchParams(params).toString(); const response = await fetch(`${baseUrl}?${query}`); const json = await response.json() if (typeof json.all === "object") { sharedMemory.systems = await Promise.all(json.all.map(async (jsonSystem: any) => { const constructedSystem: System = { id: jsonSystem.id, name: jsonSystem.fullname, routes: [], stops: [], shuttles: [], }; try { await updateStopAndOrderedStopDataForSystem(constructedSystem); } catch (e) { console.error(e); } return constructedSystem; })); } } async function fetchDataAndUpdateStore() { try { await updateSystemData(); } catch (e) { console.error(e); } } async function runDataUpdateScheduler() { // while (true) { const timeoutInMilliseconds = 10000; await fetchDataAndUpdateStore(); await new Promise((resolve) => setTimeout(resolve, timeoutInMilliseconds)); // } } function startDataUpdater() { runDataUpdateScheduler(); } export { startDataUpdater };