Skip to content

Commit

Permalink
refactor to only reference uiSettings after lifecycle functions have run
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkime committed Jun 16, 2021
1 parent cfb8417 commit d74614a
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 70 deletions.
8 changes: 3 additions & 5 deletions src/plugins/index_pattern_management/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,15 @@ export class IndexPatternManagementPlugin
return mountManagementSection(core.getStartServices, params);
},
});
}

return this.indexPatternManagementService.setup({
public start(core: CoreStart, plugins: IndexPatternManagementStartDependencies) {
return this.indexPatternManagementService.start({
httpClient: core.http,
uiSettings: core.uiSettings,
});
}

public start(core: CoreStart, plugins: IndexPatternManagementStartDependencies) {
return this.indexPatternManagementService.start();
}

public stop() {
this.indexPatternManagementService.stop();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,36 @@
* Side Public License, v 1.
*/

import { HttpSetup } from '../../../../../core/public';
import { memoize } from 'lodash';
import { HttpStart, CoreStart } from '../../../../../core/public';
import { IndexPatternCreationConfig, UrlHandler, IndexPatternCreationOption } from './config';
import { CONFIG_ROLLUPS } from '../../constants';
// @ts-ignore
import { RollupIndexPatternCreationConfig } from './rollup_creation_config';

export class IndexPatternCreationManager {
private configs: IndexPatternCreationConfig[] = [];
interface IndexPatternCreationManagerStart {
httpClient: HttpStart;
uiSettings: CoreStart['uiSettings'];
}

setup(httpClient: HttpSetup) {
return {
addCreationConfig: (Config: typeof IndexPatternCreationConfig) => {
const config = new Config({ httpClient });
export class IndexPatternCreationManager {
start({ httpClient, uiSettings }: IndexPatternCreationManagerStart) {
const getConfigs = memoize(() => {
const configs: IndexPatternCreationConfig[] = [];
configs.push(new IndexPatternCreationConfig({ httpClient }));

if (this.configs.findIndex((c) => c.key === config.key) !== -1) {
throw new Error(`${config.key} exists in IndexPatternCreationManager.`);
}
if (uiSettings.get(CONFIG_ROLLUPS, false)) {
configs.push(new RollupIndexPatternCreationConfig({ httpClient }));
}

this.configs.push(config);
},
};
}
return configs;
});

start() {
const getType = (key: string | undefined): IndexPatternCreationConfig => {
const configs = getConfigs();
if (key) {
const index = this.configs.findIndex((config) => config.key === key);
const config = this.configs[index];
const index = configs.findIndex((config) => config.key === key);
const config = configs[index];

if (config) {
return config;
Expand All @@ -48,7 +53,7 @@ export class IndexPatternCreationManager {
const options: IndexPatternCreationOption[] = [];

await Promise.all(
this.configs.map(async (config) => {
getConfigs().map(async (config) => {
const option = config.getIndexPatternCreationOption
? await config.getIndexPatternCreationOption(urlHandler)
: null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,13 @@
* Side Public License, v 1.
*/

import { HttpSetup, CoreSetup } from '../../../../core/public';
import {
IndexPatternCreationManager,
IndexPatternCreationConfig,
RollupIndexPatternCreationConfig,
} from './creation';
import {
IndexPatternListManager,
IndexPatternListConfig,
RollupIndexPatternListConfig,
} from './list';
import { HttpStart, CoreStart } from '../../../../core/public';
import { IndexPatternCreationManager } from './creation';
import { IndexPatternListManager } from './list';

import { CONFIG_ROLLUPS } from '../constants';
interface SetupDependencies {
httpClient: HttpSetup;
uiSettings: CoreSetup['uiSettings'];
interface StartDependencies {
httpClient: HttpStart;
uiSettings: CoreStart['uiSettings'];
}

/**
Expand All @@ -38,23 +29,12 @@ export class IndexPatternManagementService {
this.indexPatternListConfig = new IndexPatternListManager();
}

public setup({ httpClient, uiSettings }: SetupDependencies) {
const creationManagerSetup = this.indexPatternCreationManager.setup(httpClient);
creationManagerSetup.addCreationConfig(IndexPatternCreationConfig);
public setup() {}

const indexPatternListConfigSetup = this.indexPatternListConfig.setup();
indexPatternListConfigSetup.addListConfig(IndexPatternListConfig);

if (uiSettings.get(CONFIG_ROLLUPS, false)) {
creationManagerSetup.addCreationConfig(RollupIndexPatternCreationConfig);
indexPatternListConfigSetup.addListConfig(RollupIndexPatternListConfig);
}
}

public start() {
public start({ httpClient, uiSettings }: StartDependencies) {
return {
creation: this.indexPatternCreationManager.start(),
list: this.indexPatternListConfig.start(),
creation: this.indexPatternCreationManager.start({ httpClient, uiSettings }),
list: this.indexPatternListConfig.start({ uiSettings }),
};
}

Expand Down
38 changes: 21 additions & 17 deletions src/plugins/index_pattern_management/public/service/list/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,31 +8,35 @@

import { IIndexPattern, IFieldType } from 'src/plugins/data/public';
import { SimpleSavedObject } from 'src/core/public';
import { memoize } from 'lodash';
import { CoreStart } from '../../../../../core/public';
import { IndexPatternListConfig, IndexPatternTag } from './config';
import { CONFIG_ROLLUPS } from '../../constants';
// @ts-ignore
import { RollupIndexPatternListConfig } from './rollup_list_config';

export class IndexPatternListManager {
private configs: IndexPatternListConfig[] = [];
interface IndexPatternListManagerStart {
uiSettings: CoreStart['uiSettings'];
}

setup() {
return {
addListConfig: (Config: typeof IndexPatternListConfig) => {
const config = new Config();
export class IndexPatternListManager {
start({ uiSettings }: IndexPatternListManagerStart) {
const getConfigs = memoize(() => {
const configs: IndexPatternListConfig[] = [];
configs.push(new IndexPatternListConfig());

if (this.configs.findIndex((c) => c.key === config.key) !== -1) {
throw new Error(`${config.key} exists in IndexPatternListManager.`);
}
this.configs.push(config);
},
};
}
if (uiSettings.get(CONFIG_ROLLUPS, false)) {
configs.push(new RollupIndexPatternListConfig());
}

start() {
return configs;
});
return {
getIndexPatternTags: (
indexPattern: IIndexPattern | SimpleSavedObject<IIndexPattern>,
isDefault: boolean
) =>
this.configs.reduce(
getConfigs().reduce(
(tags: IndexPatternTag[], config) =>
config.getIndexPatternTags
? tags.concat(config.getIndexPatternTags(indexPattern, isDefault))
Expand All @@ -41,14 +45,14 @@ export class IndexPatternListManager {
),

getFieldInfo: (indexPattern: IIndexPattern, field: IFieldType): string[] =>
this.configs.reduce(
getConfigs().reduce(
(info: string[], config) =>
config.getFieldInfo ? info.concat(config.getFieldInfo(indexPattern, field)) : info,
[]
),

areScriptedFieldsEnabled: (indexPattern: IIndexPattern): boolean =>
this.configs.every((config) =>
getConfigs().every((config) =>
config.areScriptedFieldsEnabled ? config.areScriptedFieldsEnabled(indexPattern) : true
),
};
Expand Down

0 comments on commit d74614a

Please sign in to comment.