From e09c0c52a329cddfd31dc13de03ed08b0bcb076b Mon Sep 17 00:00:00 2001 From: Nathan Reese Date: Thu, 11 Jun 2020 15:20:15 -0600 Subject: [PATCH] [Maps] Extract lazy load bundle from map embeddable factory (#68928) * extract lazy load bundle from map embeddables * tslint * update imports, path changed * use correct import paths --- .../maps/public/embeddable/map_embeddable.tsx | 13 +--- .../embeddable/map_embeddable_factory.ts | 77 ++++--------------- .../plugins/maps/public/embeddable/types.ts | 11 ++- .../plugins/maps/public/kibana_services.d.ts | 8 +- .../maps/public/lazy_load_bundle/index.ts | 75 ++++++++++++++++++ .../lazy/index.ts | 4 +- 6 files changed, 109 insertions(+), 79 deletions(-) create mode 100644 x-pack/plugins/maps/public/lazy_load_bundle/index.ts rename x-pack/plugins/maps/public/{embeddable => lazy_load_bundle}/lazy/index.ts (86%) diff --git a/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx b/x-pack/plugins/maps/public/embeddable/map_embeddable.tsx index 2da320573063..b27f66ea085d 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 c6103bcc73fc..c73225fc4285 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 fc614a4a6040..9120f891453d 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 d45d7286ebf6..8fa52500fb16 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 000000000000..92cefd76aa04 --- /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 80a50061102a..b650678b3105 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';