license import { PlanarTargetWasmModule } from '../../defs/planar-target.js' ; import { DEBUG_LEVEL , enableLogLevel , log } from '../utils/logger.js' ; import { PlanarTargetDetector } from './planar-detector.js' ; declare global { function importScripts ( ... urls ) ; function ModuleFactory ( seed ) ; } enableLogLevel ( DEBUG_LEVEL . VERBOSE ) ; const START_INDEX = 1000 ; let addCount = START_INDEX ; let detector ; self . onmessage = ( e ) => { if ( typeof e . data === 'string' ) { const pathPrefix = e . data ; if ( 'importScripts' in self ) { importScripts ( `${ pathPrefix }/lib/planar/planar-target-detector.js` ) ; const moduleSeed = { locateFile ( url ) { if ( url . endsWith ( '.wasm' ) ) { return `${ pathPrefix }/third_party/planar-image/${ url }` ; } return url ; } , onRuntimeInitialized ( ) { detector . process ( new ImageData ( 1 , 1 ) , Date . now ( ) ) ; ( self as any ) . postMessage ( 'ready' ) ; } } ; ( self as any ) . Module = ModuleFactory ( moduleSeed ) ; detector = new PlanarTargetDetector ( ) ; } return ; } const host = ( self as any ) ; const { type , data , id , msgId } = e . data ; switch ( type ) { case 'process' : try { const processResult = detector . process ( data , Date . now ( ) ) ; const detections = [ ] ; for ( let r = 0 ; r < processResult . size ( ) ; r ++ ) { detections . push ( processResult . get ( r ) . id ) ; } host . postMessage ( { msgId , data : detections } ) ; } catch ( e ) { log ( e . message , DEBUG_LEVEL . ERROR ) ; host . postMessage ( { msgId , data : [ ] } ) ; } break ; case 'add' : detector . addDetectionWithId ( addCount , data ) ; host . postMessage ( { msgId , data : { idx : addCount , id } } ) ; addCount ++ ; break ; case 'remove' : detector . cancelDetection ( data ) ; host . postMessage ( { msgId , data } ) ; break ; case 'reset' : addCount = START_INDEX ; host . postMessage ( { msgId } ) ; break ; } } ;