Skip to content
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
16 changes: 1 addition & 15 deletions src/legacy/core_plugins/data/public/index_patterns/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,7 @@
*/

import { NotificationsStart } from 'src/core/public';

const createGetterSetter = <T extends object>(name: string): [() => T, (value: T) => void] => {
let value: T;

const get = (): T => {
if (!value) throw new Error(`${name} was not set`);
return value;
};

const set = (newValue: T) => {
value = newValue;
};

return [get, set];
};
import { createGetterSetter } from '../../../../../plugins/kibana_utils/public';

export const [getNotifications, setNotifications] = createGetterSetter<NotificationsStart>(
'Notifications'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,11 @@
* under the License.
*/

import { createGetterSetter } from '../../../../../../plugins/kibana_utils/public';
import { IInterpreter } from './types';
import { Start as IInspector } from '../../../../../../plugins/inspector/public';
import { ExpressionsSetup } from './plugin';

const createGetterSetter = <T extends object>(name: string): [() => T, (value: T) => void] => {
let value: T;

const get = (): T => {
if (!value) throw new Error(`${name} was not set`);
return value;
};

const set = (newValue: T) => {
value = newValue;
};

return [get, set];
};

export const [getInspector, setInspector] = createGetterSetter<IInspector>('Inspector');
export const [getInterpreter, setInterpreter] = createGetterSetter<IInterpreter>('Interpreter');
export const [getRenderersRegistry, setRenderersRegistry] = createGetterSetter<
Expand Down
36 changes: 36 additions & 0 deletions src/plugins/kibana_utils/public/core/create_getter_setter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export type Get<T> = () => T;
export type Set<T> = (value: T) => void;

export const createGetterSetter = <T extends object>(name: string): [Get<T>, Set<T>] => {
let value: T;

const get: Get<T> = () => {
if (!value) throw new Error(`${name} was not set.`);
return value;
};

const set: Set<T> = newValue => {
value = newValue;
};

return [get, set];
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { createKibanaUtilsCore } from './create_kibana_utils_core';
import { CoreStart } from 'kibana/public';

describe('createKibanaUtilsCore', () => {
it('should allows to work with multiple instances', () => {
const core1 = {} as CoreStart;
const core2 = {} as CoreStart;

const { setCoreStart: setCoreStart1, getCoreStart: getCoreStart1 } = createKibanaUtilsCore();
const { setCoreStart: setCoreStart2, getCoreStart: getCoreStart2 } = createKibanaUtilsCore();

setCoreStart1(core1);
setCoreStart2(core2);

expect(getCoreStart1()).toBe(core1);
expect(getCoreStart2()).toBe(core2);

expect(getCoreStart1() !== getCoreStart2()).toBeTruthy();
});
});
39 changes: 39 additions & 0 deletions src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { createGetterSetter, Get, Set } from './create_getter_setter';
import { CoreStart } from '../../../../core/public';
import { KUSavedObjectClient, createSavedObjectsClient } from './saved_objects_client';

interface Return {
getCoreStart: Get<CoreStart>;
setCoreStart: Set<CoreStart>;
savedObjects: KUSavedObjectClient;
}

export const createKibanaUtilsCore = (): Return => {
const [getCoreStart, setCoreStart] = createGetterSetter<CoreStart>('CoreStart');
const savedObjects = createSavedObjectsClient(getCoreStart);

return {
getCoreStart,
setCoreStart,
savedObjects,
};
};
21 changes: 21 additions & 0 deletions src/plugins/kibana_utils/public/core/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

export * from './create_getter_setter';
export * from './create_kibana_utils_core';
35 changes: 35 additions & 0 deletions src/plugins/kibana_utils/public/core/saved_objects_client.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { CoreStart } from '../../../../core/public';
import { Get } from './create_getter_setter';

type CoreSavedObjectClient = CoreStart['savedObjects']['client'];

export interface KUSavedObjectClient {
get: CoreSavedObjectClient['get'];
}

export const createSavedObjectsClient = (getCoreStart: Get<CoreStart>) => {
const savedObjectsClient: KUSavedObjectClient = {
get: (...args) => getCoreStart().savedObjects.client.get(...args),
};

return savedObjectsClient;
};
23 changes: 23 additions & 0 deletions src/plugins/kibana_utils/public/core/state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

import { createGetterSetter } from './create_getter_setter';
import { CoreStart } from '../../../../core/public';

export const [getCoreStart, setCoreStart] = createGetterSetter<CoreStart>('CoreStart');
1 change: 1 addition & 0 deletions src/plugins/kibana_utils/public/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* under the License.
*/

export * from './core';
export * from './store';
export * from './parse';
export * from './render_complete';
Expand Down