import { log } from '../util/logging' ; import { NativeComputationManager } from '../native_computation_manager' ; import { RustTimestampChannel , RustTimestampExtractChannel , RustFormatVerificationChannel } from '../native' ; import { ICheckFormatFlags , DateTimeReplacements } from '../../../../common/interfaces/interface.detect' ; import { ITicks , INeonNotification , ITimestampFormatResult , IDiscoverItem , ITimestampByFormatResult , IFormatCheckResult } from '../util/progress' ; import { CancelablePromise } from '../util/promise' ; export type TTimestampExtractAsyncEvents = 'chunk' | 'progress' | 'notification' ; export type TTimestampExtractAsyncEventChunk = ( event : ITimestampByFormatResult ) => void ; export type TTimestampExtractAsyncEventProgress = ( event : ITicks ) => void ; export type TTimestampExtractAsyncEventNotification = ( event : INeonNotification ) => void ; export type TTimestampExtractAsyncEventObject = TTimestampExtractAsyncEventChunk | TTimestampExtractAsyncEventProgress | TTimestampExtractAsyncEventNotification ; param inputString param formatString param { DateTimeReplacements } replacements export function exctractTimestamp ( inputString , formatString , replacements ) { return new CancelablePromise < void , void , TTimestampExtractAsyncEvents , TTimestampExtractAsyncEventObject > ( ( resolve , reject , cancel , refCancelCB , self ) => { log ( `exctractTimestamp called...` ) ; try { const channel = new RustTimestampExtractChannel ( inputString , formatString , replacements ) ; const computation = new NativeComputationManager < ITimestampByFormatResult > ( channel ) ; refCancelCB ( ( ) => { log ( `Get command "cancel" operation. Start cancellation` ) ; computation . requestShutdown ( ) ; } ) ; let totalTicks = 1 ; computation . onItem ( ( chunk ) => { self . emit ( 'chunk' , chunk ) ; } ) ; computation . onProgress ( ( ticks ) => { totalTicks = ticks . total ; self . emit ( 'progress' , ticks ) ; } ) ; computation . onStopped ( ( ) => { cancel ( ) ; } ) ; computation . onNotification ( ( notification ) => { self . emit ( 'notification' , notification ) ; } ) ; computation . onFinished ( ( ) => { self . emit ( 'progress' , { ellapsed : totalTicks , total : totalTicks } ) ; resolve ( ) ; } ) ; } catch ( err ) { if ( ! ( err instanceof Error ) ) { log ( `operation is stopped. Error isn't valid:` ) ; log ( err ) ; err = new Error ( `operation is stopped. Error isn't valid.` ) ; } else { log ( `operation is stopped due error: ${ err . message }` ) ; } reject ( err ) ; } } ) ; } export type TDiscoverTimespanAsyncEvents = 'chunk' | 'progress' | 'notification' ; export type TDiscoverTimespanAsyncEventChunk = ( event : ITimestampFormatResult ) => void ; export type TDiscoverTimespanAsyncEventProgress = ( event : ITicks ) => void ; export type TDiscoverTimespanAsyncEventNotification = ( event : INeonNotification ) => void ; export type TDiscoverTimespanAsyncEventObject = TDiscoverTimespanAsyncEventChunk | TDiscoverTimespanAsyncEventProgress | TDiscoverTimespanAsyncEventNotification ; param itemsToDiscover result export function discoverTimespanAsync ( itemsToDiscover ) { return new CancelablePromise < void , void , TDiscoverTimespanAsyncEvents , TDiscoverTimespanAsyncEventObject > ( ( resolve , reject , cancel , refCancelCB , self ) => { try { const timestampChannel = new RustTimestampChannel ( ) ; const computation = new NativeComputationManager < ITimestampFormatResult > ( timestampChannel ) ; refCancelCB ( ( ) => { log ( `Get command "break" operation. Starting breaking.` ) ; computation . requestShutdown ( ) ; } ) ; let totalTicks = 1 ; log ( 'start listening to rust side' ) ; computation . onItem ( ( chunk ) => { self . emit ( 'chunk' , chunk ) ; } ) ; computation . onProgress ( ( ticks ) => { log ( 'onProgress: ' + JSON . stringify ( ticks ) ) ; totalTicks = ticks . total ; self . emit ( 'progress' , ticks ) ; } ) ; computation . onStopped ( ( ) => { cancel ( ) ; } ) ; computation . onNotification ( ( notification ) => { self . emit ( 'notification' , notification ) ; } ) ; computation . onFinished ( ( ) => { self . emit ( 'progress' , { ellapsed : totalTicks , total : totalTicks } ) ; resolve ( ) ; } ) ; timestampChannel . start ( itemsToDiscover ) ; } catch ( err ) { if ( ! ( err instanceof Error ) ) { log ( `operation is stopped. Error isn't valid:` ) ; log ( err ) ; err = new Error ( `operation is stopped. Error isn't valid.` ) ; } else { log ( `operation is stopped due error: ${ err . message }` ) ; } reject ( err ) ; } } ) ; } export type TFormatVerificationAsyncEvents = 'chunk' | 'progress' | 'notification' ; export type TFormatVerificationAsyncEventChunk = ( event : IFormatCheckResult ) => void ; export type TFormatVerificationAsyncEventProgress = ( event : ITicks ) => void ; export type TFormatVerificationAsyncEventNotification = ( event : INeonNotification ) => void ; export type TFormatVerificationAsyncEventObject = TFormatVerificationAsyncEventChunk | TFormatVerificationAsyncEventProgress | TFormatVerificationAsyncEventNotification ; param { string } formatString param { ICheckFormatFlags } flags export function checkFormat ( formatString , flags = { miss_day : false , miss_month : false , miss_year : false } ) { return new CancelablePromise < void , void , TFormatVerificationAsyncEvents , TFormatVerificationAsyncEventObject > ( ( resolve , reject , cancel , refCancelCB , self ) => { log ( `checkFormat called...` ) ; try { const channel = new RustFormatVerificationChannel ( formatString , flags ) ; const computation = new NativeComputationManager < IFormatCheckResult > ( channel ) ; refCancelCB ( ( ) => { log ( `Get command "cancel" operation. Start cancellation` ) ; computation . requestShutdown ( ) ; } ) ; let totalTicks = 1 ; computation . onItem ( ( chunk ) => { self . emit ( 'chunk' , chunk ) ; } ) ; computation . onProgress ( ( ticks ) => { totalTicks = ticks . total ; self . emit ( 'progress' , ticks ) ; } ) ; computation . onStopped ( ( ) => { cancel ( ) ; } ) ; computation . onNotification ( ( notification ) => { self . emit ( 'notification' , notification ) ; } ) ; computation . onFinished ( ( ) => { self . emit ( 'progress' , { ellapsed : totalTicks , total : totalTicks } ) ; resolve ( ) ; } ) ; } catch ( err ) { if ( ! ( err instanceof Error ) ) { log ( `operation is stopped. Error isn't valid:` ) ; log ( err ) ; err = new Error ( `operation is stopped. Error isn't valid.` ) ; } else { log ( `operation is stopped due error: ${ err . message }` ) ; } reject ( err ) ; } } ) ; }