kibana/docs/development/plugins/expressions/public/kibana-plugin-plugins-expressions-public.expressionsservice.md
2020-10-30 06:01:45 +01:00

7.4 KiB

Home > kibana-plugin-plugins-expressions-public > ExpressionsService

ExpressionsService class

ExpressionsService class is used for multiple purposes:

  1. It implements the same Expressions service that can be used on both: (1) server-side and (2) browser-side. 2. It implements the same Expressions service that users can fork/clone, thus have their own instance of the Expressions plugin. 3. ExpressionsService defines the public contracts of *setup* and *start* Kibana Platform life-cycles for ease-of-use on server-side and browser-side. 4. ExpressionsService creates a bound version of all exported contract functions. 5. Functions are bound the way there are:

```ts registerFunction = (...args: Parameters<Executor['registerFunction']> ): ReturnType<Executor['registerFunction']> => this.executor.registerFunction(...args); ```

so that JSDoc appears in developers IDE when they use those plugins.expressions.registerFunction(.

Signature:

export declare class ExpressionsService implements PersistableState<ExpressionAstExpression> 

Constructors

Constructor Modifiers Description
(constructor)({ executor, renderers, }) Constructs a new instance of the ExpressionsService class

Properties

Property Modifiers Type Description
execute ExpressionsServiceStart['execute']
executor Executor
extract (state: ExpressionAstExpression) => {
state: ExpressionAstExpression;
references: SavedObjectReference[];
}
Extracts saved object references from expression AST
fork () => ExpressionsService
getFunction ExpressionsServiceStart['getFunction']
getFunctions () => ReturnType<Executor['getFunctions']> Returns POJO map of all registered expression functions, where keys are names of the functions and values are ExpressionFunction instances.
getRenderer ExpressionsServiceStart['getRenderer']
getRenderers () => ReturnType<ExpressionRendererRegistry['toJS']> Returns POJO map of all registered expression renderers, where keys are names of the renderers and values are ExpressionRenderer instances.
getType ExpressionsServiceStart['getType']
getTypes () => ReturnType<Executor['getTypes']> Returns POJO map of all registered expression types, where keys are names of the types and values are ExpressionType instances.
inject (state: ExpressionAstExpression, references: SavedObjectReference[]) => ExpressionAstExpression Injects saved object references into expression AST
migrate (state: SerializableState, version: string) => ExpressionAstExpression Injects saved object references into expression AST
migrateToLatest (state: unknown, version: string) => ExpressionAstExpression Injects saved object references into expression AST
registerFunction (functionDefinition: AnyExpressionFunctionDefinition | (() => AnyExpressionFunctionDefinition)) => void Register an expression function, which will be possible to execute as part of the expression pipeline.Below we register a function which simply sleeps for given number of milliseconds to delay the execution and outputs its input as-is.
expressions.registerFunction({
  name: 'sleep',
  args: {
    time: {
      aliases: ['_'],
      help: 'Time in milliseconds for how long to sleep',
      types: ['number'],
    },
  },
  help: '',
  fn: async (input, args, context) => {
    await new Promise(r => setTimeout(r, args.time));
    return input;
  },
}

The actual function is defined in the fn key. The function can be *async*. It receives three arguments: (1) input is the output of the previous function or the initial input of the expression if the function is first in chain; (2) args are function arguments as defined in expression string, that can be edited by user (e.g in case of Canvas); (3) context is a shared object passed to all functions that can be used for side-effects. | | registerRenderer | | (definition: AnyExpressionRenderDefinition | (() => AnyExpressionRenderDefinition)) => void | | | registerType | | (typeDefinition: AnyExpressionTypeDefinition | (() => AnyExpressionTypeDefinition)) => void | | | renderers | | ExpressionRendererRegistry | | | run | | ExpressionsServiceStart['run'] | | | telemetry | | (state: ExpressionAstExpression, telemetryData?: Record<string, any>) => Record<string, any> | Extracts telemetry from expression AST |

Methods

Method Modifiers Description
setup() Returns Kibana Platform *setup* life-cycle contract. Useful to return the same contract on server-side and browser-side.
start() Returns Kibana Platform *start* life-cycle contract. Useful to return the same contract on server-side and browser-side.
stop()