import { Action , Module , Mutation , VuexModule } from 'vuex-module-decorators' ; import { JobResult , TransformationRequest } from './transformation' ; import { TransformationRest } from './transformationRest' ; import { Data } from '@/datasource/datasource' ; import * as DatasourceRest from '@/datasource/datasourceRest' ; import { PIPELINE_SERVICE_URL } from '@/env' ; @ Module ( { namespaced : true } ) export default class TransformationModule extends VuexModule { private data : Data | null = null ; private function = '' ; private result : JobResult | null = null ; private isLoadingResult = false ; private isLoadingData = false ; private timeoutHandle : number | null = null ; private readonly transformationRest = new TransformationRest ( PIPELINE_SERVICE_URL ) ; @ Mutation setData ( value ) { this . data = value ; this . isLoadingData = false ; } @ Action setDataAndSubmit ( value ) { this . context . commit ( 'setData' , value ) ; this . context . dispatch ( 'scheduleSubmit' ) . catch ( ( ) => { } ) ; } @ Mutation setFunction ( value ) { this . function = value ; } @ Action setFunctionAndSubmit ( value ) { this . context . commit ( 'setFunction' , value ) ; this . context . dispatch ( 'scheduleSubmit' ) . catch ( ( ) => { } ) ; } @ Mutation setResult ( value ) { this . result = value ; this . isLoadingResult = false ; } @ Mutation setIsLoadingResult ( value ) { this . isLoadingResult = value ; } @ Mutation setIsLoadingData ( value ) { this . isLoadingData = value ; } @ Mutation private setTimeoutHandle ( value ) { this . timeoutHandle = value ; } @ Action async loadDataFromDatasource ( datasourceId ) { this . context . commit ( 'setIsLoadingData' , true ) ; const data = await DatasourceRest . getDatasourceData ( datasourceId ) ; this . context . dispatch ( 'setDataAndSubmit' , data ) . catch ( ( ) => { } ) ; } @ Action scheduleSubmit ( ) { if ( this . timeoutHandle != null ) { window . clearTimeout ( this . timeoutHandle ) ; } const handle = window . setTimeout ( ( ) => { this . context . dispatch ( 'transformData' ) . catch ( ( ) => { } ) ; } , 1500 ) ; this . context . commit ( 'setTimeoutHandle' , handle ) ; } @ Action async transformData ( ) { if ( this . data == null ) { return ; } this . context . commit ( 'setIsLoadingResult' , true ) ; this . context . commit ( 'setTimeoutHandle' , null ) ; const request = { data : this . data , func : this . function , } ; const result = await this . transformationRest . transformData ( request ) ; this . context . commit ( 'setResult' , result ) ; } }