diff --git a/src/legacy/core_plugins/data/public/index_patterns/services.ts b/src/legacy/core_plugins/data/public/index_patterns/services.ts index 3782f494e8178..5cc087548d6fb 100644 --- a/src/legacy/core_plugins/data/public/index_patterns/services.ts +++ b/src/legacy/core_plugins/data/public/index_patterns/services.ts @@ -18,21 +18,7 @@ */ import { NotificationsStart } from 'src/core/public'; - -const createGetterSetter = (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( 'Notifications' diff --git a/src/legacy/core_plugins/expressions/public/np_ready/public/services.ts b/src/legacy/core_plugins/expressions/public/np_ready/public/services.ts index 4d95a8a91d0bb..ae2a7955233d1 100644 --- a/src/legacy/core_plugins/expressions/public/np_ready/public/services.ts +++ b/src/legacy/core_plugins/expressions/public/np_ready/public/services.ts @@ -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 = (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('Inspector'); export const [getInterpreter, setInterpreter] = createGetterSetter('Interpreter'); export const [getRenderersRegistry, setRenderersRegistry] = createGetterSetter< diff --git a/src/plugins/kibana_utils/public/core/create_getter_setter.ts b/src/plugins/kibana_utils/public/core/create_getter_setter.ts new file mode 100644 index 0000000000000..be2fd48ee6e7b --- /dev/null +++ b/src/plugins/kibana_utils/public/core/create_getter_setter.ts @@ -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; +export type Set = (value: T) => void; + +export const createGetterSetter = (name: string): [Get, Set] => { + let value: T; + + const get: Get = () => { + if (!value) throw new Error(`${name} was not set.`); + return value; + }; + + const set: Set = newValue => { + value = newValue; + }; + + return [get, set]; +}; diff --git a/src/plugins/kibana_utils/public/core/create_kibana_utils_core.test.ts b/src/plugins/kibana_utils/public/core/create_kibana_utils_core.test.ts new file mode 100644 index 0000000000000..c5b23bdf0055f --- /dev/null +++ b/src/plugins/kibana_utils/public/core/create_kibana_utils_core.test.ts @@ -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(); + }); +}); diff --git a/src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts b/src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts new file mode 100644 index 0000000000000..84ecffa1da634 --- /dev/null +++ b/src/plugins/kibana_utils/public/core/create_kibana_utils_core.ts @@ -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; + setCoreStart: Set; + savedObjects: KUSavedObjectClient; +} + +export const createKibanaUtilsCore = (): Return => { + const [getCoreStart, setCoreStart] = createGetterSetter('CoreStart'); + const savedObjects = createSavedObjectsClient(getCoreStart); + + return { + getCoreStart, + setCoreStart, + savedObjects, + }; +}; diff --git a/src/plugins/kibana_utils/public/core/index.ts b/src/plugins/kibana_utils/public/core/index.ts new file mode 100644 index 0000000000000..7e8dff7191fe8 --- /dev/null +++ b/src/plugins/kibana_utils/public/core/index.ts @@ -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'; diff --git a/src/plugins/kibana_utils/public/core/saved_objects_client.ts b/src/plugins/kibana_utils/public/core/saved_objects_client.ts new file mode 100644 index 0000000000000..40407fea5d189 --- /dev/null +++ b/src/plugins/kibana_utils/public/core/saved_objects_client.ts @@ -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) => { + const savedObjectsClient: KUSavedObjectClient = { + get: (...args) => getCoreStart().savedObjects.client.get(...args), + }; + + return savedObjectsClient; +}; diff --git a/src/plugins/kibana_utils/public/core/state.ts b/src/plugins/kibana_utils/public/core/state.ts new file mode 100644 index 0000000000000..8ac6e4e0e58e6 --- /dev/null +++ b/src/plugins/kibana_utils/public/core/state.ts @@ -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'); diff --git a/src/plugins/kibana_utils/public/index.ts b/src/plugins/kibana_utils/public/index.ts index bac0ef629789a..7cb4d4a34e971 100644 --- a/src/plugins/kibana_utils/public/index.ts +++ b/src/plugins/kibana_utils/public/index.ts @@ -17,6 +17,7 @@ * under the License. */ +export * from './core'; export * from './store'; export * from './parse'; export * from './render_complete';