import { Logger } from "@oasis-engine/core" ; export class DRACOWorker { private _worker : Worker ; private _costs : { [ taskId ] : number ; } = { } ; private _currentLoad : number = 0 ; private _callbacks : { [ taskId ] : IResolveReject ; } = { } ; get currentLoad ( ) : number { return this . _currentLoad ; } constructor ( workerSourceURL , decoderWASMBinary ? ) { this . _worker = new Worker ( workerSourceURL ) ; this . _worker . onmessage = ( e ) => { const message = e . data ; switch ( message . type ) { case "decode" : this . _callbacks [ message . id ] . resolve ( message . geometry ) ; break ; case "error" : this . _callbacks [ message . id ] . reject ( message ) ; break ; default : Logger . error ( 'DRACOWorker: Unexpected message, "' + message . type + '"' ) ; } } ; if ( decoderWASMBinary ) { this . _worker . postMessage ( { type : "init" , decoderConfig : { wasmBinary : decoderWASMBinary } } ) ; } else { this . _worker . postMessage ( { type : "init" , decoderConfig : { } } ) ; } } setCosts ( taskId , cost ) { this . _costs [ taskId ] = cost ; } addCurrentLoad ( cost ) { this . _currentLoad += cost ; } setCallback ( taskId , resolve , reject ) { this . _callbacks [ taskId ] = { resolve , reject } ; } decode ( taskId , taskConfig , buffer ) { this . _worker . postMessage ( { type : "decode" , id : taskId , taskConfig , buffer } , [ buffer ] ) ; } releaseTask ( taskId ) { this . _currentLoad -= this . _costs [ taskId ] ; delete this . _callbacks [ taskId ] ; delete this . _costs [ taskId ] ; } } interface IResolveReject { resolve : ( any ) => void ; reject : ( any ) => void ; } export interface ITaskConfig { attributeIDs : { [ attribute ] : number ; } ; attributeTypes : { [ attribute ] : string ; } ; useUniqueIDs : boolean ; indexType : string ; }