Skip to content

Commit

Permalink
[expressions] Remove legacy APIs. (#75517) (#75573)
Browse files Browse the repository at this point in the history
  • Loading branch information
lukeelmers authored Aug 20, 2020
1 parent cf12eac commit b6b1079
Show file tree
Hide file tree
Showing 16 changed files with 27 additions and 504 deletions.
11 changes: 0 additions & 11 deletions src/legacy/ui/public/new_platform/new_platform.karma_mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,6 @@ export const npSetup = {
registerFunction: sinon.fake(),
registerRenderer: sinon.fake(),
registerType: sinon.fake(),
__LEGACY: {
renderers: {
register: () => undefined,
get: () => null,
},
getExecutor: () => ({
interpreter: {
interpretAst: () => {},
},
}),
},
},
data: {
autocomplete: {
Expand Down
3 changes: 0 additions & 3 deletions src/plugins/expressions/kibana.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@
"version": "kibana",
"server": true,
"ui": true,
"requiredPlugins": [
"bfetch"
],
"extraPublicDirs": ["common", "common/fonts"],
"requiredBundles": [
"kibanaUtils",
Expand Down
9 changes: 1 addition & 8 deletions src/plugins/expressions/public/loader.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import { first, skip, toArray } from 'rxjs/operators';
import { loader, ExpressionLoader } from './loader';
import { Observable } from 'rxjs';
import { ExpressionAstExpression, parseExpression, IInterpreterRenderHandlers } from '../common';
import { parseExpression, IInterpreterRenderHandlers } from '../common';

// eslint-disable-next-line
const { __getLastExecution } = require('./services');
Expand All @@ -42,13 +42,6 @@ jest.mock('./services', () => {
const moduleMock = {
__execution: undefined,
__getLastExecution: () => moduleMock.__execution,
getInterpreter: () => {
return {
interpretAst: async (expression: ExpressionAstExpression) => {
return { type: 'render', as: 'test' };
},
};
},
getRenderersRegistry: () => ({
get: (id: string) => renderers[id],
}),
Expand Down
27 changes: 2 additions & 25 deletions src/plugins/expressions/public/mocks.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import React from 'react';
import { ExpressionsSetup, ExpressionsStart, plugin as pluginInitializer } from '.';

import { coreMock } from '../../../core/public/mocks';
import { bfetchPluginMock } from '../../bfetch/public/mocks';

export type Setup = jest.Mocked<ExpressionsSetup>;
export type Start = jest.Mocked<ExpressionsStart>;
Expand All @@ -39,23 +38,6 @@ const createSetupContract = (): Setup => {
registerRenderer: jest.fn(),
registerType: jest.fn(),
run: jest.fn(),
__LEGACY: {
functions: {
register: () => {},
} as any,
renderers: {
register: () => {},
} as any,
types: {
register: () => {},
} as any,
getExecutor: () => ({
interpreter: {
interpretAst: (() => {}) as any,
},
}),
loadLegacyServerFunctionWrappers: () => Promise.resolve(),
},
};
return setupContract;
};
Expand Down Expand Up @@ -84,20 +66,15 @@ const createPlugin = async () => {
const coreSetup = coreMock.createSetup();
const coreStart = coreMock.createStart();
const plugin = pluginInitializer(pluginInitializerContext);
const setup = await plugin.setup(coreSetup, {
bfetch: bfetchPluginMock.createSetupContract(),
});
const setup = await plugin.setup(coreSetup);

return {
pluginInitializerContext,
coreSetup,
coreStart,
plugin,
setup,
doStart: async () =>
await plugin.start(coreStart, {
bfetch: bfetchPluginMock.createStartContract(),
}),
doStart: async () => await plugin.start(coreStart),
};
};

Expand Down
123 changes: 9 additions & 114 deletions src/plugins/expressions/public/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,75 +17,19 @@
* under the License.
*/

import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from '../../../core/public';
import { ExpressionExecutor } from './types';
import { PluginInitializerContext, CoreSetup, CoreStart, Plugin } from 'src/core/public';
import {
ExpressionRendererRegistry,
FunctionsRegistry,
serializeProvider,
TypesRegistry,
ExpressionsService,
ExpressionsServiceSetup,
ExpressionsServiceStart,
ExecutionContext,
} from '../common';
import { BfetchPublicSetup, BfetchPublicStart } from '../../bfetch/public';
import {
setCoreStart,
setInterpreter,
setRenderersRegistry,
setNotifications,
setExpressionsService,
} from './services';
import { setRenderersRegistry, setNotifications, setExpressionsService } from './services';
import { ReactExpressionRenderer } from './react_expression_renderer';
import { ExpressionLoader, loader } from './loader';
import { render, ExpressionRenderHandler } from './render';

export interface ExpressionsSetupDeps {
bfetch: BfetchPublicSetup;
}

export interface ExpressionsStartDeps {
bfetch: BfetchPublicStart;
}

export interface ExpressionsSetup extends ExpressionsServiceSetup {
/**
* @todo Get rid of these `__LEGACY` APIs.
*
* `__LEGACY` APIs are used by Canvas. It should be possible to stop
* using all of them (except `loadLegacyServerFunctionWrappers`) and use
* Kibana Platform plugin contracts instead.
*/
__LEGACY: {
/**
* Use `registerType` and `getTypes` instead.
*/
types: TypesRegistry;

/**
* Use `registerFunction` and `getFunctions` instead.
*/
functions: FunctionsRegistry;

/**
* Use `registerRenderer` and `getRenderers`, and `getRenderer` instead.
*/
renderers: ExpressionRendererRegistry;

/**
* Use `run` function instead.
*/
getExecutor: () => ExpressionExecutor;

/**
* This function is used by Canvas to load server-side function and create
* browser-side "wrapper" for each one. This function can be removed once
* we enable expressions on server-side: https://github.com/elastic/kibana/issues/46906
*/
loadLegacyServerFunctionWrappers: () => Promise<void>;
};
}
export type ExpressionsSetup = ExpressionsServiceSetup;

export interface ExpressionsStart extends ExpressionsServiceStart {
ExpressionLoader: typeof ExpressionLoader;
Expand All @@ -95,9 +39,7 @@ export interface ExpressionsStart extends ExpressionsServiceStart {
render: typeof render;
}

export class ExpressionsPublicPlugin
implements
Plugin<ExpressionsSetup, ExpressionsStart, ExpressionsSetupDeps, ExpressionsStartDeps> {
export class ExpressionsPublicPlugin implements Plugin<ExpressionsSetup, ExpressionsStart> {
private readonly expressions: ExpressionsService = new ExpressionsService();

constructor(initializerContext: PluginInitializerContext) {}
Expand All @@ -116,68 +58,21 @@ export class ExpressionsPublicPlugin
});
}

public setup(core: CoreSetup, { bfetch }: ExpressionsSetupDeps): ExpressionsSetup {
public setup(core: CoreSetup): ExpressionsSetup {
this.configureExecutor(core);

const { expressions } = this;
const { executor, renderers } = expressions;
const { renderers } = expressions;

setRenderersRegistry(renderers);
setExpressionsService(this.expressions);
setExpressionsService(expressions);

const expressionsSetup = expressions.setup();

// This is legacy. Should go away when we get rid of __LEGACY.
const getExecutor = (): ExpressionExecutor => {
return { interpreter: { interpretAst: expressionsSetup.run } };
};

setInterpreter(getExecutor().interpreter);

let cached: Promise<void> | null = null;
const loadLegacyServerFunctionWrappers = async () => {
if (!cached) {
cached = (async () => {
const serverFunctionList = await core.http.get(`/api/interpreter/fns`);
const batchedFunction = bfetch.batchedFunction({ url: `/api/interpreter/fns` });
const { serialize } = serializeProvider(executor.getTypes());

// For every sever-side function, register a client-side
// function that matches its definition, but which simply
// calls the server-side function endpoint.
Object.keys(serverFunctionList).forEach((functionName) => {
if (expressionsSetup.getFunction(functionName)) {
return;
}
const fn = () => ({
...serverFunctionList[functionName],
fn: (input: any, args: any) => {
return batchedFunction({ functionName, args, context: serialize(input) });
},
});
expressionsSetup.registerFunction(fn);
});
})();
}
return cached;
};

const setup: ExpressionsSetup = {
...expressionsSetup,
__LEGACY: {
types: executor.types,
functions: executor.functions,
renderers,
getExecutor,
loadLegacyServerFunctionWrappers,
},
};
const setup = expressions.setup();

return Object.freeze(setup);
}

public start(core: CoreStart, { bfetch }: ExpressionsStartDeps): ExpressionsStart {
setCoreStart(core);
public start(core: CoreStart): ExpressionsStart {
setNotifications(core.notifications);

const { expressions } = this;
Expand Down
13 changes: 3 additions & 10 deletions src/plugins/expressions/public/services.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,15 @@
*/

import { NotificationsStart } from 'kibana/public';
import { createKibanaUtilsCore, createGetterSetter } from '../../kibana_utils/public';
import { ExpressionInterpreter } from './types';
import { ExpressionsSetup } from './plugin';
import { ExpressionsService } from '../common';
import { createGetterSetter } from '../../kibana_utils/public';
import { ExpressionsService, ExpressionRendererRegistry } from '../common';

export const { getCoreStart, setCoreStart } = createKibanaUtilsCore();

export const [getInterpreter, setInterpreter] = createGetterSetter<ExpressionInterpreter>(
'Interpreter'
);
export const [getNotifications, setNotifications] = createGetterSetter<NotificationsStart>(
'Notifications'
);

export const [getRenderersRegistry, setRenderersRegistry] = createGetterSetter<
ExpressionsSetup['__LEGACY']['renderers']
ExpressionRendererRegistry
>('Renderers registry');

export const [getExpressionsService, setExpressionsService] = createGetterSetter<
Expand Down
2 changes: 1 addition & 1 deletion src/plugins/expressions/server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

import { PluginInitializerContext } from '../../../core/server';
import { PluginInitializerContext } from 'src/core/server';
import { ExpressionsServerPlugin } from './plugin';

export { ExpressionsServerSetup, ExpressionsServerStart } from './plugin';
Expand Down
Loading

0 comments on commit b6b1079

Please sign in to comment.