import * as Rx from 'rxjs' ; import { mergeMap , share , observeOn } from 'rxjs/operators' ; import { summarizeEventStream , Update } from './common' ; import { OptimizerConfig , OptimizerEvent , OptimizerState , getBundleCacheEvent$ , getOptimizerCacheKey , watchBundlesForChanges$ , runWorkers , OptimizerInitializedEvent , createOptimizerStateSummarizer , handleOptimizerCompletion , } from './optimizer' ; export type OptimizerUpdate = Update < OptimizerEvent , OptimizerState > ; export type OptimizerUpdate$ = Rx . Observable < OptimizerUpdate > ; export function runOptimizer ( config ) { return Rx . defer ( async ( ) => { if ( process . platform === 'darwin' ) { try { require . resolve ( 'fsevents' ) ; } catch ( error ) { if ( error . code === 'MODULE_NOT_FOUND' ) { throw new Error ( '`fsevents` module is not installed, most likely because you need to follow the instructions at https://github.com/nodejs/node-gyp/blob/master/macOS_Catalina.md and re-bootstrap Kibana' ) ; } throw error ; } } return { startTime : Date . now ( ) , cacheKey : await getOptimizerCacheKey ( config ) , } ; } ) . pipe ( mergeMap ( ( { startTime , cacheKey } ) => { const bundleCacheEvent$ = getBundleCacheEvent$ ( config , cacheKey ) . pipe ( observeOn ( Rx . asyncScheduler ) , share ( ) ) ; const init$ = Rx . concat ( bundleCacheEvent$ , Rx . of < OptimizerInitializedEvent > ( { type : 'optimizer initialized' , } ) ) ; const changeEvent$ = config . watch ? watchBundlesForChanges$ ( bundleCacheEvent$ , startTime ) . pipe ( share ( ) ) : Rx . EMPTY ; const workerEvent$ = runWorkers ( config , cacheKey , bundleCacheEvent$ , changeEvent$ ) ; return summarizeEventStream < OptimizerEvent , OptimizerState > ( Rx . merge ( init$ , changeEvent$ , workerEvent$ ) , { phase : 'initializing' , compilerStates : [ ] , offlineBundles : [ ] , onlineBundles : [ ] , startTime , durSec : 0 , } , createOptimizerStateSummarizer ( config ) ) ; } ) , handleOptimizerCompletion ( config ) ) ; }