Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Maps] EMS baselayer should load cleanly #88531

Merged
merged 7 commits into from
Jan 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,18 @@ export type ESGeoLineSourceResponseMeta = {
totalEntities: number;
};

export type VectorTileLayerMeta = {
tileLayerId: string;
};

// Partial because objects are justified downstream in constructors
export type DataMeta = Partial<
VectorSourceRequestMeta &
VectorJoinSourceRequestMeta &
VectorStyleRequestMeta &
ESSearchSourceResponseMeta &
ESGeoLineSourceResponseMeta
ESGeoLineSourceResponseMeta &
VectorTileLayerMeta
>;

type NumericalStyleFieldData = {
Expand Down
4 changes: 2 additions & 2 deletions x-pack/plugins/maps/public/actions/data_request_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ import { IVectorStyle } from '../classes/styles/vector/vector_style';
const FIT_TO_BOUNDS_SCALE_FACTOR = 0.1;

export type DataRequestContext = {
startLoading(dataId: string, requestToken: symbol, meta: DataMeta): void;
stopLoading(dataId: string, requestToken: symbol, data: object, meta?: DataMeta): void;
startLoading(dataId: string, requestToken: symbol, requestMeta: DataMeta): void;
stopLoading(dataId: string, requestToken: symbol, data: object, resultsMeta?: DataMeta): void;
onLoadError(dataId: string, requestToken: symbol, errorMessage: string): void;
updateSourceData(newData: unknown): void;
isRequestStillActive(dataId: string, requestToken: symbol): boolean;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { getEMSSettings } from '../../kibana_services';
// @ts-expect-error
import { KibanaTilemapSource } from '../sources/kibana_tilemap_source';
import { TileLayer } from './tile_layer/tile_layer';
// @ts-expect-error
import { VectorTileLayer } from './vector_tile_layer/vector_tile_layer';
// @ts-expect-error
import { EMSTMSSource } from '../sources/ems_tms_source';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ITileLayerArguments, TileLayer } from '../tile_layer/tile_layer';

export class VectorTileLayer extends TileLayer {
static type: string;
constructor(args: ITileLayerArguments);
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class VectorTileLayer extends TileLayer {
return prevMeta.tileLayerId === nextMeta.tileLayerId;
}

async syncData({ startLoading, stopLoading, onLoadError, dataFilters }) {
async syncData({ startLoading, stopLoading, onLoadError }) {
const nextMeta = { tileLayerId: this.getSource().getTileLayerId() };
const canSkipSync = this._canSkipSync({
prevDataRequest: this.getSourceDataRequest(),
Expand All @@ -56,14 +56,14 @@ export class VectorTileLayer extends TileLayer {

const requestToken = Symbol(`layer-source-refresh:${this.getId()} - source`);
try {
startLoading(SOURCE_DATA_REQUEST_ID, requestToken, dataFilters);
startLoading(SOURCE_DATA_REQUEST_ID, requestToken, nextMeta);
thomasneirynck marked this conversation as resolved.
Show resolved Hide resolved
const styleAndSprites = await this.getSource().getVectorStyleSheetAndSpriteMeta(isRetina());
const spriteSheetImageData = await loadSpriteSheetImageData(styleAndSprites.spriteMeta.png);
const data = {
...styleAndSprites,
spriteSheetImageData,
};
stopLoading(SOURCE_DATA_REQUEST_ID, requestToken, data, nextMeta);
stopLoading(SOURCE_DATA_REQUEST_ID, requestToken, data);
} catch (error) {
onLoadError(SOURCE_DATA_REQUEST_ID, requestToken, error.message);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/

import { ITileLayerArguments } from '../tile_layer/tile_layer';
import { SOURCE_TYPES } from '../../../../common/constants';
import { MapFilters, XYZTMSSourceDescriptor } from '../../../../common/descriptor_types';
import { ITMSSource, AbstractTMSSource } from '../../sources/tms_source';
import { ILayer } from '../layer';
import { VectorTileLayer } from './vector_tile_layer';
import { DataRequestContext } from '../../../actions';

const sourceDescriptor: XYZTMSSourceDescriptor = {
type: SOURCE_TYPES.EMS_XYZ,
urlTemplate: 'https://example.com/{x}/{y}/{z}.png',
id: 'mockSourceId',
};

class MockTileSource extends AbstractTMSSource implements ITMSSource {
readonly _descriptor: XYZTMSSourceDescriptor;
constructor(descriptor: XYZTMSSourceDescriptor) {
super(descriptor, {});
this._descriptor = descriptor;
}

async getDisplayName(): Promise<string> {
return this._descriptor.urlTemplate;
}

async getUrlTemplate(): Promise<string> {
return 'template/{x}/{y}/{z}.png';
}

getTileLayerId() {
return this._descriptor.id;
}

getVectorStyleSheetAndSpriteMeta() {
throw new Error('network error');
}
}

describe('VectorTileLayer', () => {
it('should correctly inject tileLayerId in meta', async () => {
const source = new MockTileSource(sourceDescriptor);

const args: ITileLayerArguments = {
source,
layerDescriptor: { id: 'layerid', sourceDescriptor },
};

const layer: ILayer = new VectorTileLayer(args);

let actualMeta;
let actualErrorMessage;
const mockContext = ({
startLoading: (requestId: string, token: string, meta: unknown) => {
actualMeta = meta;
},
onLoadError: (requestId: string, token: string, message: string) => {
actualErrorMessage = message;
},
dataFilters: ({ foo: 'bar' } as unknown) as MapFilters,
} as unknown) as DataRequestContext;

await layer.syncData(mockContext);

expect(actualMeta).toStrictEqual({ tileLayerId: 'mockSourceId' });
expect(actualErrorMessage).toStrictEqual('network error');
});
});
2 changes: 1 addition & 1 deletion x-pack/plugins/maps/public/selectors/map_selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ export function createLayerInstance(
joins,
});
case VectorTileLayer.type:
return new VectorTileLayer({ layerDescriptor, source });
return new VectorTileLayer({ layerDescriptor, source: source as ITMSSource });
case HeatmapLayer.type:
return new HeatmapLayer({ layerDescriptor, source });
case BlendedVectorLayer.type:
Expand Down