import { LoadingPhase , Message , MessageType } from '../common/message' ; import { ModelGeometry , ReaderOptions } from '../reader/model-geometry' ; function isImageBitmap ( obj ) { return obj . width != null && obj . height != null && obj . close != null ; } if ( self && self instanceof DedicatedWorkerGlobalScope ) { var worker = self as DedicatedWorkerGlobalScope ; worker . onmessage = ( e ) => { let model = e . data . model ; let headers = e . data . headers ; let options = e . data . options ; let geometry = new ModelGeometry ( ) ; geometry . onerror = ( msg ) => { const message = { type : MessageType . FAILED , message : msg , percent : 0 , phase : LoadingPhase . DOWNLOADING } ; worker . postMessage ( message ) ; console . error ( msg ) ; worker . close ( ) ; } ; geometry . onloaded = ( ) => { try { let result = { } ; let transferable = [ ] ; for ( let i in geometry ) { if ( ! geometry . hasOwnProperty ( i ) ) { continue ; } let prop = geometry [ i ] ; if ( typeof prop === "function" || i . startsWith ( "_" ) ) { continue ; } result [ i ] = prop ; if ( ArrayBuffer . isView ( prop ) ) { transferable . push ( prop . buffer ) ; } else if ( isImageBitmap ( prop ) ) { transferable . push ( prop ) ; } } const message = { type : MessageType . COMPLETED , message : "Read Completed" , percent : 100 , result : result , phase : LoadingPhase . READING } ; worker . postMessage ( message , transferable ) ; worker . close ( ) ; } catch ( e ) { const message = { type : MessageType . FAILED , message : e , percent : 0 , phase : LoadingPhase . READING } ; worker . postMessage ( message ) ; console . error ( e ) ; worker . close ( ) ; } } ; geometry . load ( model , headers , ( message ) => { worker . postMessage ( message ) ; } , options ) ; } ; }