Skip to content

Commit

Permalink
[Maps] Extract lazy load bundle from map embeddable factory (#68928)
Browse files Browse the repository at this point in the history
* extract lazy load bundle from map embeddables

* tslint

* update imports, path changed

* use correct import paths
  • Loading branch information
nreese authored Jun 11, 2020
1 parent 2d1312f commit e09c0c5
Show file tree
Hide file tree
Showing 6 changed files with 109 additions and 79 deletions.
13 changes: 2 additions & 11 deletions x-pack/plugins/maps/public/embeddable/map_embeddable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,10 @@ import 'mapbox-gl/dist/mapbox-gl.css';
import { Subscription } from 'rxjs';
import { Unsubscribe } from 'redux';
import { EuiLoadingSpinner } from '@elastic/eui';
import {
Embeddable,
IContainer,
EmbeddableOutput,
} from '../../../../../src/plugins/embeddable/public';
import { Embeddable, IContainer } from '../../../../../src/plugins/embeddable/public';
import { APPLY_FILTER_TRIGGER } from '../../../../../src/plugins/ui_actions/public';
import {
esFilters,
IIndexPattern,
TimeRange,
Filter,
Query,
Expand Down Expand Up @@ -57,13 +52,9 @@ import { RenderToolTipContent } from '../classes/tooltips/tooltip_property';
import { getUiActions, getCoreI18n } from '../kibana_services';
import { LayerDescriptor } from '../../common/descriptor_types';

import { MapEmbeddableInput, MapEmbeddableConfig } from './types';
import { MapEmbeddableConfig, MapEmbeddableInput, MapEmbeddableOutput } from './types';
export { MapEmbeddableInput, MapEmbeddableConfig };

export interface MapEmbeddableOutput extends EmbeddableOutput {
indexPatterns: IIndexPattern[];
}

const GisMap = lazy(() => import('../connected_components/gis_map'));
export class MapEmbeddable extends Embeddable<MapEmbeddableInput, MapEmbeddableOutput> {
type = MAP_SAVED_OBJECT_TYPE;
Expand Down
77 changes: 17 additions & 60 deletions x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,71 +6,16 @@

import _ from 'lodash';
import { i18n } from '@kbn/i18n';
import { AnyAction } from 'redux';
import { IIndexPattern } from 'src/plugins/data/public';
import {
Embeddable,
EmbeddableFactoryDefinition,
IContainer,
} from '../../../../../src/plugins/embeddable/public';
import '../index.scss';
import { createMapPath, MAP_SAVED_OBJECT_TYPE, APP_ICON } from '../../common/constants';
import { LayerDescriptor } from '../../common/descriptor_types';
import { MapStore, MapStoreState } from '../reducers/store';
import { MapEmbeddableConfig, MapEmbeddableInput } from './types';
import { MapEmbeddableOutput } from './map_embeddable';
import { RenderToolTipContent } from '../classes/tooltips/tooltip_property';
import { EventHandlers } from '../reducers/non_serializable_instances';

let whenModulesLoadedPromise: Promise<boolean>;

let getMapsSavedObjectLoader: any;
let MapEmbeddable: new (
config: MapEmbeddableConfig,
initialInput: MapEmbeddableInput,
parent?: IContainer,
renderTooltipContent?: RenderToolTipContent,
eventHandlers?: EventHandlers
) => Embeddable<MapEmbeddableInput, MapEmbeddableOutput>;

let getIndexPatternService: () => {
get: (id: string) => IIndexPattern | undefined;
};
let getHttp: () => any;
let getMapsCapabilities: () => any;
let createMapStore: () => MapStore;
let addLayerWithoutDataSync: (layerDescriptor: LayerDescriptor) => AnyAction;
let getQueryableUniqueIndexPatternIds: (state: MapStoreState) => string[];
let getInitialLayers: (
layerListJSON?: string,
initialLayers?: LayerDescriptor[]
) => LayerDescriptor[];
let mergeInputWithSavedMap: any;

async function waitForMapDependencies(): Promise<boolean> {
if (typeof whenModulesLoadedPromise !== 'undefined') {
return whenModulesLoadedPromise;
}

whenModulesLoadedPromise = new Promise(async (resolve) => {
({
// @ts-ignore
getMapsSavedObjectLoader,
getQueryableUniqueIndexPatternIds,
MapEmbeddable,
getIndexPatternService,
getHttp,
getMapsCapabilities,
createMapStore,
addLayerWithoutDataSync,
getInitialLayers,
mergeInputWithSavedMap,
} = await import('./lazy'));

resolve(true);
});
return whenModulesLoadedPromise;
}
import { MapEmbeddableInput } from './types';
import { lazyLoadMapModules } from '../lazy_load_bundle';

export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {
type = MAP_SAVED_OBJECT_TYPE;
Expand All @@ -83,7 +28,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {
};

async isEditable() {
await waitForMapDependencies();
const { getMapsCapabilities } = await lazyLoadMapModules();
return getMapsCapabilities().save as boolean;
}

Expand All @@ -100,6 +45,12 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {

async _getIndexPatterns(layerList: LayerDescriptor[]): Promise<IIndexPattern[]> {
// Need to extract layerList from store to get queryable index pattern ids
const {
addLayerWithoutDataSync,
createMapStore,
getIndexPatternService,
getQueryableUniqueIndexPatternIds,
} = await lazyLoadMapModules();
const store = createMapStore();
let queryableIndexPatternIds: string[];
try {
Expand Down Expand Up @@ -130,6 +81,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {
}

async _fetchSavedMap(savedObjectId: string) {
const { getMapsSavedObjectLoader } = await lazyLoadMapModules();
const savedObjectLoader = getMapsSavedObjectLoader();
return await savedObjectLoader.get(savedObjectId);
}
Expand All @@ -139,7 +91,12 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {
input: MapEmbeddableInput,
parent?: IContainer
) => {
await waitForMapDependencies();
const {
getInitialLayers,
getHttp,
MapEmbeddable,
mergeInputWithSavedMap,
} = await lazyLoadMapModules();
const savedMap = await this._fetchSavedMap(savedObjectId);
const layerList = getInitialLayers(savedMap.layerListJSON);
const indexPatterns = await this._getIndexPatterns(layerList);
Expand Down Expand Up @@ -179,7 +136,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition {
};

create = async (input: MapEmbeddableInput, parent?: IContainer) => {
await waitForMapDependencies();
const { getInitialLayers, MapEmbeddable } = await lazyLoadMapModules();
const layerList = getInitialLayers();
const indexPatterns = await this._getIndexPatterns(layerList);

Expand Down
11 changes: 9 additions & 2 deletions x-pack/plugins/maps/public/embeddable/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

import { IIndexPattern } from '../../../../../src/plugins/data/common/index_patterns';
import { MapSettings } from '../reducers/map';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { EmbeddableInput } from '../../../../../src/plugins/embeddable/public/lib/embeddables';
import {
EmbeddableInput,
EmbeddableOutput,
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
} from '../../../../../src/plugins/embeddable/public/lib/embeddables';
import { Filter, Query, RefreshInterval, TimeRange } from '../../../../../src/plugins/data/common';
import { LayerDescriptor, MapCenterAndZoom } from '../../common/descriptor_types';

Expand Down Expand Up @@ -36,3 +39,7 @@ export interface MapEmbeddableInput extends EmbeddableInput {
hiddenLayers?: string[];
hideFilterActions?: boolean;
}

export interface MapEmbeddableOutput extends EmbeddableOutput {
indexPatterns: IIndexPattern[];
}
8 changes: 4 additions & 4 deletions x-pack/plugins/maps/public/kibana_services.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
import { IIndexPattern, DataPublicPluginStart } from 'src/plugins/data/public';
import { DataPublicPluginStart } from 'src/plugins/data/public';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { IndexPatternsService } from 'src/plugins/data/public/index_patterns';
import { MapsConfigType } from '../config';
import { MapsLegacyConfigType } from '../../../../src/plugins/maps_legacy/public';

Expand All @@ -14,9 +16,7 @@ export function getIndexPatternSelectComponent(): any;
export function getHttp(): any;
export function getTimeFilter(): any;
export function getToasts(): any;
export function getIndexPatternService(): {
get: (id: string) => IIndexPattern | undefined;
};
export function getIndexPatternService(): IndexPatternsService;
export function getAutocompleteService(): any;
export function getSavedObjectsClient(): any;
export function getMapsCapabilities(): any;
Expand Down
75 changes: 75 additions & 0 deletions x-pack/plugins/maps/public/lazy_load_bundle/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
/*
* 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 { AnyAction } from 'redux';
// eslint-disable-next-line @kbn/eslint/no-restricted-paths
import { IndexPatternsService } from 'src/plugins/data/public/index_patterns';
import { Embeddable, IContainer } from '../../../../../src/plugins/embeddable/public';
import { LayerDescriptor } from '../../common/descriptor_types';
import { MapStore, MapStoreState } from '../reducers/store';
import { EventHandlers } from '../reducers/non_serializable_instances';
import { RenderToolTipContent } from '../classes/tooltips/tooltip_property';
import { MapEmbeddableConfig, MapEmbeddableInput, MapEmbeddableOutput } from '../embeddable/types';

let loadModulesPromise: Promise<LazyLoadedMapModules>;

interface LazyLoadedMapModules {
getMapsSavedObjectLoader: any;
MapEmbeddable: new (
config: MapEmbeddableConfig,
initialInput: MapEmbeddableInput,
parent?: IContainer,
renderTooltipContent?: RenderToolTipContent,
eventHandlers?: EventHandlers
) => Embeddable<MapEmbeddableInput, MapEmbeddableOutput>;
getIndexPatternService: () => IndexPatternsService;
getHttp: () => any;
getMapsCapabilities: () => any;
createMapStore: () => MapStore;
addLayerWithoutDataSync: (layerDescriptor: LayerDescriptor) => AnyAction;
getQueryableUniqueIndexPatternIds: (state: MapStoreState) => string[];
getInitialLayers: (
layerListJSON?: string,
initialLayers?: LayerDescriptor[]
) => LayerDescriptor[];
mergeInputWithSavedMap: any;
}

export async function lazyLoadMapModules(): Promise<LazyLoadedMapModules> {
if (typeof loadModulesPromise !== 'undefined') {
return loadModulesPromise;
}

loadModulesPromise = new Promise(async (resolve) => {
const {
// @ts-ignore
getMapsSavedObjectLoader,
getQueryableUniqueIndexPatternIds,
MapEmbeddable,
getIndexPatternService,
getHttp,
getMapsCapabilities,
createMapStore,
addLayerWithoutDataSync,
getInitialLayers,
mergeInputWithSavedMap,
} = await import('./lazy');

resolve({
getMapsSavedObjectLoader,
getQueryableUniqueIndexPatternIds,
MapEmbeddable,
getIndexPatternService,
getHttp,
getMapsCapabilities,
createMapStore,
addLayerWithoutDataSync,
getInitialLayers,
mergeInputWithSavedMap,
});
});
return loadModulesPromise;
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@

// @ts-ignore
export * from '../../angular/services/gis_map_saved_object_loader';
export * from '../map_embeddable';
export * from '../../embeddable/map_embeddable';
export * from '../../kibana_services';
export * from '../../reducers/store';
export * from '../../actions';
export * from '../../selectors/map_selectors';
export * from '../../angular/get_initial_layers';
export * from './../merge_input_with_saved_map';
export * from '../../embeddable/merge_input_with_saved_map';

0 comments on commit e09c0c5

Please sign in to comment.