import { StateContainer , createStateContainer , } from '../../../opensearch_dashboards_utils/common/state_containers' ; import { ExecutorState , defaultState as executorDefaultState } from '../executor' ; import { ExpressionAstExpression } from '../ast' ; import { ExpressionValue } from '../expression_types' ; export interface ExecutionState < Output = ExpressionValue > extends ExecutorState { ast : ExpressionAstExpression ; /**
     * Tracks state of execution.
     *
     * - `not-started` - before .start() method was called.
     * - `pending` - immediately after .start() method is called.
     * - `result` - when expression execution completed.
     * - `error` - when execution failed with error.
     */ state : 'not-started' | 'pending' | 'result' | 'error' ; /**
     * Result of the expression execution.
     */ result ? : Output ; /**
     * Error happened during the execution.
     */ error ? : Error ; } const executionDefaultState = { ... executorDefaultState , state : 'not-started' , ast : { type : 'expression' , chain : [ ] , } , } ; export interface ExecutionPureTransitions < Output = ExpressionValue > { start : ( state : ExecutionState < Output > ) => ( ) => ExecutionState < Output > ; setResult : ( state : ExecutionState < Output > ) => ( result : Output ) => ExecutionState < Output > ; setError : ( state : ExecutionState < Output > ) => ( error : Error ) => ExecutionState < Output > ; } export const executionPureTransitions = { start : ( state ) => ( ) => ( { ... state , state : 'pending' , } ) , setResult : ( state ) => ( result ) => ( { ... state , state : 'result' , result , } ) , setError : ( state ) => ( error ) => ( { ... state , state : 'error' , error , } ) , } ; export type ExecutionContainer < Output = ExpressionValue > = StateContainer < ExecutionState < Output > , ExecutionPureTransitions < Output > > ; const freeze = < T > ( state ) : T => state ; export const createExecutionContainer = < Output = ExpressionValue > ( state = executionDefaultState ) : ExecutionContainer < Output > => { const container = createStateContainer < ExecutionState < Output > , ExecutionPureTransitions < Output > , object > ( state , executionPureTransitions , { } , { freeze , } ) ; return container ; } ;