[Maps] data load event handlers (#49373)

* [Maps] data load event handlers

* update readme featuresCount to reflect variable name

* fix exception with EMS_TMS onDataLoadEnd

* wrap featuresCount in resultMeta object so different layer types can return different results
This commit is contained in:
Nathan Reese 2019-10-31 11:05:29 -06:00 committed by GitHub
parent 6f464ad0d2
commit 70c05830ab
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 93 additions and 8 deletions

View file

@ -21,10 +21,15 @@ import { FLYOUT_STATE } from '../reducers/ui';
import {
cancelRequest,
registerCancelCallback,
unregisterCancelCallback
unregisterCancelCallback,
getEventHandlers,
} from '../reducers/non_serializable_instances';
import { updateFlyout } from '../actions/ui_actions';
import { FEATURE_ID_PROPERTY_NAME, SOURCE_DATA_ID_ORIGIN } from '../../common/constants';
import {
FEATURE_ID_PROPERTY_NAME,
LAYER_TYPE,
SOURCE_DATA_ID_ORIGIN
} from '../../common/constants';
export const SET_SELECTED_LAYER = 'SET_SELECTED_LAYER';
export const SET_TRANSIENT_LAYER = 'SET_TRANSIENT_LAYER';
@ -456,6 +461,14 @@ export function startDataLoad(layerId, dataId, requestToken, meta = {}) {
dispatch(cancelRequest(layer.getPrevRequestToken(dataId)));
}
const eventHandlers = getEventHandlers(getState());
if (eventHandlers && eventHandlers.onDataLoad) {
eventHandlers.onDataLoad({
layerId,
dataId,
});
}
dispatch({
meta,
type: LAYER_DATA_LOAD_STARTED,
@ -480,9 +493,26 @@ export function updateSourceDataRequest(layerId, newData) {
}
export function endDataLoad(layerId, dataId, requestToken, data, meta) {
return async (dispatch) => {
return async (dispatch, getState) => {
dispatch(unregisterCancelCallback(requestToken));
const features = data ? data.features : [];
const features = data && data.features ? data.features : [];
const eventHandlers = getEventHandlers(getState());
if (eventHandlers && eventHandlers.onDataLoadEnd) {
const layer = getLayerById(layerId, getState());
const resultMeta = {};
if (layer && layer.getType() === LAYER_TYPE.VECTOR) {
resultMeta.featuresCount = features.length;
}
eventHandlers.onDataLoadEnd({
layerId,
dataId,
resultMeta
});
}
dispatch(cleanTooltipStateForLayer(layerId, features));
dispatch({
type: LAYER_DATA_LOAD_ENDED,
@ -503,8 +533,18 @@ export function endDataLoad(layerId, dataId, requestToken, data, meta) {
}
export function onDataLoadError(layerId, dataId, requestToken, errorMessage) {
return async (dispatch) => {
return async (dispatch, getState) => {
dispatch(unregisterCancelCallback(requestToken));
const eventHandlers = getEventHandlers(getState());
if (eventHandlers && eventHandlers.onDataLoadError) {
eventHandlers.onDataLoadError({
layerId,
dataId,
errorMessage,
});
}
dispatch(cleanTooltipStateForLayer(layerId));
dispatch({
type: LAYER_DATA_LOAD_ERROR,

View file

@ -58,3 +58,21 @@ const renderTooltipContent = ({ addFilters, closeTooltip, features, isLocked, lo
const mapEmbeddable = await factory.createFromState(state, input, parent, renderTooltipContent);
```
#### Event handlers
```
const eventHandlers = {
onDataLoad: (layerId: string, dataId: string) => {
// take action on data load
},
onDataLoadEnd: (layerId: string, dataId: string, resultMeta: object) => {
// take action on data load end
},
onDataLoadError: (layerId: string, dataId: string, errorMessage: string) => {
// take action on data load error
},
}
const mapEmbeddable = await factory.createFromState(state, input, parent, renderTooltipContent, eventHandlers);
```

View file

@ -31,14 +31,14 @@ import {
setOpenTOCDetails,
} from '../actions/ui_actions';
import { getIsLayerTOCOpen, getOpenTOCDetails } from '../selectors/ui_selectors';
import { getInspectorAdapters } from '../reducers/non_serializable_instances';
import { getInspectorAdapters, setEventHandlers } from '../reducers/non_serializable_instances';
import { getMapCenter, getMapZoom } from '../selectors/map_selectors';
import { MAP_SAVED_OBJECT_TYPE } from '../../common/constants';
export class MapEmbeddable extends Embeddable {
type = MAP_SAVED_OBJECT_TYPE;
constructor(config, initialInput, parent, renderTooltipContent) {
constructor(config, initialInput, parent, renderTooltipContent, eventHandlers) {
super(
initialInput,
{
@ -50,6 +50,7 @@ export class MapEmbeddable extends Embeddable {
parent);
this._renderTooltipContent = renderTooltipContent;
this._eventHandlers = eventHandlers;
this._layerList = config.layerList;
this._store = createMapStore();
@ -98,6 +99,7 @@ export class MapEmbeddable extends Embeddable {
* @param {ContainerState} containerState
*/
render(domNode) {
this._store.dispatch(setEventHandlers(this._eventHandlers));
this._store.dispatch(setReadOnly(true));
this._store.dispatch(disableScrollZoom());

View file

@ -123,6 +123,7 @@ export class MapEmbeddableFactory extends EmbeddableFactory {
input,
parent,
renderTooltipContent,
eventHandlers,
) {
const layerList = state && state.layerList ? state.layerList : getInitialLayers();
const indexPatterns = await this._getIndexPatterns(layerList);
@ -137,7 +138,8 @@ export class MapEmbeddableFactory extends EmbeddableFactory {
},
input,
parent,
renderTooltipContent
renderTooltipContent,
eventHandlers,
);
}

View file

@ -406,5 +406,9 @@ export class AbstractLayer {
mbMap.setLayoutProperty(mbLayerId, 'visibility', this.isVisible() ? 'visible' : 'none');
}
getType() {
return this._descriptor.type;
}
}

View file

@ -10,6 +10,7 @@ import { MapAdapter } from '../inspector/adapters/map_adapter';
const REGISTER_CANCEL_CALLBACK = 'REGISTER_CANCEL_CALLBACK';
const UNREGISTER_CANCEL_CALLBACK = 'UNREGISTER_CANCEL_CALLBACK';
const SET_EVENT_HANDLERS = 'SET_EVENT_HANDLERS';
function createInspectorAdapters() {
const inspectorAdapters = {
@ -27,6 +28,7 @@ export function nonSerializableInstances(state, action = {}) {
return {
inspectorAdapters: createInspectorAdapters(),
cancelRequestCallbacks: new Map(), // key is request token, value is cancel callback
eventHandlers: {},
};
}
@ -41,6 +43,12 @@ export function nonSerializableInstances(state, action = {}) {
return {
...state,
};
case SET_EVENT_HANDLERS: {
return {
...state,
eventHandlers: action.eventHandlers
};
}
default:
return state;
@ -57,6 +65,10 @@ export const getCancelRequestCallbacks = ({ nonSerializableInstances }) => {
return nonSerializableInstances.cancelRequestCallbacks;
};
export const getEventHandlers = ({ nonSerializableInstances }) => {
return nonSerializableInstances.eventHandlers;
};
// Actions
export const registerCancelCallback = (requestToken, callback) => {
return {
@ -86,3 +98,10 @@ export const cancelRequest = (requestToken) => {
}
};
};
export const setEventHandlers = (eventHandlers = {}) => {
return {
type: SET_EVENT_HANDLERS,
eventHandlers,
};
};