Skip to content

Commit bde212a

Browse files
Merge branch 'master' into stabilize-rollup-test
2 parents f62ca09 + 0340fac commit bde212a

File tree

36 files changed

+1176
-199
lines changed

36 files changed

+1176
-199
lines changed

src/plugins/expressions/common/execution/execution.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -336,16 +336,18 @@ describe('Execution', () => {
336336
});
337337

338338
test('execution state is "pending" while execution is in progress', async () => {
339+
jest.useFakeTimers();
339340
const execution = createExecution('sleep 20');
340341
execution.start(null);
341-
await new Promise(r => setTimeout(r, 5));
342+
jest.advanceTimersByTime(5);
342343
expect(execution.state.get().state).toBe('pending');
344+
jest.useRealTimers();
343345
});
344346

345347
test('execution state is "result" when execution successfully completes', async () => {
346348
const execution = createExecution('sleep 1');
347349
execution.start(null);
348-
await new Promise(r => setTimeout(r, 30));
350+
await execution.result;
349351
expect(execution.state.get().state).toBe('result');
350352
});
351353

src/plugins/expressions/common/service/expressions_services.ts

Lines changed: 115 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,41 @@ import { ExpressionRendererRegistry } from '../expression_renderers';
2222
import { ExpressionAstExpression } from '../ast';
2323
import { ExecutionContract } from '../execution/execution_contract';
2424

25-
export type ExpressionsServiceSetup = ReturnType<ExpressionsService['setup']>;
26-
export type ExpressionsServiceStart = ReturnType<ExpressionsService['start']>;
25+
/**
26+
* The public contract that `ExpressionsService` provides to other plugins
27+
* in Kibana Platform in *setup* life-cycle.
28+
*/
29+
export type ExpressionsServiceSetup = Pick<
30+
ExpressionsService,
31+
| 'getFunction'
32+
| 'getFunctions'
33+
| 'getRenderer'
34+
| 'getRenderers'
35+
| 'getType'
36+
| 'getTypes'
37+
| 'registerFunction'
38+
| 'registerRenderer'
39+
| 'registerType'
40+
| 'run'
41+
| 'fork'
42+
>;
43+
44+
/**
45+
* The public contract that `ExpressionsService` provides to other plugins
46+
* in Kibana Platform in *start* life-cycle.
47+
*/
48+
export type ExpressionsServiceStart = Pick<
49+
ExpressionsService,
50+
| 'getFunction'
51+
| 'getFunctions'
52+
| 'getRenderer'
53+
| 'getRenderers'
54+
| 'getType'
55+
| 'getTypes'
56+
| 'run'
57+
| 'execute'
58+
| 'fork'
59+
>;
2760

