import * as tasks from './task' ; import { MESSAGE } from './constant' ; const ctx = typeof self !== 'undefined' ? self : ( { } as any ) ; interface Event { type : string ; data : any ; } ctx . onmessage = ( event ) => { const { taskType , data } = event . data ; if ( ! taskType ) { return ; } const task = tasks [ taskType ] ; if ( typeof task === 'function' ) { const result = task ( ... data ) ; const parsed = JSON . parse ( JSON . stringify ( result ) ) ; ctx . postMessage ( { status : MESSAGE . SUCCESS , data : parsed } ) ; return ; } ctx . postMessage ( { status : MESSAGE . FAILURE } ) ; } ;