-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtimeline-worker.js
More file actions
43 lines (42 loc) · 2.06 KB
/
Copy pathtimeline-worker.js
File metadata and controls
43 lines (42 loc) · 2.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
* Web Worker: parse timeline JSON and run processTimelineData + calculateStats off the main thread.
* Loads rbush (for spatial index) and timeline-utils via importScripts.
*/
importScripts('https://cdn.jsdelivr.net/npm/[email protected]/rbush.min.js', 'timeline-utils.js');
self.onmessage = function (e) {
const { jsonText, countryGeoJSON, probabilityThreshold } = e.data || {};
try {
if (countryGeoJSON && typeof timelineUtils !== 'undefined' && timelineUtils.setCountryGeoJSON) {
timelineUtils.setCountryGeoJSON(countryGeoJSON);
}
const json = JSON.parse(jsonText);
const segments = timelineUtils.getSegmentsFromData(json);
if (!segments.length) {
const hint = Array.isArray(json) ? ' (root array was empty)' : ` (expected semanticSegments or root array; got keys: ${Object.keys(json).slice(0, 5).join(', ')})`;
throw new Error('Invalid JSON structure. No timeline segments found' + hint);
}
const options = probabilityThreshold != null ? { probabilityThreshold } : {};
const processed = timelineUtils.processTimelineData(json, options);
const initialStats = timelineUtils.calculateStats(processed.allSegments);
const payload = {
allSegments: processed.allSegments,
allLocations: processed.allLocations,
years: processed.years,
initialStats: {
totalDistanceMeters: initialStats.totalDistanceMeters,
totalVisits: initialStats.totalVisits,
countries: Array.from(initialStats.countries),
transport: initialStats.transport,
visits: initialStats.visits,
visitTypes: initialStats.visitTypes
}
};
self.postMessage(payload);
} catch (err) {
const msg = err && err.message ? err.message : String(err);
if (typeof console !== 'undefined' && console.error) {
console.error('[timeline-worker] Parse/process error:', msg, err);
}
self.postMessage({ error: msg });
}
};