Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[Chore] Better handle add data to map error and loading indicator (#3106)

Shan He3769aaf74fff
diff --git a/src/actions/src/ui-state-actions.ts b/src/actions/src/ui-state-actions.ts
index b8a02ad88..39d360c66 100644
--- a/src/actions/src/ui-state-actions.ts
+++ b/src/actions/src/ui-state-actions.ts
@@ -495,25 +495,3 @@ export const togglePanelListView: (
// @ts-ignore
const uiStateActions = null;
/* eslint-enable @typescript-eslint/no-unused-vars */
-
-/** SET_LOADING_INDICATOR */
-export type SetLoadingIndicatorAction = {
- payload: {
- change: number;
- };
-};
-
-/**
- * Change of number of active loading actions.
- * @memberof uiStateActions
- * @param payload
- * @param payload.change Change of number of active loading actions.
- * @public
- */
-export const setLoadingIndicator: (
- payload: SetLoadingIndicatorAction['payload']
-) => Merge<SetLoadingIndicatorAction, {type: typeof ActionTypes.SET_LOADING_INDICATOR}> =
- createAction(
- ActionTypes.SET_LOADING_INDICATOR,
- (payload: SetLoadingIndicatorAction['payload']) => ({payload})
- );
diff --git a/src/actions/src/vis-state-actions.ts b/src/actions/src/vis-state-actions.ts
index 603cd0b9c..b8bd3ccc5 100644
--- a/src/actions/src/vis-state-actions.ts
+++ b/src/actions/src/vis-state-actions.ts
@@ -1634,6 +1634,13 @@ export type SyncTimeFilterWithLayerTimelineAction = {
enable: boolean;
};
+/**
+ * Sync time filter with layer timeline
+ * @memberof visStateActions
+ * @param idx
+ * @param enable
+ * @return action
+ */
export function syncTimeFilterWithLayerTimeline(
idx: SyncTimeFilterWithLayerTimelineAction['idx'],
enable: SyncTimeFilterWithLayerTimelineAction['enable']
@@ -1653,6 +1660,13 @@ export type setTimeFilterSyncTimelineModeAction = {
mode: SyncTimelineMode;
};
+/**
+ * Set time filter sync timeline mode
+ * @memberof visStateActions
+ * @param id
+ * @param mode
+ * @return action
+ */
export function setTimeFilterSyncTimelineMode({
id,
mode
@@ -1674,6 +1688,7 @@ export type CreateNewDatasetSuccessPayload = {
/**
* Called when a new dataset is created successfully via async table methods
+ * @memberof visStateActions
* @param payload
* @param payload.results - results of promises.allSettlted
* @returns
@@ -1682,6 +1697,21 @@ export const createNewDatasetSuccess = createAction<CreateNewDatasetSuccessPaylo
ActionTypes.CREATE_NEW_DATASET_SUCCESS
);
+export type SetLoadingIndicatorPayload = {
+ change: number;
+};
+
+/**
+ * Change of number of active loading items, used to render loading indicator.
+ * @memberof visStateActions
+ * @param payload
+ * @param payload.change Change of number of active loading actions.
+ * @public
+ */
+export const setLoadingIndicator = createAction<SetLoadingIndicatorPayload>(
+ ActionTypes.SET_LOADING_INDICATOR
+);
+
/**
* This declaration is needed to group actions in docs
*/
diff --git a/src/components/src/kepler-gl.tsx b/src/components/src/kepler-gl.tsx
index f8e63a731..06cb99e3a 100644
--- a/src/components/src/kepler-gl.tsx
+++ b/src/components/src/kepler-gl.tsx
@@ -177,7 +177,7 @@ export const mapFieldsSelector = (props: KeplerGLProps, index = 0) => ({
mapControls: props.uiState.mapControls,
readOnly: props.uiState.readOnly,
locale: props.uiState.locale,
- isLoadingIndicatorVisible: Number(props.uiState.loadingIndicatorValue) > 0,
+ isLoadingIndicatorVisible: Number(props.visState.loadingIndicatorValue) > 0,
sidePanelWidth: props.sidePanelWidth ? props.sidePanelWidth : DEFAULT_KEPLER_GL_PROPS.width,
// mapStyle
diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsx
index d02441b26..05bcc2d35 100644
--- a/src/components/src/map-container.tsx
+++ b/src/components/src/map-container.tsx
@@ -1015,7 +1015,7 @@ export default function MapContainerFactory(
_onLayerLoadingStateChange = debounce(() => {
// trigger loading indicator update without any change to update UI
- this.props.uiStateActions.setLoadingIndicator({change: 0});
+ this.props.visStateActions.setLoadingIndicator({change: 0});
}, DEBOUNCE_LOADING_STATE_PROPAGATE);
_toggleMapControl = panelId => {
diff --git a/src/duckdb/src/table/duckdb-table.ts b/src/duckdb/src/table/duckdb-table.ts
index dbb14597b..aa24bf3ec 100644
--- a/src/duckdb/src/table/duckdb-table.ts
+++ b/src/duckdb/src/table/duckdb-table.ts
@@ -127,6 +127,7 @@ export class KeplerGlDuckDbTable extends KeplerTable {
await c.query(createTableSql);
} catch (error) {
console.log('importRowData', error);
+ throw error;
}
}
@@ -143,9 +144,11 @@ export class KeplerGlDuckDbTable extends KeplerTable {
FROM ST_READ('${this.id}', keep_wkb = TRUE);
ALTER TABLE '${this.label}' RENAME '${DUCKDB_WKB_COLUMN}' TO '${KEPLER_GEOM_FROM_GEOJSON_COLUMN}';
`;
+
await c.query(createTableSql);
} catch (error) {
console.error('importGeoJsonData', error);
+ throw error;
}
return {
@@ -180,6 +183,7 @@ export class KeplerGlDuckDbTable extends KeplerTable {
// Known issues:
// 1) Arrow Type with extension name: geoarrow.point and format: +w:2 is not currently supported in DuckDB.
console.error('importArrowData', error);
+ throw error;
}
return {
@@ -252,6 +256,7 @@ export class KeplerGlDuckDbTable extends KeplerTable {
.filter(col => col) as arrow.Vector[];
} catch (error) {
console.error('DuckDB table: createTableAndGetArrow', error);
+ throw error;
}
await c.close();
diff --git a/src/processors/src/file-handler.ts b/src/processors/src/file-handler.ts
index d137f4b30..b54bdc5db 100644
--- a/src/processors/src/file-handler.ts
+++ b/src/processors/src/file-handler.ts
@@ -216,7 +216,6 @@ export async function processFileData({
const {fileName, data} = content;
let format: string | undefined;
let processor: ((data: any) => ProcessorResult | LoadedMap | null) | undefined;
- console.log('Processing file', fileName);
// generate unique id with length of 4 using fileName string
const id = generateHashIdFromString(fileName);
// decide on which table class to use based on application config
diff --git a/src/reducers/src/combined-updaters.ts b/src/reducers/src/combined-updaters.ts
index a5737c0c8..6222316ec 100644
--- a/src/reducers/src/combined-updaters.ts
+++ b/src/reducers/src/combined-updaters.ts
@@ -183,6 +183,7 @@ export const addDataToMapUpdater = (
return compose_<KeplerGlState>([
pick_('visState')(
+ // this part can be async
apply_<VisState, any>(visStateUpdateVisDataUpdater, {
datasets,
options,
diff --git a/src/reducers/src/ui-state-updaters.ts b/src/reducers/src/ui-state-updaters.ts
index a6c827fca..f0df2dccf 100644
--- a/src/reducers/src/ui-state-updaters.ts
+++ b/src/reducers/src/ui-state-updaters.ts
@@ -925,27 +925,3 @@ export const togglePanelListViewUpdater = (
[stateProp]: listView
};
};
-
-/**
- * Update state of the loading indicator.
- * @memberof uiStateUpdaters
- * @param state uiState
- * @param action
- * @param action.payload Payload with change of number of active loading actions.
- * @returns nextState
- * @public
- */
-export const setLoadingIndicatorUpdater = (
- state: UiState,
- {payload: {change}}: UIStateActions.SetLoadingIndicatorAction
-): UiState => {
- let {loadingIndicatorValue} = state;
- if (!loadingIndicatorValue) {
- loadingIndicatorValue = 0;
- }
-
- return {
- ...state,
- loadingIndicatorValue: Math.max(loadingIndicatorValue + change, 0)
- };
-};
diff --git a/src/reducers/src/ui-state.ts b/src/reducers/src/ui-state.ts
index 88a0f0e53..95fa20444 100644
--- a/src/reducers/src/ui-state.ts
+++ b/src/reducers/src/ui-state.ts
@@ -44,9 +44,7 @@ const actionHandler = {
[ActionTypes.TOGGLE_SPLIT_MAP]: uiStateUpdaters.toggleSplitMapUpdater,
[ActionTypes.SHOW_DATASET_TABLE]: uiStateUpdaters.showDatasetTableUpdater,
[ActionTypes.SET_LOCALE]: uiStateUpdaters.setLocaleUpdater,
- [ActionTypes.TOGGLE_PANEL_LIST_VIEW]: uiStateUpdaters.togglePanelListViewUpdater,
-
- [ActionTypes.SET_LOADING_INDICATOR]: uiStateUpdaters.setLoadingIndicatorUpdater
+ [ActionTypes.TOGGLE_PANEL_LIST_VIEW]: uiStateUpdaters.togglePanelListViewUpdater
};
/* Reducer */
diff --git a/src/reducers/src/vis-state-updaters.ts b/src/reducers/src/vis-state-updaters.ts
index c8e0118f0..98cca04a7 100644
--- a/src/reducers/src/vis-state-updaters.ts
+++ b/src/reducers/src/vis-state-updaters.ts
@@ -42,9 +42,9 @@ import {
setFilter,
processFileContent,
fitBounds as fitMapBounds,
- setLoadingIndicator,
toggleLayerForMap,
- applyFilterConfig
+ applyFilterConfig,
+ SetLoadingIndicatorPayload
} from '@kepler.gl/actions';
// Utils
@@ -102,6 +102,7 @@ import {
compose_,
filterOutById,
merge_,
+ payload_,
pick_,
removeElementAtIndex,
swap_
@@ -323,6 +324,8 @@ export const INITIAL_VIS_STATE: VisState = {
fileLoading: false,
fileLoadingProgress: {},
+ // for loading datasets
+ loadingIndicatorValue: 0,
loaders: [],
loadOptions: {},
@@ -334,6 +337,18 @@ export const INITIAL_VIS_STATE: VisState = {
schema: KeplerGLSchema
};
+export const ACTION_TASK_FIT_BOUNDS = Task.fromCallback(
+ (_, cb) => cb(),
+
+ 'ACTION_TASK_FIT_BOUNDS'
+);
+
+export const ACTION_TASK_ADD_NOTIFICATION = Task.fromCallback(
+ (_, cb) => cb(),
+
+ 'ACTION_TASK_ADD_NOTIFICATION'
+);
+
type UpdateStateWithLayerAndDataType = {
layers: Layer[];
layerData: any[];
@@ -2356,7 +2371,7 @@ export const updateVisDataUpdater = (
createDatasetTasks.push(task);
} else {
notificationTasks.push(
- ACTION_TASK().map(() =>
+ ACTION_TASK_ADD_NOTIFICATION().map(() =>
addNotification(
errorNotification({
message: `Failed to create a new dataset due to data verification errors`,
@@ -2375,11 +2390,7 @@ export const updateVisDataUpdater = (
: null;
if (datasetsAllSettledTask) {
- // indicate that something is in progress
- const setIsLoadingTask = ACTION_TASK().map(() => {
- return setLoadingIndicator({change: 1});
- });
- updatedState = withTask(updatedState, setIsLoadingTask);
+ updatedState = setLoadingIndicatorUpdater(updatedState, payload_({change: 1, type: ''}));
}
return withTask(updatedState, [
@@ -2392,17 +2403,25 @@ export const createNewDatasetSuccessUpdater = (
state: VisState,
action: PayloadAction<CreateNewDatasetSuccessPayload>
): VisState => {
- // console.log('createNewDatasetSuccessUpdater', action.payload);
const {results, addToMapOptions} = action.payload;
- const newDataEntries = results.reduce((accu, result) => {
+ const notificationTasks: Task[] = [];
+
+ const newDataEntries = results.reduce((accu, result, idx) => {
if (result.status === 'fulfilled') {
const dataset = result.value;
return {...accu, [dataset.id]: dataset};
} else {
- // handle create dataset error
- console.error(
- 'createNewDatasetSuccessUpdater: failed',
- result.reason || (result as any).value
+ // show error notification on UI
+ notificationTasks.push(
+ ACTION_TASK().map(() =>
+ addNotification(
+ errorNotification({
+ message: `Dataset error: Failed to create a new dataset:
+ ${result.reason || (result as any).value}`,
+ id: `dataset-create-failed-${idx}`
+ })
+ )
+ )
);
return accu;
}
@@ -2429,11 +2448,10 @@ export const createNewDatasetSuccessUpdater = (
postMergerPayload
});
- // resolve active loading initiated by updateVisDataUpdater
- const setIsLoadingTask = ACTION_TASK().map(() => {
- return setLoadingIndicator({change: -1});
- });
- return withTask(updatedState, setIsLoadingTask);
+ return withTask(
+ setLoadingIndicatorUpdater(updatedState, payload_({change: -1})),
+ notificationTasks
+ );
};
/**
@@ -2546,13 +2564,14 @@ function postMergeUpdater(mergedState: VisState, postMergerPayload: PostMergerPa
if (newLayers.length && (options || {}).centerMap) {
const bounds = findMapBounds(newLayers);
if (bounds) {
- const fitBoundsTask = ACTION_TASK().map(() => {
+ const fitBoundsTask = ACTION_TASK_FIT_BOUNDS().map(() => {
return fitMapBounds(bounds);
});
updatedState = withTask(updatedState, fitBoundsTask);
}
}
+ // need to center map here if we have new layers
return updatedState;
}
@@ -3651,6 +3670,30 @@ export function setTimeFilterTimelineModeUpdater<S extends VisState>(
return adjustAnimationConfigWithFilter(newState, filterIdx);
}
+/**
+ * Update state of the loading indicator.
+ * @memberof visStateUpdaters
+ * @param state visState
+ * @param action
+ * @param action.payload Payload with change of number of active loading actions.
+ * @returns nextState
+ * @public
+ */
+export const setLoadingIndicatorUpdater = (
+ state: VisState,
+ {payload: {change}}: {payload: SetLoadingIndicatorPayload}
+): VisState => {
+ let {loadingIndicatorValue} = state;
+ if (!loadingIndicatorValue) {
+ loadingIndicatorValue = 0;
+ }
+
+ return {
+ ...state,
+ loadingIndicatorValue: Math.max(loadingIndicatorValue + change, 0)
+ };
+};
+
function adjustAnimationConfigWithFilter<S extends VisState>(state: S, filterIdx: number): S {
const filter = state.filters[filterIdx];
if ((filter as TimeRangeFilter).syncedWithLayerTimeline) {
diff --git a/src/reducers/src/vis-state.ts b/src/reducers/src/vis-state.ts
index f74800f10..c0bd34c27 100644
--- a/src/reducers/src/vis-state.ts
+++ b/src/reducers/src/vis-state.ts
@@ -161,7 +161,9 @@ const actionHandler = {
[ActionTypes.UPDATE_EFFECT]: visStateUpdaters.updateEffectUpdater,
- [ActionTypes.CREATE_NEW_DATASET_SUCCESS]: visStateUpdaters.createNewDatasetSuccessUpdater
+ [ActionTypes.CREATE_NEW_DATASET_SUCCESS]: visStateUpdaters.createNewDatasetSuccessUpdater,
+
+ [ActionTypes.SET_LOADING_INDICATOR]: visStateUpdaters.setLoadingIndicatorUpdater
};
// construct vis-state reducer
diff --git a/src/schemas/src/vis-state-schema.ts b/src/schemas/src/vis-state-schema.ts
index 9307d011d..5ccc68048 100644
--- a/src/schemas/src/vis-state-schema.ts
+++ b/src/schemas/src/vis-state-schema.ts
@@ -75,6 +75,7 @@ export interface VisState {
splitMapsToBeMerged: SplitMap[];
fileLoading: FileLoading | false;
fileLoadingProgress: FileLoadingProgress;
+ loadingIndicatorValue: number;
loaders: Loader[];
loadOptions: object;
initialState?: Partial<VisState>;
diff --git a/test/node/reducers/composer-state-test.js b/test/node/reducers/composer-state-test.js
index f45884576..e4d4431fe 100644
--- a/test/node/reducers/composer-state-test.js
+++ b/test/node/reducers/composer-state-test.js
@@ -127,7 +127,7 @@ test('#composerStateReducer - addDataToMapUpdater: mapState should be centered (
// layers should generate a fit bounds task
const tasks = drainTasksForTesting();
- t.equal(tasks.length, 2, 'Should create one fit bounds task and one setLoadingIndicator task');
+ t.equal(tasks.length, 1, 'Should create one fit bounds task');
newState.mapState = mapStateReducer(newState.mapState, succeedTaskWithValues(tasks[0], {}));
@@ -441,7 +441,7 @@ test('#composerStateReducer - replaceDataInMapUpdater', t => {
// layers should generate a fit bounds task
const tasks = drainTasksForTesting();
- t.equal(tasks.length, 2, 'Should create one fit bounds task and one setLoadingIndicator task');
+ t.equal(tasks.length, 1, 'Should create one fit bounds task');
nextState = {
...nextState,
mapState: mapStateReducer(nextState.mapState, succeedTaskWithValues(tasks[0], {}))
diff --git a/test/node/reducers/vis-state-merger-test.js b/test/node/reducers/vis-state-merger-test.js
index 14918997b..8a0f0b1da 100644
--- a/test/node/reducers/vis-state-merger-test.js
+++ b/test/node/reducers/vis-state-merger-test.js
@@ -2217,7 +2217,7 @@ test('VisStateMerger -> asynс mergers', t => {
t.ok(nextState.test.visState.datasets[testCsvDataId], 'should add csv data');
const tasks = drainTasksForTesting();
- t.equal(tasks.length, 2, 'Should create one fit bounds task and one setLoadingIndicator task');
+ t.equal(tasks.length, 1, 'Should create one fit bounds task');
t.equal(tasks[0].type, 'MOCK_MERGE_TASK', 'should create merger task');
// add another dataset will async merger is in process