@@ -64,27 +64,16 @@ export function linkCrashData() {
6464}
6565
6666/**
67- * Load units data with caching
67+ * Load units data with caching (standalone version for parallel loading)
6868 */
69- export async function loadUnitsData ( ) {
70- showLoading ( 'Loading units data...' ) ;
71-
69+ async function loadUnitsDataOnly ( ) {
7270 try {
7371 // Try cache first
7472 const cached = await dbCache . getFresh ( 'crashData' , 'units-2012-2024' , 7 * 24 * 60 * 60 * 1000 ) ;
7573
7674 if ( cached ) {
7775 console . log ( '✅ Using cached units data' ) ;
7876 updateDataState ( { unitsData : cached } ) ;
79- linkCrashData ( ) ;
80-
81- const { populateFilterOptions } = await import ( './filters.js' ) ;
82- const { updateMarkerColorLegend } = await import ( './map-renderer.js' ) ;
83-
84- populateFilterOptions ( ) ;
85- updateMarkerColorLegend ( ) ;
86- await loadLGABoundaries ( ) ;
87-
8877 return cached ;
8978 }
9079
@@ -106,6 +95,24 @@ export async function loadUnitsData() {
10695 console . warn ( 'Failed to cache units data:' , cacheError ) ;
10796 }
10897
98+ return unitsData ;
99+
100+ } catch ( error ) {
101+ console . error ( 'Error loading units data:' , error ) ;
102+ throw error ;
103+ }
104+ }
105+
106+ /**
107+ * Load units data with caching (legacy - kept for compatibility)
108+ * @deprecated Use loadData() instead for parallel loading
109+ */
110+ export async function loadUnitsData ( ) {
111+ showLoading ( 'Loading units data...' ) ;
112+
113+ try {
114+ const unitsData = await loadUnitsDataOnly ( ) ;
115+
109116 // Link data together
110117 linkCrashData ( ) ;
111118
@@ -122,27 +129,23 @@ export async function loadUnitsData() {
122129 return unitsData ;
123130
124131 } catch ( error ) {
125- console . error ( 'Error loading units data:' , error ) ;
126132 hideLoading ( ) ;
127133 alert ( 'Error loading units data. Please check your connection and try again.' ) ;
128134 throw error ;
129135 }
130136}
131137
132138/**
133- * Load casualty data with caching
139+ * Load casualty data with caching (standalone version for parallel loading)
134140 */
135- export async function loadCasualtyData ( ) {
136- showLoading ( 'Loading casualty data...' ) ;
137-
141+ async function loadCasualtyDataOnly ( ) {
138142 try {
139143 // Try cache first
140144 const cached = await dbCache . getFresh ( 'crashData' , 'casualty-2012-2024' , 7 * 24 * 60 * 60 * 1000 ) ;
141145
142146 if ( cached ) {
143147 console . log ( '✅ Using cached casualty data' ) ;
144148 updateDataState ( { casualtyData : cached } ) ;
145- await loadUnitsData ( ) ;
146149 return cached ;
147150 }
148151
@@ -164,35 +167,43 @@ export async function loadCasualtyData() {
164167 console . warn ( 'Failed to cache casualty data:' , cacheError ) ;
165168 }
166169
167- // Load units data next
168- await loadUnitsData ( ) ;
169-
170170 return casualtyData ;
171171
172172 } catch ( error ) {
173173 console . error ( 'Error loading casualty data:' , error ) ;
174+ throw error ;
175+ }
176+ }
177+
178+ /**
179+ * Load casualty data with caching (legacy - kept for compatibility)
180+ * @deprecated Use loadData() instead for parallel loading
181+ */
182+ export async function loadCasualtyData ( ) {
183+ showLoading ( 'Loading casualty data...' ) ;
184+
185+ try {
186+ await loadCasualtyDataOnly ( ) ;
187+ // Load units data next
188+ await loadUnitsData ( ) ;
189+ } catch ( error ) {
174190 hideLoading ( ) ;
175191 alert ( 'Error loading casualty data. Please check your connection and try again.' ) ;
176192 throw error ;
177193 }
178194}
179195
180196/**
181- * Load and decompress crash data with caching
197+ * Load and decompress crash data with caching (standalone version for parallel loading)
182198 */
183- export async function loadCrashData ( ) {
184- showLoading ( 'Loading crash data...' ) ;
185-
199+ async function loadCrashDataOnly ( ) {
186200 try {
187201 // Try to load from IndexedDB cache first
188202 const cached = await dbCache . getFresh ( 'crashData' , 'crash-2012-2024' , 7 * 24 * 60 * 60 * 1000 ) ; // 7 days
189203
190204 if ( cached ) {
191205 console . log ( '✅ Using cached crash data' ) ;
192206 updateDataState ( { crashData : cached } ) ;
193-
194- // Load casualty data next
195- await loadCasualtyData ( ) ;
196207 return cached ;
197208 }
198209
@@ -231,13 +242,26 @@ export async function loadCrashData() {
231242 // Non-fatal, continue anyway
232243 }
233244
234- // Load casualty data next
235- await loadCasualtyData ( ) ;
236-
237245 return crashData ;
238246
239247 } catch ( error ) {
240248 console . error ( 'Error loading crash data:' , error ) ;
249+ throw error ;
250+ }
251+ }
252+
253+ /**
254+ * Load and decompress crash data with caching (legacy - kept for compatibility)
255+ * @deprecated Use loadData() instead for parallel loading
256+ */
257+ export async function loadCrashData ( ) {
258+ showLoading ( 'Loading crash data...' ) ;
259+
260+ try {
261+ await loadCrashDataOnly ( ) ;
262+ // Load casualty data next
263+ await loadCasualtyData ( ) ;
264+ } catch ( error ) {
241265 hideLoading ( ) ;
242266 alert ( 'Error loading crash data. Please check your connection and try again.' ) ;
243267 throw error ;
@@ -364,12 +388,34 @@ export function precomputeLGAAssignments() {
364388}
365389
366390/**
367- * Main data loading function
391+ * Main data loading function - parallelized for faster loading
368392 */
369393export async function loadData ( ) {
370394 try {
371- await loadCrashData ( ) ;
372- // Casualty and units data are loaded in sequence by loadCrashData
395+ // Load all three datasets in parallel for faster performance
396+ showLoading ( 'Loading crash data (1/3)...' ) ;
397+
398+ const [ crashDataResult , casualtyDataResult , unitsDataResult ] = await Promise . all ( [
399+ loadCrashDataOnly ( ) ,
400+ loadCasualtyDataOnly ( ) ,
401+ loadUnitsDataOnly ( )
402+ ] ) ;
403+
404+ console . log ( '✅ All datasets loaded in parallel' ) ;
405+
406+ // Link data together after all loaded
407+ linkCrashData ( ) ;
408+
409+ // After linking, populate filter options and load boundaries
410+ const { populateFilterOptions } = await import ( './filters.js' ) ;
411+ const { updateMarkerColorLegend } = await import ( './map-renderer.js' ) ;
412+
413+ populateFilterOptions ( ) ;
414+ updateMarkerColorLegend ( ) ;
415+
416+ // Load LGA boundaries (which will trigger initial filter application)
417+ await loadLGABoundaries ( ) ;
418+
373419 } catch ( error ) {
374420 console . error ( 'Failed to load data:' , error ) ;
375421 }
0 commit comments