Skip to content

Commit

Permalink
fix: cancel geocoding process on nav
Browse files Browse the repository at this point in the history
Also, clean up listener for updates.

Fixes: #29
  • Loading branch information
stdavis authored and steveoh committed Oct 12, 2021
1 parent 68acc9c commit 64a7899
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
17 changes: 10 additions & 7 deletions src/pages/Geocoding.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ export default function Geocoding() {
status: 'idle',
});
const startTime = useRef(new Date());
const abortController = useRef(new AbortController());
const geocodeContext = useGeocodeContext()[0];
const draggable = useRef(null);

Expand All @@ -24,8 +23,12 @@ export default function Geocoding() {
window.ugrc.startDrag('ugrc_geocode_results.csv');
};

const cancel = () => {
window.ugrc.cancelGeocode();
};

useEffect(() => {
window.ugrc.onGeocodingUpdate((_, data) => {
window.ugrc.subscribeToGeocodingUpdates((_, data) => {
setStats(data);
});

Expand All @@ -35,20 +38,20 @@ export default function Geocoding() {
fields: geocodeContext.fields,
apiKey: geocodeContext.apiKey,
wkid,
abortSignal: abortController.current.signal,
});
});

return () => {
cancel();
window.ugrc.unsubscribeFromGeocodingUpdates();
};
}, [geocodeContext]);

const progress = stats.rowsProcessed / stats.totalRows || 0;
const elapsedTime = new Date().getTime() - startTime.current.getTime();
const timePerRow = elapsedTime / stats.rowsProcessed;
const estimatedTimeRemaining = timePerRow * (stats.totalRows - stats.rowsProcessed);

const cancel = () => {
window.ugrc.cancelGeocode();
};

return (
<article>
<Link type="back-button" to="/plan">
Expand Down
3 changes: 2 additions & 1 deletion src/services/preload.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ contextBridge.exposeInMainWorld('ugrc', {
getRecordCount: (content) => ipcRenderer.invoke('getRecordCount', content),
saveConfig: (content) => ipcRenderer.send('saveConfig', content),
getConfigItem: (content) => ipcRenderer.invoke('getConfigItem', content),
onGeocodingUpdate: (event, arg) => ipcRenderer.on('onGeocodingUpdate', event, arg),
subscribeToGeocodingUpdates: (event, arg) => ipcRenderer.on('onGeocodingUpdate', event, arg),
unsubscribeFromGeocodingUpdates: () => ipcRenderer.removeAllListeners('onGeocodingUpdate'),
geocode: (content) => ipcRenderer.send('geocode', content),
cancelGeocode: (content) => ipcRenderer.send('cancelGeocode', content),
startDrag: (content) => ipcRenderer.send('ondragstart', content),
Expand Down

0 comments on commit 64a7899

Please sign in to comment.