diff --git a/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx b/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx index 2da3205730638..b27f66ea085de 100644 --- a/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx +++ b/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx @@ -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, @@ -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 { type = MAP_SAVED_OBJECT_TYPE; diff --git a/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts b/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts index c6103bcc73fc5..c73225fc4285b 100644 --- a/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts +++ b/x-pack/plugins/maps/public/embeddable/map_embeddable_factory.ts @@ -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; - -let getMapsSavedObjectLoader: any; -let MapEmbeddable: new ( - config: MapEmbeddableConfig, - initialInput: MapEmbeddableInput, - parent?: IContainer, - renderTooltipContent?: RenderToolTipContent, - eventHandlers?: EventHandlers -) => Embeddable; - -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 { - 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; @@ -83,7 +28,7 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition { }; async isEditable() { - await waitForMapDependencies(); + const { getMapsCapabilities } = await lazyLoadMapModules(); return getMapsCapabilities().save as boolean; } @@ -100,6 +45,12 @@ export class MapEmbeddableFactory implements EmbeddableFactoryDefinition { async _getIndexPatterns(layerList: LayerDescriptor[]): Promise { // 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 { @@ -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); } @@ -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); @@ -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); diff --git a/x-pack/plugins/maps/public/embeddable/types.ts b/x-pack/plugins/maps/public/embeddable/types.ts index fc614a4a6040c..9120f891453de 100644 --- a/x-pack/plugins/maps/public/embeddable/types.ts +++ b/x-pack/plugins/maps/public/embeddable/types.ts @@ -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'; @@ -36,3 +39,7 @@ export interface MapEmbeddableInput extends EmbeddableInput { hiddenLayers?: string[]; hideFilterActions?: boolean; } + +export interface MapEmbeddableOutput extends EmbeddableOutput { + indexPatterns: IIndexPattern[]; +} diff --git a/x-pack/plugins/maps/public/kibana_services.d.ts b/x-pack/plugins/maps/public/kibana_services.d.ts index d45d7286ebf68..8fa52500fb16e 100644 --- a/x-pack/plugins/maps/public/kibana_services.d.ts +++ b/x-pack/plugins/maps/public/kibana_services.d.ts @@ -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'; @@ -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; diff --git a/x-pack/plugins/maps/public/lazy_load_bundle/index.ts b/x-pack/plugins/maps/public/lazy_load_bundle/index.ts new file mode 100644 index 0000000000000..92cefd76aa047 --- /dev/null +++ b/x-pack/plugins/maps/public/lazy_load_bundle/index.ts @@ -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; + +interface LazyLoadedMapModules { + getMapsSavedObjectLoader: any; + MapEmbeddable: new ( + config: MapEmbeddableConfig, + initialInput: MapEmbeddableInput, + parent?: IContainer, + renderTooltipContent?: RenderToolTipContent, + eventHandlers?: EventHandlers + ) => Embeddable; + 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 { + 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; +} diff --git a/x-pack/plugins/maps/public/embeddable/lazy/index.ts b/x-pack/plugins/maps/public/lazy_load_bundle/lazy/index.ts similarity index 86% rename from x-pack/plugins/maps/public/embeddable/lazy/index.ts rename to x-pack/plugins/maps/public/lazy_load_bundle/lazy/index.ts index 80a50061102ad..b650678b3105c 100644 --- a/x-pack/plugins/maps/public/embeddable/lazy/index.ts +++ b/x-pack/plugins/maps/public/lazy_load_bundle/lazy/index.ts @@ -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';