From 9ff5def71be191a45c2d78669c5c18991404bda2 Mon Sep 17 00:00:00 2001 From: Clint Andrew Hall Date: Sun, 1 Aug 2021 17:50:52 -0400 Subject: [PATCH] [canvas] Create Expressions Service; remove legacy service --- .../functions/common/render.ts | 9 ++- x-pack/plugins/canvas/public/application.tsx | 22 ++++--- ...element_content.js => element_content.tsx} | 64 ++++++++----------- .../components/element_content/index.js | 32 ---------- .../components/element_content/index.tsx | 27 ++++++++ ...ement_type.js => invalid_element_type.tsx} | 7 +- ...d_expression.js => invalid_expression.tsx} | 6 +- .../public/components/expression/index.tsx | 6 +- .../canvas/public/lib/run_interpreter.ts | 15 ++--- x-pack/plugins/canvas/public/plugin.tsx | 8 ++- .../canvas/public/services/expressions.ts | 10 +++ .../plugins/canvas/public/services/index.ts | 12 ++-- .../public/services/kibana/expressions.ts | 19 ++++++ .../canvas/public/services/kibana/index.ts | 13 ++-- .../canvas/public/services/legacy/context.tsx | 3 - .../canvas/public/services/legacy/index.ts | 11 +--- .../services/legacy/stubs/expressions.ts | 25 -------- .../public/services/legacy/stubs/index.ts | 2 - .../public/services/stubs/expressions.ts | 29 +++++++++ .../canvas/public/services/stubs/index.ts | 9 ++- .../expressions.ts => setup_expressions.ts} | 26 ++++---- 21 files changed, 195 insertions(+), 160 deletions(-) rename x-pack/plugins/canvas/public/components/element_content/{element_content.js => element_content.tsx} (58%) delete mode 100644 x-pack/plugins/canvas/public/components/element_content/index.js create mode 100644 x-pack/plugins/canvas/public/components/element_content/index.tsx rename x-pack/plugins/canvas/public/components/element_content/{invalid_element_type.js => invalid_element_type.tsx} (89%) rename x-pack/plugins/canvas/public/components/element_content/{invalid_expression.js => invalid_expression.tsx} (86%) create mode 100644 x-pack/plugins/canvas/public/services/expressions.ts create mode 100644 x-pack/plugins/canvas/public/services/kibana/expressions.ts delete mode 100644 x-pack/plugins/canvas/public/services/legacy/stubs/expressions.ts create mode 100644 x-pack/plugins/canvas/public/services/stubs/expressions.ts rename x-pack/plugins/canvas/public/{services/legacy/expressions.ts => setup_expressions.ts} (75%) diff --git a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/render.ts b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/render.ts index 7a52833693cc6..df5f8233d462f 100644 --- a/x-pack/plugins/canvas/canvas_plugin_src/functions/common/render.ts +++ b/x-pack/plugins/canvas/canvas_plugin_src/functions/common/render.ts @@ -6,7 +6,7 @@ */ import { ExpressionFunctionDefinition } from 'src/plugins/expressions/common'; -import { Render, ContainerStyle } from '../../../types'; +import { ExpressionValueRender, ContainerStyle } from '../../../types'; import { getFunctionHelp } from '../../../i18n'; import { DEFAULT_ELEMENT_CSS } from '../../../common/lib/constants'; @@ -19,11 +19,14 @@ interface Arguments { css: string; containerStyle: ContainerStyleArgument; } + +export type Renderable = ExpressionValueRender & Arguments; + export function render(): ExpressionFunctionDefinition< 'render', - Render, + ExpressionValueRender, Arguments, - Render + ExpressionValueRender > { const { help, args: argHelp } = getFunctionHelp().render; diff --git a/x-pack/plugins/canvas/public/application.tsx b/x-pack/plugins/canvas/public/application.tsx index b7910c8293d80..30b2d78a6b1fe 100644 --- a/x-pack/plugins/canvas/public/application.tsx +++ b/x-pack/plugins/canvas/public/application.tsx @@ -32,7 +32,12 @@ import { init as initStatsReporter } from './lib/ui_metric'; import { CapabilitiesStrings } from '../i18n'; -import { startServices, services, LegacyServicesProvider, CanvasPluginServices } from './services'; +import { + startLegacyServices, + services, + LegacyServicesProvider, + CanvasPluginServices, +} from './services'; import { initFunctions } from './functions'; // @ts-expect-error untyped local import { appUnload } from './state/actions/app'; @@ -93,9 +98,11 @@ export const initializeCanvas = async ( setupPlugins: CanvasSetupDeps, startPlugins: CanvasStartDeps, registries: SetupRegistries, - appUpdater: BehaviorSubject + appUpdater: BehaviorSubject, + pluginServices: PluginServices ) => { - await startServices(coreSetup, coreStart, setupPlugins, startPlugins, appUpdater); + await startLegacyServices(coreSetup, coreStart, setupPlugins, startPlugins, appUpdater); + const { expressions } = pluginServices.getServices(); // Adding these functions here instead of in plugin.ts. // Some of these functions have deep dependencies into Canvas, which was bulking up the size @@ -108,13 +115,13 @@ export const initializeCanvas = async ( }); for (const fn of canvasFunctions) { - services.expressions.getService().registerFunction(fn); + expressions.registerFunction(fn); } // Create Store const canvasStore = await createStore(coreSetup); - registerLanguage(Object.values(services.expressions.getService().getFunctions())); + registerLanguage(Object.values(expressions.getFunctions())); // Init Registries initRegistries(); @@ -145,10 +152,7 @@ export const initializeCanvas = async ( }, ], content: (domNode) => { - ReactDOM.render( - , - domNode - ); + ReactDOM.render(, domNode); return () => ReactDOM.unmountComponentAtNode(domNode); }, }); diff --git a/x-pack/plugins/canvas/public/components/element_content/element_content.js b/x-pack/plugins/canvas/public/components/element_content/element_content.tsx similarity index 58% rename from x-pack/plugins/canvas/public/components/element_content/element_content.js rename to x-pack/plugins/canvas/public/components/element_content/element_content.tsx index ee0ce5193102e..d508c21cb17d6 100644 --- a/x-pack/plugins/canvas/public/components/element_content/element_content.js +++ b/x-pack/plugins/canvas/public/components/element_content/element_content.tsx @@ -6,49 +6,62 @@ */ import React from 'react'; -import PropTypes from 'prop-types'; +import { omitBy, isNil } from 'lodash'; +// @ts-expect-error Untyped library import Style from 'style-it'; + +import { ExpressionRenderer } from 'src/plugins/expressions'; import { getType } from '@kbn/interpreter/common'; import { Loading } from '../loading'; import { RenderWithFn } from '../render_with_fn'; +// @ts-expect-error Untyped local import { ElementShareContainer } from '../element_share_container'; import { InvalidExpression } from './invalid_expression'; import { InvalidElementType } from './invalid_element_type'; +import { RendererHandlers } from '../../../types'; +import { Renderable } from '../../../canvas_plugin_src/functions/common/render'; -const isLoading = (renderable, state) => !state || !renderable; - -const isNotValidForRendering = (renderable, renderFunction) => - renderable && getType(renderable) !== 'render' && !renderFunction; - -const isNotValidExpression = (renderable, renderFunction, state) => - state === 'error' || // The renderable has an error - getType(renderable) !== 'render' || // The renderable isn't, well, renderable - !renderFunction; // We can't find an element in the registry for this +export interface Props { + renderable: Renderable | null; + renderFunction: ExpressionRenderer | null; + height: number; + width: number; + handlers: RendererHandlers; + backgroundColor: string; + selectElement: () => void; + state: string; +} -export const ElementContent = (props) => { +export const ElementContent = (props: Props) => { const { renderable, renderFunction, width, height, handlers, backgroundColor, state } = props; const { onComplete } = handlers; - if (isLoading(renderable, state)) { + if (!state || !renderable) { return ; } // renderable is available, but no matching element is found, render invalid - if (isNotValidForRendering(renderable, renderFunction)) { + if (renderable && getType(renderable) !== 'render' && !renderFunction) { return ; } // error state, render invalid expression notice - if (isNotValidExpression(renderable, renderFunction, state)) { + if ( + state === 'error' || // The renderable has an error + getType(renderable) !== 'render' || // The renderable isn't, well, renderable + !renderFunction // We can't find an element in the registry for this + ) { return ; } + const containerStyle = omitBy(renderable.containerStyle, isNil); + return Style.it( renderable.css,
{
); }; - -ElementContent.propTypes = { - renderable: PropTypes.shape({ - css: PropTypes.string, - value: PropTypes.object, - }), - renderFunction: PropTypes.shape({ - name: PropTypes.string, - render: PropTypes.func, - reuseDomNode: PropTypes.bool, - }), - size: PropTypes.object, - handlers: PropTypes.shape({ - setFilter: PropTypes.func.isRequired, - getFilter: PropTypes.func.isRequired, - done: PropTypes.func.isRequired, - onComplete: PropTypes.func.isRequired, // local, not passed through - }).isRequired, - state: PropTypes.string, - backgroundColor: PropTypes.string, -}; diff --git a/x-pack/plugins/canvas/public/components/element_content/index.js b/x-pack/plugins/canvas/public/components/element_content/index.js deleted file mode 100644 index 3039b0276db17..0000000000000 --- a/x-pack/plugins/canvas/public/components/element_content/index.js +++ /dev/null @@ -1,32 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import PropTypes from 'prop-types'; -import { connect } from 'react-redux'; -import { compose, withProps } from 'recompose'; -import { get } from 'lodash'; -import { withServices } from '../../services'; -import { getSelectedPage, getPageById } from '../../state/selectors/workpad'; -import { ElementContent as Component } from './element_content'; - -const mapStateToProps = (state) => ({ - backgroundColor: getPageById(state, getSelectedPage(state)).style.background, -}); - -export const ElementContent = compose( - connect(mapStateToProps), - withServices, - withProps(({ renderable, services }) => ({ - renderFunction: services.expressions.getRenderer(get(renderable, 'as')), - })) -)(Component); - -ElementContent.propTypes = { - renderable: PropTypes.shape({ - as: PropTypes.string, - }), -}; diff --git a/x-pack/plugins/canvas/public/components/element_content/index.tsx b/x-pack/plugins/canvas/public/components/element_content/index.tsx new file mode 100644 index 0000000000000..bdbfc205d4c81 --- /dev/null +++ b/x-pack/plugins/canvas/public/components/element_content/index.tsx @@ -0,0 +1,27 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import React from 'react'; +import { useSelector } from 'react-redux'; +import { getSelectedPage, getPageById } from '../../state/selectors/workpad'; +import { useExpressionsService } from '../../services'; +import { ElementContent as Component, Props as ComponentProps } from './element_content'; +import { State } from '../../../types'; + +export type Props = Omit; + +export const ElementContent = (props: Props) => { + const expressionsService = useExpressionsService(); + const selectedPageId = useSelector(getSelectedPage); + const backgroundColor = + useSelector((state: State) => getPageById(state, selectedPageId)?.style.background) || ''; + const { renderable } = props; + + const renderFunction = renderable ? expressionsService.getRenderer(renderable.as) : null; + + return ; +}; diff --git a/x-pack/plugins/canvas/public/components/element_content/invalid_element_type.js b/x-pack/plugins/canvas/public/components/element_content/invalid_element_type.tsx similarity index 89% rename from x-pack/plugins/canvas/public/components/element_content/invalid_element_type.js rename to x-pack/plugins/canvas/public/components/element_content/invalid_element_type.tsx index 4067e12051588..d1801bbce88a9 100644 --- a/x-pack/plugins/canvas/public/components/element_content/invalid_element_type.js +++ b/x-pack/plugins/canvas/public/components/element_content/invalid_element_type.tsx @@ -13,7 +13,12 @@ import React from 'react'; import PropTypes from 'prop-types'; -export const InvalidElementType = ({ renderableType, selectElement }) => ( +export interface Props { + renderableType: string; + selectElement: () => void; +} + +export const InvalidElementType = ({ renderableType, selectElement }: Props) => (

Element not found: {renderableType}

); diff --git a/x-pack/plugins/canvas/public/components/element_content/invalid_expression.js b/x-pack/plugins/canvas/public/components/element_content/invalid_expression.tsx similarity index 86% rename from x-pack/plugins/canvas/public/components/element_content/invalid_expression.js rename to x-pack/plugins/canvas/public/components/element_content/invalid_expression.tsx index 6c2c103fa2b9e..cd6f6605de57a 100644 --- a/x-pack/plugins/canvas/public/components/element_content/invalid_expression.js +++ b/x-pack/plugins/canvas/public/components/element_content/invalid_expression.tsx @@ -13,7 +13,11 @@ import React from 'react'; import PropTypes from 'prop-types'; -export const InvalidExpression = ({ selectElement }) => ( +export interface Props { + selectElement: () => void; +} + +export const InvalidExpression = ({ selectElement }: Props) => (

Invalid expression

); diff --git a/x-pack/plugins/canvas/public/components/expression/index.tsx b/x-pack/plugins/canvas/public/components/expression/index.tsx index cc7a38a7d4406..c4b271bb74663 100644 --- a/x-pack/plugins/canvas/public/components/expression/index.tsx +++ b/x-pack/plugins/canvas/public/components/expression/index.tsx @@ -8,7 +8,7 @@ import React, { FC, useState, useCallback, useMemo, useEffect } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { fromExpression } from '@kbn/interpreter/common'; -import { useServices } from '../../services'; +import { useExpressionsService } from '../../services'; import { getSelectedPage, getSelectedElement } from '../../state/selectors/workpad'; // @ts-expect-error import { setExpression, flushContext } from '../../state/actions/elements'; @@ -45,7 +45,7 @@ export const Expression: FC = ({ done }) => { }; const ExpressionContainer: FC = ({ done, element, pageId }) => { - const services = useServices(); + const expressions = useExpressionsService(); const dispatch = useDispatch(); const [isCompact, setCompact] = useState(true); const toggleCompactView = useCallback(() => { @@ -114,7 +114,7 @@ const ExpressionContainer: FC = ({ done, element, page ): Promise { const context = { variables }; - return await expressionsService - .getService() - .execute(ast, null, context) - .getData() - .pipe(pluck('result')) - .toPromise(); + const { execute } = pluginServices.getServices().expressions; + return await execute(ast, null, context).getData().pipe(pluck('result')).toPromise(); } /** @@ -49,9 +45,8 @@ export async function runInterpreter( const context = { variables }; try { - const renderable = await expressionsService - .getService() - .execute(ast, input, context) + const { execute } = pluginServices.getServices().expressions; + const renderable = await execute(ast, input, context) .getData() .pipe(pluck('result')) .toPromise(); diff --git a/x-pack/plugins/canvas/public/plugin.tsx b/x-pack/plugins/canvas/public/plugin.tsx index 2df5bf56e2782..841f4b6d7c157 100644 --- a/x-pack/plugins/canvas/public/plugin.tsx +++ b/x-pack/plugins/canvas/public/plugin.tsx @@ -31,6 +31,7 @@ import { Start as InspectorStart } from '../../../../src/plugins/inspector/publi import { BfetchPublicSetup } from '../../../../src/plugins/bfetch/public'; import { PresentationUtilPluginStart } from '../../../../src/plugins/presentation_util/public'; import { getPluginApi, CanvasApi } from './plugin_api'; +import { setupExpressions } from './setup_expressions'; import { pluginServiceRegistry } from './services/kibana'; export { CoreStart, CoreSetup }; @@ -87,12 +88,15 @@ export class CanvasPlugin const lastPath = getSessionStorage().get( `${SESSIONSTORAGE_LASTPATH}:${coreSetup.http.basePath.get()}` ); + if (lastPath) { this.appUpdater.next(() => ({ defaultPath: `#${lastPath}`, })); } + setupExpressions({ coreSetup, setupPlugins }); + coreSetup.application.register({ category: DEFAULT_APP_CATEGORIES.kibana, id: 'canvas', @@ -124,7 +128,8 @@ export class CanvasPlugin setupPlugins, startPlugins, registries, - this.appUpdater + this.appUpdater, + pluginServices ); const unmount = renderApp({ coreStart, startPlugins, params, canvasStore, pluginServices }); @@ -145,6 +150,7 @@ export class CanvasPlugin const { argTypeSpecs } = await import('./expression_types/arg_types'); return argTypeSpecs; }); + canvasApi.addTransitions(async () => { const { transitions } = await import('./transitions'); return transitions; diff --git a/x-pack/plugins/canvas/public/services/expressions.ts b/x-pack/plugins/canvas/public/services/expressions.ts new file mode 100644 index 0000000000000..a1af0fba50a5c --- /dev/null +++ b/x-pack/plugins/canvas/public/services/expressions.ts @@ -0,0 +1,10 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { ExpressionsService } from '../../../../../src/plugins/expressions/public'; + +export type CanvasExpressionsService = ExpressionsService; diff --git a/x-pack/plugins/canvas/public/services/index.ts b/x-pack/plugins/canvas/public/services/index.ts index 1348463cc6008..adfb537632769 100644 --- a/x-pack/plugins/canvas/public/services/index.ts +++ b/x-pack/plugins/canvas/public/services/index.ts @@ -8,24 +8,28 @@ export * from './legacy'; import { PluginServices } from '../../../../../src/plugins/presentation_util/public'; -import { CanvasWorkpadService } from './workpad'; +import { CanvasExpressionsService } from './expressions'; import { CanvasNavLinkService } from './nav_link'; import { CanvasNotifyService } from './notify'; import { CanvasPlatformService } from './platform'; import { CanvasReportingService } from './reporting'; +import { CanvasWorkpadService } from './workpad'; export interface CanvasPluginServices { - workpad: CanvasWorkpadService; + expressions: CanvasExpressionsService; + navLink: CanvasNavLinkService; notify: CanvasNotifyService; platform: CanvasPlatformService; reporting: CanvasReportingService; - navLink: CanvasNavLinkService; + workpad: CanvasWorkpadService; } export const pluginServices = new PluginServices(); -export const useWorkpadService = () => (() => pluginServices.getHooks().workpad.useService())(); +export const useExpressionsService = () => + (() => pluginServices.getHooks().expressions.useService())(); export const useNavLinkService = () => (() => pluginServices.getHooks().navLink.useService())(); export const useNotifyService = () => (() => pluginServices.getHooks().notify.useService())(); export const usePlatformService = () => (() => pluginServices.getHooks().platform.useService())(); export const useReportingService = () => (() => pluginServices.getHooks().reporting.useService())(); +export const useWorkpadService = () => (() => pluginServices.getHooks().workpad.useService())(); diff --git a/x-pack/plugins/canvas/public/services/kibana/expressions.ts b/x-pack/plugins/canvas/public/services/kibana/expressions.ts new file mode 100644 index 0000000000000..4e3bb52a5d449 --- /dev/null +++ b/x-pack/plugins/canvas/public/services/kibana/expressions.ts @@ -0,0 +1,19 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { KibanaPluginServiceFactory } from '../../../../../../src/plugins/presentation_util/public'; + +import { CanvasStartDeps } from '../../plugin'; +import { CanvasExpressionsService } from '../expressions'; + +export type CanvasExpressionsServiceFactory = KibanaPluginServiceFactory< + CanvasExpressionsService, + CanvasStartDeps +>; + +export const expressionsServiceFactory: CanvasExpressionsServiceFactory = ({ startPlugins }) => + startPlugins.expressions.fork(); diff --git a/x-pack/plugins/canvas/public/services/kibana/index.ts b/x-pack/plugins/canvas/public/services/kibana/index.ts index f3f789272cfe4..b5dc1690a23ee 100644 --- a/x-pack/plugins/canvas/public/services/kibana/index.ts +++ b/x-pack/plugins/canvas/public/services/kibana/index.ts @@ -12,28 +12,31 @@ import { KibanaPluginServiceParams, } from '../../../../../../src/plugins/presentation_util/public'; -import { workpadServiceFactory } from './workpad'; +import { CanvasPluginServices } from '..'; +import { CanvasStartDeps } from '../../plugin'; +import { expressionsServiceFactory } from './expressions'; import { navLinkServiceFactory } from './nav_link'; import { notifyServiceFactory } from './notify'; import { platformServiceFactory } from './platform'; import { reportingServiceFactory } from './reporting'; -import { CanvasPluginServices } from '..'; -import { CanvasStartDeps } from '../../plugin'; +import { workpadServiceFactory } from './workpad'; -export { workpadServiceFactory } from './workpad'; +export { expressionsServiceFactory } from './expressions'; export { notifyServiceFactory } from './notify'; export { platformServiceFactory } from './platform'; export { reportingServiceFactory } from './reporting'; +export { workpadServiceFactory } from './workpad'; export const pluginServiceProviders: PluginServiceProviders< CanvasPluginServices, KibanaPluginServiceParams > = { - workpad: new PluginServiceProvider(workpadServiceFactory), + expressions: new PluginServiceProvider(expressionsServiceFactory), navLink: new PluginServiceProvider(navLinkServiceFactory), notify: new PluginServiceProvider(notifyServiceFactory), platform: new PluginServiceProvider(platformServiceFactory), reporting: new PluginServiceProvider(reportingServiceFactory), + workpad: new PluginServiceProvider(workpadServiceFactory), }; export const pluginServiceRegistry = new PluginServiceRegistry< diff --git a/x-pack/plugins/canvas/public/services/legacy/context.tsx b/x-pack/plugins/canvas/public/services/legacy/context.tsx index cd10c1c9bf9a7..53702ac0acc89 100644 --- a/x-pack/plugins/canvas/public/services/legacy/context.tsx +++ b/x-pack/plugins/canvas/public/services/legacy/context.tsx @@ -21,7 +21,6 @@ export interface WithServicesProps { const defaultContextValue = { embeddables: {}, - expressions: {}, search: {}, }; @@ -29,7 +28,6 @@ export const ServicesContext = createContext(defaultContextValue export const useServices = () => useContext(ServicesContext); export const useEmbeddablesService = () => useServices().embeddables; -export const useExpressionsService = () => useServices().expressions; export const useLabsService = () => useServices().labs; export const withServices = (type: ComponentType) => { const EnhancedType: FC = (props) => @@ -44,7 +42,6 @@ export const LegacyServicesProvider: FC<{ const specifiedProviders: CanvasServiceProviders = { ...services, ...providers }; const value = { embeddables: specifiedProviders.embeddables.getService(), - expressions: specifiedProviders.expressions.getService(), search: specifiedProviders.search.getService(), labs: specifiedProviders.labs.getService(), }; diff --git a/x-pack/plugins/canvas/public/services/legacy/index.ts b/x-pack/plugins/canvas/public/services/legacy/index.ts index a28d86e8ef7d2..4b2bae745c5c7 100644 --- a/x-pack/plugins/canvas/public/services/legacy/index.ts +++ b/x-pack/plugins/canvas/public/services/legacy/index.ts @@ -9,7 +9,6 @@ import { BehaviorSubject } from 'rxjs'; import { CoreSetup, CoreStart, AppUpdater } from '../../../../../../src/core/public'; import { CanvasSetupDeps, CanvasStartDeps } from '../../plugin'; import { embeddablesServiceFactory } from './embeddables'; -import { expressionsServiceFactory } from './expressions'; import { searchServiceFactory } from './search'; import { labsServiceFactory } from './labs'; @@ -71,7 +70,6 @@ export type ServiceFromProvider

= P extends CanvasServiceProvider ? export const services = { embeddables: new CanvasServiceProvider(embeddablesServiceFactory), - expressions: new CanvasServiceProvider(expressionsServiceFactory), search: new CanvasServiceProvider(searchServiceFactory), labs: new CanvasServiceProvider(labsServiceFactory), }; @@ -80,12 +78,11 @@ export type CanvasServiceProviders = typeof services; export interface CanvasServices { embeddables: ServiceFromProvider; - expressions: ServiceFromProvider; search: ServiceFromProvider; labs: ServiceFromProvider; } -export const startServices = async ( +export const startLegacyServices = async ( coreSetup: CoreSetup, coreStart: CoreStart, canvasSetupPlugins: CanvasSetupDeps, @@ -103,8 +100,4 @@ export const stopServices = () => { Object.values(services).forEach((provider) => provider.stop()); }; -export const { - embeddables: embeddableService, - expressions: expressionsService, - search: searchService, -} = services; +export const { embeddables: embeddableService, search: searchService } = services; diff --git a/x-pack/plugins/canvas/public/services/legacy/stubs/expressions.ts b/x-pack/plugins/canvas/public/services/legacy/stubs/expressions.ts deleted file mode 100644 index 1093bb7452133..0000000000000 --- a/x-pack/plugins/canvas/public/services/legacy/stubs/expressions.ts +++ /dev/null @@ -1,25 +0,0 @@ -/* - * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one - * or more contributor license agreements. Licensed under the Elastic License - * 2.0; you may not use this file except in compliance with the Elastic License - * 2.0. - */ - -import { AnyExpressionRenderDefinition } from 'src/plugins/expressions'; -import { ExpressionsService } from '../'; -import { plugin } from '../../../../../../../src/plugins/expressions/public'; -import { functions as functionDefinitions } from '../../../../canvas_plugin_src/functions/common'; -import { renderFunctions } from '../../../../canvas_plugin_src/renderers/core'; - -const placeholder = {} as any; -const expressionsPlugin = plugin(placeholder); -const setup = expressionsPlugin.setup(placeholder); - -export const expressionsService: ExpressionsService = setup.fork(); - -export function setupExpressionsService() { - functionDefinitions.forEach((fn) => expressionsService.registerFunction(fn)); - renderFunctions.forEach((fn) => { - expressionsService.registerRenderer((fn as unknown) as AnyExpressionRenderDefinition); - }); -} diff --git a/x-pack/plugins/canvas/public/services/legacy/stubs/index.ts b/x-pack/plugins/canvas/public/services/legacy/stubs/index.ts index b9e6d106c03c3..40ab38df998cb 100644 --- a/x-pack/plugins/canvas/public/services/legacy/stubs/index.ts +++ b/x-pack/plugins/canvas/public/services/legacy/stubs/index.ts @@ -7,13 +7,11 @@ import { CanvasServices, services } from '../'; import { embeddablesService } from './embeddables'; -import { expressionsService } from './expressions'; import { labsService } from './labs'; import { searchService } from './search'; export const stubs: CanvasServices = { embeddables: embeddablesService, - expressions: expressionsService, search: searchService, labs: labsService, }; diff --git a/x-pack/plugins/canvas/public/services/stubs/expressions.ts b/x-pack/plugins/canvas/public/services/stubs/expressions.ts new file mode 100644 index 0000000000000..9bd07f76eb44f --- /dev/null +++ b/x-pack/plugins/canvas/public/services/stubs/expressions.ts @@ -0,0 +1,29 @@ +/* + * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one + * or more contributor license agreements. Licensed under the Elastic License + * 2.0; you may not use this file except in compliance with the Elastic License + * 2.0. + */ + +import { AnyExpressionRenderDefinition } from 'src/plugins/expressions'; +import { plugin } from '../../../../../../src/plugins/expressions/public'; +import { functions as functionDefinitions } from '../../../canvas_plugin_src/functions/common'; +import { renderFunctions } from '../../../canvas_plugin_src/renderers/core'; +import { PluginServiceFactory } from '../../../../../../src/plugins/presentation_util/public'; +import { CanvasExpressionsService } from '../expressions'; + +type CanvasExpressionsServiceFactory = PluginServiceFactory; + +export const expressionsServiceFactory: CanvasExpressionsServiceFactory = () => { + const placeholder = {} as any; + const expressionsPlugin = plugin(placeholder); + const setup = expressionsPlugin.setup(placeholder); + const expressionsService = setup.fork(); + + functionDefinitions.forEach((fn) => expressionsService.registerFunction(fn)); + renderFunctions.forEach((fn) => { + expressionsService.registerRenderer((fn as unknown) as AnyExpressionRenderDefinition); + }); + + return expressionsService; +}; diff --git a/x-pack/plugins/canvas/public/services/stubs/index.ts b/x-pack/plugins/canvas/public/services/stubs/index.ts index 31aac1bcddec0..ba7f38fb673cb 100644 --- a/x-pack/plugins/canvas/public/services/stubs/index.ts +++ b/x-pack/plugins/canvas/public/services/stubs/index.ts @@ -14,24 +14,27 @@ import { } from '../../../../../../src/plugins/presentation_util/public'; import { CanvasPluginServices } from '..'; -import { workpadServiceFactory } from './workpad'; +import { expressionsServiceFactory } from './expressions'; import { navLinkServiceFactory } from './nav_link'; import { notifyServiceFactory } from './notify'; import { platformServiceFactory } from './platform'; import { reportingServiceFactory } from './reporting'; +import { workpadServiceFactory } from './workpad'; -export { workpadServiceFactory } from './workpad'; +export { expressionsServiceFactory } from './expressions'; export { navLinkServiceFactory } from './nav_link'; export { notifyServiceFactory } from './notify'; export { platformServiceFactory } from './platform'; export { reportingServiceFactory } from './reporting'; +export { workpadServiceFactory } from './workpad'; export const pluginServiceProviders: PluginServiceProviders = { - workpad: new PluginServiceProvider(workpadServiceFactory), + expressions: new PluginServiceProvider(expressionsServiceFactory), navLink: new PluginServiceProvider(navLinkServiceFactory), notify: new PluginServiceProvider(notifyServiceFactory), platform: new PluginServiceProvider(platformServiceFactory), reporting: new PluginServiceProvider(reportingServiceFactory), + workpad: new PluginServiceProvider(workpadServiceFactory), }; export const pluginServiceRegistry = new PluginServiceRegistry( diff --git a/x-pack/plugins/canvas/public/services/legacy/expressions.ts b/x-pack/plugins/canvas/public/setup_expressions.ts similarity index 75% rename from x-pack/plugins/canvas/public/services/legacy/expressions.ts rename to x-pack/plugins/canvas/public/setup_expressions.ts index b48172172e3c7..3fc39564de0a9 100644 --- a/x-pack/plugins/canvas/public/services/legacy/expressions.ts +++ b/x-pack/plugins/canvas/public/setup_expressions.ts @@ -5,22 +5,24 @@ * 2.0. */ -import { CanvasServiceFactory } from '.'; -import { - ExpressionsService, - serializeProvider, -} from '../../../../../../src/plugins/expressions/common'; -import { API_ROUTE_FUNCTIONS } from '../../../common/lib/constants'; - -export const expressionsServiceFactory: CanvasServiceFactory = async ( +import { CoreSetup } from '../../../../src/core/public'; +import { serializeProvider } from '../../../../src/plugins/expressions/common'; +import { API_ROUTE_FUNCTIONS } from '../common/lib/constants'; + +import { CanvasSetupDeps } from './plugin'; + +// TODO: clintandrewhall - This is getting refactored shortly. https://github.com/elastic/kibana/issues/105675 +export const setupExpressions = async ({ coreSetup, - coreStart, setupPlugins, - startPlugins -) => { +}: { + coreSetup: CoreSetup; + setupPlugins: CanvasSetupDeps; +}) => { const { expressions, bfetch } = setupPlugins; let cached: Promise | null = null; + const loadServerFunctionWrappers = async () => { if (!cached) { cached = (async () => { @@ -51,6 +53,4 @@ export const expressionsServiceFactory: CanvasServiceFactory }; await loadServerFunctionWrappers(); - - return setupPlugins.expressions.fork(); };