@@ -9,8 +9,8 @@ import { noJimpInstance, webWorkerInfo } from '../utils/errorMsg';
9
9
import { setItem , getItem , removeItem } from '../utils/storage' ;
10
10
import ROOT from '../utils/build' ;
11
11
import MainPropTypes from '../validators/props' ;
12
-
13
- const processImage = require ( '../utils/options' ) ;
12
+ import worker from 'workerize-loader?inline!../worker' ;
13
+ import processImage from '../utils/options' ;
14
14
15
15
class ProcessImage extends Component {
16
16
static propTypes = MainPropTypes ;
@@ -37,10 +37,7 @@ class ProcessImage extends Component {
37
37
this . checkStorageSupport ( ) ;
38
38
39
39
if ( typeof Worker !== 'undefined' && ! this . props . disableWebWorker ) {
40
- this . worker = work ( require . resolve ( '../worker.js' ) ) ;
41
- // this.worker = new NewWorker();
42
-
43
- this . sendPropsToWorker ( this . props , this . worker ) ;
40
+ this . worker = worker ( ) ;
44
41
}
45
42
} ;
46
43
@@ -56,11 +53,11 @@ class ProcessImage extends Component {
56
53
57
54
componentDidUpdate = ( ) => {
58
55
if ( this . props . image && ! this . props . disableRerender ) {
59
- if ( typeof Worker !== 'undefined' && ! this . props . disableWebWorker ) {
60
- this . sendPropsToWorker ( this . props , this . worker ) ;
61
- } else {
62
- this . processInMainThread ( this . props ) ;
63
- }
56
+ this . processInMainThreadOrInWebWorker (
57
+ this . worker ,
58
+ this . props ,
59
+ this . myStorage
60
+ ) ;
64
61
}
65
62
} ;
66
63
@@ -130,28 +127,20 @@ class ProcessImage extends Component {
130
127
} ) ;
131
128
} ;
132
129
133
- processInWebWorker = ( worker , props , storageReference ) => {
134
- if ( worker !== null ) {
135
- worker . onmessage = e => {
136
- // avoid loop
137
- if ( e . data . src !== this . state . src || e . data . err !== this . state . err ) {
138
- this . setState ( { src : e . data . src , err : e . data . err } ) ;
139
- setItem ( 'placeholder' , e . data . src , storageReference ) ;
140
- this . passPropsToParent ( props , e . data . src , e . data . err ) ;
141
- if ( typeof props . onProcessFinish === 'function' ) {
142
- props . onProcessFinish ( ) ;
143
- }
144
- }
145
- } ;
146
- }
147
- } ;
148
-
149
- sendPropsToWorker = ( props , worker ) => {
130
+ processInWebWorker = async ( worker , props , storageReference ) => {
150
131
if ( worker !== null ) {
151
- worker . postMessage ( {
132
+ const result = await worker . process ( {
152
133
props : filterPropsToListen ( props ) ,
153
134
image : props . image
154
135
} ) ;
136
+ if ( result . src !== this . state . src || result . err !== this . state . err ) {
137
+ this . setState ( { src : result . src , err : result . err } ) ;
138
+ setItem ( 'placeholder' , result . src , storageReference ) ;
139
+ this . passPropsToParent ( props , result . src , result . err ) ;
140
+ if ( typeof props . onProcessFinish === 'function' ) {
141
+ props . onProcessFinish ( ) ;
142
+ }
143
+ }
155
144
}
156
145
} ;
157
146
0 commit comments