2861
export interface ExpressionServiceParams {
2962
executor?: Executor;
@@ -97,6 +130,14 @@ export class ExpressionsService {
97130
...args: Parameters<Executor['registerFunction']>
98131
): ReturnType<Executor['registerFunction']> => this.executor.registerFunction(...args);
99132

133+
public readonly registerType = (
134+
...args: Parameters<Executor['registerType']>
135+
): ReturnType<Executor['registerType']> => this.executor.registerType(...args);
136+
137+
public readonly registerRenderer = (
138+
...args: Parameters<ExpressionRendererRegistry['register']>
139+
): ReturnType<ExpressionRendererRegistry['register']> => this.renderers.register(...args);
140+
100141
/**
101142
* Executes expression string or a parsed expression AST and immediately
102143
* returns the result.
@@ -132,21 +173,51 @@ export class ExpressionsService {
132173
): Promise<Output> => this.executor.run<Input, Output, ExtraContext>(ast, input, context);
133174

134175
/**
135-
* Create a new instance of `ExpressionsService`. The new instance inherits
136-
* all state of the original `ExpressionsService`, including all expression
137-
* types, expression functions and context. Also, all new types and functions
138-
* registered in the original services AFTER the forking event will be
139-
* available in the forked instance. However, all new types and functions
140-
* registered in the forked instances will NOT be available to the original
141-
* service.
176+
* Get a registered `ExpressionFunction` by its name, which was registered
177+
* using the `registerFunction` method. The returned `ExpressionFunction`
178+
* instance is an internal representation of the function in Expressions
179+
* service - do not mutate that object.
142180
*/
143-
public readonly fork = (): ExpressionsService => {
144-
const executor = this.executor.fork();
145-
const renderers = this.renderers;
146-
const fork = new ExpressionsService({ executor, renderers });
181+
public readonly getFunction = (name: string): ReturnType<Executor['getFunction']> =>
182+
this.executor.getFunction(name);
147183

148-
return fork;
149-
};
184+
/**
185+
* Returns POJO map of all registered expression functions, where keys are
186+
* names of the functions and values are `ExpressionFunction` instances.
187+
*/
188+
public readonly getFunctions = (): ReturnType<Executor['getFunctions']> =>
189+
this.executor.getFunctions();
190+
191+
/**
192+
* Get a registered `ExpressionRenderer` by its name, which was registered
193+
* using the `registerRenderer` method. The returned `ExpressionRenderer`
194+
* instance is an internal representation of the renderer in Expressions
195+
* service - do not mutate that object.
196+
*/
197+
public readonly getRenderer = (name: string): ReturnType<ExpressionRendererRegistry['get']> =>
198+
this.renderers.get(name);
199+
200+
/**
201+
* Returns POJO map of all registered expression renderers, where keys are
202+
* names of the renderers and values are `ExpressionRenderer` instances.
203+
*/
204+
public readonly getRenderers = (): ReturnType<ExpressionRendererRegistry['toJS']> =>
205+
this.renderers.toJS();
206+
207+
/**
208+
* Get a registered `ExpressionType` by its name, which was registered
209+
* using the `registerType` method. The returned `ExpressionType`
210+
* instance is an internal representation of the type in Expressions
211+
* service - do not mutate that object.
212+
*/
213+
public readonly getType = (name: string): ReturnType<Executor['getType']> =>
214+
this.executor.getType(name);
215+
216+
/**
217+
* Returns POJO map of all registered expression types, where keys are
218+
* names of the types and values are `ExpressionType` instances.
219+
*/
220+
public readonly getTypes = (): ReturnType<Executor['getTypes']> => this.executor.getTypes();
150221

151222
/**
152223
* Starts expression execution and immediately returns `ExecutionContract`
@@ -168,53 +239,37 @@ export class ExpressionsService {
168239
return execution.contract;
169240
};
170241

171-
public setup() {
172-
const { executor, renderers, registerFunction, run, fork } = this;
173-
174-
const getFunction = executor.getFunction.bind(executor);
175-
const getFunctions = executor.getFunctions.bind(executor);
176-
const getRenderer = renderers.get.bind(renderers);
177-
const getRenderers = renderers.toJS.bind(renderers);
178-
const getType = executor.getType.bind(executor);
179-
const getTypes = executor.getTypes.bind(executor);
180-
const registerRenderer = renderers.register.bind(renderers);
181-
const registerType = executor.registerType.bind(executor);
182-
183-
return {
184-
fork,
185-
getFunction,
186-
getFunctions,
187-
getRenderer,
188-
getRenderers,
189-
getType,
190-
getTypes,
191-
registerFunction,
192-
registerRenderer,
193-
registerType,
194-
run,
195-
};
242+
/**
243+
* Create a new instance of `ExpressionsService`. The new instance inherits
244+
* all state of the original `ExpressionsService`, including all expression
245+
* types, expression functions and context. Also, all new types and functions
246+
* registered in the original services AFTER the forking event will be
247+
* available in the forked instance. However, all new types and functions
248+
* registered in the forked instances will NOT be available to the original
249+
* service.
250+
*/
251+
public readonly fork = (): ExpressionsService => {
252+
const executor = this.executor.fork();
253+
const renderers = this.renderers;
254+
const fork = new ExpressionsService({ executor, renderers });
255+
256+
return fork;
257+
};
258+
259+
/**
260+
* Returns Kibana Platform *setup* life-cycle contract. Useful to return the
261+
* same contract on server-side and browser-side.
262+
*/
263+
public setup(): ExpressionsServiceSetup {
264+
return this;
196265
}
197266

198-
public start() {
199-
const { execute, executor, renderers, run } = this;
200-
201-
const getFunction = executor.getFunction.bind(executor);
202-
const getFunctions = executor.getFunctions.bind(executor);
203-
const getRenderer = renderers.get.bind(renderers);
204-
const getRenderers = renderers.toJS.bind(renderers);
205-
const getType = executor.getType.bind(executor);
206-
const getTypes = executor.getTypes.bind(executor);
207-
208-
return {
209-
execute,
210-
getFunction,
211-
getFunctions,
212-
getRenderer,
213-
getRenderers,
214-
getType,
215-
getTypes,
216-
run,
217-
};
267+
/**
268+
* Returns Kibana Platform *start* life-cycle contract. Useful to return the
269+
* same contract on server-side and browser-side.
270+
*/
271+
public start(): ExpressionsServiceStart {
272+
return this;
218273
}
219274

220275
public stop() {}

src/plugins/expressions/public/mocks.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ const createStartContract = (): Start => {
6868
execute: jest.fn(),
6969
ExpressionLoader: jest.fn(),
7070
ExpressionRenderHandler: jest.fn(),
71+
fork: jest.fn(),
7172
getFunction: jest.fn(),
7273
getFunctions: jest.fn(),
7374
getRenderer: jest.fn(),

src/plugins/expressions/public/plugin.ts

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ export class ExpressionsPublicPlugin
177177
},
178178
};
179179

180-
return setup;
180+
return Object.freeze(setup);
181181
}
182182

