const { extract } = require ( './image-target/tracker/extract' ) ; const { buildTrackingImageList } = require ( './image-target/image-list' ) ; const postMessage = ( data , callback ) => { callback ( { data } ) ; } ; export class Worker { public onmessage = ( e ) => { console . log ( 'message' , e ) ; } ; postMessage ( data ) { this . processMessage ( { data } ) ; } processMessage ( msg ) { const { data } = msg ; if ( data . type === 'compile' ) { const { targetImages } = data ; const percentPerImage = 50.0 / targetImages . length ; let percent = 0.0 ; const list = [ ] ; for ( let i = 0 ; i < targetImages . length ; i ++ ) { const targetImage = targetImages [ i ] ; const imageList = buildTrackingImageList ( targetImage ) ; const percentPerAction = percentPerImage / imageList . length ; const trackingData = _extractTrackingFeatures ( imageList , ( _ ) => { percent += percentPerAction ; postMessage ( { type : 'progress' , percent } , this . onmessage ) ; } ) ; list . push ( trackingData ) ; } postMessage ( { type : 'compileDone' , list , } , this . onmessage ) ; } } } ; const _extractTrackingFeatures = ( imageList , doneCallback ) => { const featureSets = [ ] ; for ( let i = 0 ; i < imageList . length ; i ++ ) { const image = imageList [ i ] ; const points = extract ( image ) ; const featureSet = { data : image . data , scale : image . scale , width : image . width , height : image . height , points , } ; featureSets . push ( featureSet ) ; doneCallback ( i ) ; } return featureSets ; } ;