@@ -7,15 +7,22 @@ class MyWorker {
77 if ( this . reciveDataFunctions [ data . type ] instanceof Function ) {
88 this . reciveDataFunctions [ data . type ] ( data . content ) ;
99 } else {
10- console . error ( `收到的data.type:${ data . type } 没有注册处理函数!` ) ;
10+ console . error ( `[MyWorker] 收到的data.type:${ data . type } 没有注册处理函数!` ) ;
1111 console . log ( data ) ;
1212 } ;
1313 } ;
14+ this . Worker . onerror = function ( err ) {
15+ console . log ( '[MyWorker]worker is suffering!' , err )
16+ } ;
1417 this . reciveDataFunctions = { } ;
18+ this . reciveData ( 'Log' , ( msg ) => { console . log ( `[MyWorkerScript]${ msg } ` ) ; } ) ;
19+ this . reciveData ( 'Error' , ( error_args ) => {
20+ console . error ( `[MyWorkerScript]遇到错误!\n${ error_args } ` ) ;
21+ } ) ;
1522 this . reciveData ( 'UnknownDatatype' , ( data ) => {
16- console . error ( `[MainThread ]发送给${ this . WebWorkScriptURL } 的data.type为:${ data . type } 的数据其MyWorkerScript没有注册处理函数!` ) ;
23+ console . error ( `[MyWorker ]发送给${ this . WebWorkScriptURL } 的data.type为:${ data . type } 的数据其MyWorkerScript没有注册处理函数!` ) ;
1724 console . log ( data ) ;
18- } )
25+ } ) ;
1926 } ;
2027
2128 sendData = ( dataType , dataContent , transferList = [ ] ) => {
@@ -26,23 +33,43 @@ class MyWorker {
2633 } ;
2734
2835 reciveData = ( dataType , dealDataContent = ( dataContent ) => { } ) => {
36+ if ( this . reciveDataFunctions [ dataType ] ) console . warn ( `警告!dataType:${ dataType } 的处理函数已经存在,进行覆盖!` )
2937 this . reciveDataFunctions [ dataType ] = dealDataContent ;
3038 } ;
39+
40+ unreciveData = ( dataType ) => {
41+ if ( dataType in this . reciveDataFunctions ) delete this . reciveDataFunctions [ dataType ] ;
42+ } ;
3143} ;
3244
3345class MyWorkerScript {
3446 constructor ( worker_self ) {
35- this . reciveDataFunctions = { } ;
3647 this . worker_self = worker_self ;
37- worker_self . onmessage = ( { data } ) => {
48+ this . reciveDataFunctions = { } ;
49+
50+ this . worker_self . onmessage = ( { data } ) => {
3851 if ( this . reciveDataFunctions [ data . type ] instanceof Function ) {
3952 this . reciveDataFunctions [ data . type ] ( data . content ) ;
4053 } else {
41- console . error ( `[WorkerThread ]收到的data的data.type:${ data . type } 没有注册处理函数!` ) ;
54+ console . error ( `[MyWorkerScript ]收到的data的data.type:${ data . type } 没有注册处理函数!` ) ;
4255 console . log ( data ) ;
4356 this . sendData ( 'UnknownDatatype' , data ) ;
4457 } ;
4558 } ;
59+ this . worker_self . onerror = ( ...args ) => {
60+ this . sendData ( 'Error' , args ) ;
61+ } ;
62+
63+ this . worker_self . addEventListener ( 'unhandledrejection' , ( event ) => {
64+ // the event object has two special properties:
65+ // event.promise - the promise that generated the error
66+ // event.reason - the unhandled error object
67+ this . sendData ( 'Error' , event . reason ) ;
68+ } ) ;
69+ } ;
70+
71+ log = ( msg ) => {
72+ this . sendData ( 'Log' , msg ) ;
4673 } ;
4774
4875 sendData = ( dataType , dataContent , transferList = [ ] , targetOrigin = undefined ) => {
0 commit comments