183183
public start(core: CoreStart, { inspector, bfetch }: ExpressionsStartDeps): ExpressionsStart {
@@ -186,17 +186,19 @@ export class ExpressionsPublicPlugin
186186
setNotifications(core.notifications);
187187

188188
const { expressions } = this;
189-
const expressionsStart = expressions.start();
190-
191-
return {
192-
...expressionsStart,
189+
const start = {
190+
...expressions.start(),
193191
ExpressionLoader,
194192
ExpressionRenderHandler,
195193
loader,
196194
ReactExpressionRenderer,
197195
render,
198196
};
197+
198+
return Object.freeze(start);
199199
}
200200

201-
public stop() {}
201+
public stop() {
202+
this.expressions.stop();
203+
}
202204
}

src/plugins/expressions/server/index.ts

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,88 @@ import { ExpressionsServerPlugin } from './plugin';
2222

2323
export { ExpressionsServerSetup, ExpressionsServerStart } from './plugin';
2424

25+
// Kibana Platform.
26+
export { ExpressionsServerPlugin as Plugin };
27+
export * from './plugin';
2528
export function plugin(initializerContext: PluginInitializerContext) {
2629
return new ExpressionsServerPlugin(initializerContext);
2730
}
31+
32+
// Static exports.
33+
export {
34+
AnyExpressionFunctionDefinition,
35+
AnyExpressionTypeDefinition,
36+
ArgumentType,
37+
Datatable,
38+
DatatableColumn,
39+
DatatableColumnType,
40+
DatatableRow,
41+
Execution,
42+
ExecutionContainer,
43+
ExecutionContext,
44+
ExecutionParams,
45+
ExecutionState,
46+
Executor,
47+
ExecutorContainer,
48+
ExecutorState,
49+
ExpressionAstArgument,
50+
ExpressionAstExpression,
51+
ExpressionAstFunction,
52+
ExpressionAstNode,
53+
ExpressionFunction,
54+
ExpressionFunctionDefinition,
55+
ExpressionFunctionKibana,
56+
ExpressionFunctionParameter,
57+
ExpressionImage,
58+
ExpressionRenderDefinition,
59+
ExpressionRenderer,
60+
ExpressionRendererRegistry,
61+
ExpressionType,
62+
ExpressionTypeDefinition,
63+
ExpressionTypeStyle,
64+
ExpressionValue,
65+
ExpressionValueBoxed,
66+
ExpressionValueConverter,
67+
ExpressionValueError,
68+
ExpressionValueNum,
69+
ExpressionValueRender,
70+
ExpressionValueSearchContext,
71+
ExpressionValueUnboxed,
72+
Filter,
73+
Font,
74+
FontLabel,
75+
FontStyle,
76+
FontValue,
77+
FontWeight,
78+
format,
79+
formatExpression,
80+
FunctionsRegistry,
81+
IInterpreterRenderHandlers,
82+
InterpreterErrorType,
83+
IRegistry,
84+
KIBANA_CONTEXT_NAME,
85+
KibanaContext,
86+
KibanaDatatable,
87+
KibanaDatatableColumn,
88+
KibanaDatatableRow,
89+
KnownTypeToString,
90+
Overflow,
91+
parse,
92+
parseExpression,
93+
PointSeries,
94+
PointSeriesColumn,
95+
PointSeriesColumnName,
96+
PointSeriesColumns,
97+
PointSeriesRow,
98+
Range,
99+
SerializedDatatable,
100+
SerializedFieldFormat,
101+
Style,
102+
TextAlignment,
103+
TextDecoration,
104+
TypesRegistry,
105+
TypeString,
106+
TypeToString,
107+
UnmappedTypeStrings,
108+
ExpressionValueRender as Render,
109+
} from '../common';

src/plugins/expressions/server/mocks.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,17 @@ export type Start = jest.Mocked<ExpressionsServerStart>;
3030

3131
const createSetupContract = (): Setup => {
3232
const setupContract: Setup = {
33+
fork: jest.fn(),
34+
getFunction: jest.fn(),
35+
getFunctions: jest.fn(),
36+
getRenderer: jest.fn(),
37+
getRenderers: jest.fn(),
38+
getType: jest.fn(),
39+
getTypes: jest.fn(),
40+
registerFunction: jest.fn(),
41+
registerRenderer: jest.fn(),
42+
registerType: jest.fn(),
43+
run: jest.fn(),
3344
__LEGACY: {
3445
register: jest.fn(),
3546
registries: jest.fn(),
@@ -39,7 +50,17 @@ const createSetupContract = (): Setup => {
3950
};
4051

4152
const createStartContract = (): Start => {
42-
const startContract: Start = {};
53+
const startContract: Start = {
54+
execute: jest.fn(),
55+
fork: jest.fn(),
56+
getFunction: jest.fn(),
57+
getFunctions: jest.fn(),
58+
getRenderer: jest.fn(),
59+
getRenderers: jest.fn(),
60+
getType: jest.fn(),
61+
getTypes: jest.fn(),
62+
run: jest.fn(),
63+
};
4364

4465
return startContract;
4566
};

0 commit comments

Comments
 (0)